Skip to content

Commit

Permalink
actionFlags: split tag into shorthand and longhand
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jul 15, 2024
1 parent 3a4e1c3 commit b0b7d7c
Show file tree
Hide file tree
Showing 9 changed files with 78 additions and 62 deletions.
2 changes: 1 addition & 1 deletion defaultActions_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ func TestActionFlags(t *testing.T) {

cmd.Flag("alpha").Changed = true
a := actionFlags(cmd).Invoke(Context{Value: "-a"})
assertEqual(t, ActionValuesDescribed("b", "", "h", "help for this command").Tag("flags").NoSpace('b', 'h').Invoke(Context{}).Prefix("-a"), a)
assertEqual(t, ActionValuesDescribed("b", "", "h", "help for this command").Tag("shorthand flags").NoSpace('b', 'h').Invoke(Context{}).Prefix("-a"), a)
}

func TestActionExecCommandEnv(t *testing.T) {
Expand Down
32 changes: 20 additions & 12 deletions example-nonposix/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ func TestInterspersed(t *testing.T) {
"-delim-slash", "OptargDelimiter '/'",
).NoSpace('.').
Style(style.Yellow).
Tag("flags"))
Tag("shorthand flags"))

s.Run("-delim-colon:d1", "positional1", "-d").
Expect(carapace.ActionValues())
Expand All @@ -49,11 +49,15 @@ func TestRoot(t *testing.T) {
Usage("OptargDelimiter '/'"))

s.Run("-c").
Expect(carapace.ActionValuesDescribed(
"-c", "CountN",
"-count", "CountN").
NoSpace('.').
Tag("flags"))
Expect(carapace.Batch(
carapace.ActionValuesDescribed(
"-c", "CountN",
).Tag("shorthand flags"),
carapace.ActionValuesDescribed(
"-count", "CountN",
).Tag("longhand flags"),
).ToA().
NoSpace('.'))
})
}

Expand All @@ -68,11 +72,15 @@ func TestNargs(t *testing.T) {
Usage("Nargs"))

s.Run("--nargs-any", "na2", "-c").
Expect(carapace.ActionValuesDescribed(
"-c", "CountN",
"-count", "CountN").
NoSpace('.').
Tag("flags"))
Expect(carapace.Batch(
carapace.ActionValuesDescribed(
"-c", "CountN",
).Tag("shorthand flags"),
carapace.ActionValuesDescribed(
"-count", "CountN",
).Tag("longhand flags"),
).ToA().
NoSpace('.'))

s.Run("--nargs-any", "na1", "na2", "").
Expect(carapace.ActionValues("na3").
Expand All @@ -99,6 +107,6 @@ func TestNargs(t *testing.T) {
"--nargs-two", "Nargs").
Style(style.Magenta).
NoSpace('.').
Tag("flags"))
Tag("longhand flags"))
})
}
22 changes: 13 additions & 9 deletions example/cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,15 +212,19 @@ func TestDash(t *testing.T) {
Usage("action [pos1] [pos2] [--] [dashAny]..."))

s.Run("action", "--", "-").
Expect(carapace.ActionStyledValuesDescribed(
"--embedded-bool", "embedded bool flag", style.Default,
"--embedded-optarg", "embedded optarg flag", style.Yellow,
"--embedded-string", "embedded string flag", style.Blue,
"-h", "help for embedded", style.Default,
"--help", "help for embedded", style.Default).
Expect(carapace.Batch(
carapace.ActionStyledValuesDescribed(
"-h", "help for embedded", style.Default,
).Tag("shorthand flags"),
carapace.ActionStyledValuesDescribed(
"--embedded-bool", "embedded bool flag", style.Default,
"--embedded-optarg", "embedded optarg flag", style.Yellow,
"--embedded-string", "embedded string flag", style.Blue,
"--help", "help for embedded", style.Default,
).Tag("longhand flags"),
).ToA().
NoSpace('.').
Usage("action [pos1] [pos2] [--] [dashAny]...").
Tag("flags"))
Usage("action [pos1] [pos2] [--] [dashAny]..."))

s.Run("action", "--", "--").
Expect(carapace.ActionStyledValuesDescribed(
Expand All @@ -230,7 +234,7 @@ func TestDash(t *testing.T) {
"--help", "help for embedded", style.Default).
NoSpace('.').
Usage("action [pos1] [pos2] [--] [dashAny]...").
Tag("flags"))
Tag("longhand flags"))

s.Run("action", "--", "embeddedP1", "--embedded-optarg=").
Expect(carapace.ActionValues("eo1", "eo2", "eo3").
Expand Down
10 changes: 5 additions & 5 deletions example/cmd/chain_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ func TestShorthandChain(t *testing.T) {
"v", style.Blue,
).Prefix("-b").
NoSpace('c', 'o').
Tag("flags"))
Tag("shorthand flags"))

s.Run("chain", "-bc").
Expect(carapace.ActionStyledValues(
Expand All @@ -26,7 +26,7 @@ func TestShorthandChain(t *testing.T) {
"v", style.Blue,
).Prefix("-bc").
NoSpace('c', 'o').
Tag("flags"))
Tag("shorthand flags"))

s.Run("chain", "-bcc").
Expect(carapace.ActionStyledValues(
Expand All @@ -35,15 +35,15 @@ func TestShorthandChain(t *testing.T) {
"v", style.Blue,
).Prefix("-bcc").
NoSpace('c', 'o').
Tag("flags"))
Tag("shorthand flags"))

s.Run("chain", "-bcco").
Expect(carapace.ActionStyledValues(
"c", style.Default,
"v", style.Blue,
).Prefix("-bcco").
NoSpace('c').
Tag("flags"))
Tag("shorthand flags"))

s.Run("chain", "-bcco", "").
Expect(carapace.ActionValues(
Expand Down Expand Up @@ -75,6 +75,6 @@ func TestShorthandChain(t *testing.T) {
"o", style.Yellow,
).Prefix("-c").
NoSpace('c', 'o').
Tag("flags"))
Tag("shorthand flags"))
})
}
4 changes: 2 additions & 2 deletions example/cmd/interspersed_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,15 @@ func TestInterspersed(t *testing.T) {
).
StyleR(&style.Carapace.FlagArg).
NoSpace('.').
Tag("flags"))
Tag("longhand flags"))

s.Run("interspersed", "--bool", "--s").
Expect(carapace.ActionValuesDescribed(
"--string", "string flag",
).
StyleR(&style.Carapace.FlagArg).
NoSpace('.').
Tag("flags"))
Tag("longhand flags"))

s.Run("interspersed", "--bool", "").
Expect(carapace.ActionValues(
Expand Down
4 changes: 2 additions & 2 deletions example/cmd/modifier_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -323,7 +323,7 @@ func TestSplit(t *testing.T) {
Suffix(" ").
NoSpace('*').
Usage("Split()").
Tag("flags"))
Tag("longhand flags"))

s.Run("modifier", "--split", "pos1 --bool=").
Expect(carapace.ActionStyledValues(
Expand Down Expand Up @@ -405,7 +405,7 @@ func TestSplitP(t *testing.T) {
).NoSpace('*').
Prefix("pos1>subdir/file1.txt ").
Suffix(" ").
Tag("flags").
Tag("longhand flags").
Usage("SplitP()"))

s.Run("modifier", "--splitp", "pos1 1>").
Expand Down
44 changes: 24 additions & 20 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,21 +96,25 @@ func TestRoot(t *testing.T) {
).Tag("main commands"))

s.Run("-").
Expect(carapace.ActionStyledValuesDescribed(
"--chdir", "change work directory", style.Blue,
"-C", "change work directory", style.Blue,
"--array", "multiflag", style.Blue,
"-a", "multiflag", style.Blue,
"-h", "help for example", style.Default,
"--help", "help for example", style.Default,
"--persistentFlag", "Help message for persistentFlag", style.Yellow,
"--persistentFlag2", "Help message for persistentFlag2", style.Blue,
"-p", "Help message for persistentFlag", style.Yellow,
"--toggle", "Help message for toggle", style.Default,
"-t", "Help message for toggle", style.Default,
"-v", "version for example", style.Default,
"--version", "version for example", style.Default,
).NoSpace('.').Tag("flags"))
Expect(carapace.Batch(
carapace.ActionStyledValuesDescribed(
"-C", "change work directory", style.Blue,
"-a", "multiflag", style.Blue,
"-h", "help for example", style.Default,
"-p", "Help message for persistentFlag", style.Yellow,
"-t", "Help message for toggle", style.Default,
"-v", "version for example", style.Default,
).Tag("shorthand flags"),
carapace.ActionStyledValuesDescribed(
"--chdir", "change work directory", style.Blue,
"--array", "multiflag", style.Blue,
"--help", "help for example", style.Default,
"--persistentFlag", "Help message for persistentFlag", style.Yellow,
"--persistentFlag2", "Help message for persistentFlag2", style.Blue,
"--toggle", "Help message for toggle", style.Default,
"--version", "version for example", style.Default,
).Tag("longhand flags"),
).ToA().NoSpace('.'))

s.Run("--").
Expect(carapace.ActionStyledValuesDescribed(
Expand All @@ -121,27 +125,27 @@ func TestRoot(t *testing.T) {
"--persistentFlag2", "Help message for persistentFlag2", style.Blue,
"--toggle", "Help message for toggle", style.Default,
"--version", "version for example", style.Default,
).NoSpace('.').Tag("flags"))
).NoSpace('.').Tag("longhand flags"))

s.Run("--a").
Expect(carapace.ActionStyledValuesDescribed(
"--array", "multiflag", style.Blue,
).NoSpace('.').Tag("flags"))
).NoSpace('.').Tag("longhand flags"))

s.Run("--array").
Expect(carapace.ActionStyledValuesDescribed(
"--array", "multiflag", style.Blue,
).NoSpace('.').Tag("flags"))
).NoSpace('.').Tag("longhand flags"))

s.Run("--array", "", "--a").
Expect(carapace.ActionStyledValuesDescribed(
"--array", "multiflag", style.Blue,
).NoSpace('.').Tag("flags"))
).NoSpace('.').Tag("longhand flags"))

s.Run("-a", "", "--a").
Expect(carapace.ActionStyledValuesDescribed(
"--array", "multiflag", style.Blue,
).NoSpace('.').Tag("flags"))
).NoSpace('.').Tag("longhand flags"))
})
}

Expand Down
18 changes: 9 additions & 9 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ func actionFlags(cmd *cobra.Command) Action {
isShorthandSeries := flagSet.IsShorthandSeries(c.Value)

nospace := make([]rune, 0)
vals := make([]string, 0)
batch := Batch()
flagSet.VisitAll(func(f *pflagfork.Flag) {
switch {
case f.Hidden && !env.Hidden():
Expand All @@ -103,33 +103,33 @@ func actionFlags(cmd *cobra.Command) Action {
return // abort shorthand flag series if a previous one is not bool or count and requires an argument (no default value)
}
}
vals = append(vals, f.Shorthand, f.Usage, f.Style())
batch = append(batch, ActionStyledValuesDescribed(f.Shorthand, f.Usage, f.Style()).Tag("shorthand flags"))
if f.IsOptarg() {
nospace = append(nospace, []rune(f.Shorthand)[0])
}
}
} else {
switch f.Mode() {
case pflagfork.NameAsShorthand:
vals = append(vals, "-"+f.Name, f.Usage, f.Style())
batch = append(batch, ActionStyledValuesDescribed("-"+f.Name, f.Usage, f.Style()).Tag("longhand flags"))
case pflagfork.Default:
vals = append(vals, "--"+f.Name, f.Usage, f.Style())
batch = append(batch, ActionStyledValuesDescribed("--"+f.Name, f.Usage, f.Style()).Tag("longhand flags"))
}

if f.Shorthand != "" && f.ShorthandDeprecated == "" {
vals = append(vals, "-"+f.Shorthand, f.Usage, f.Style())
batch = append(batch, ActionStyledValuesDescribed("-"+f.Shorthand, f.Usage, f.Style()).Tag("shorthand flags"))
}
}
})

if isShorthandSeries {
if len(nospace) > 0 {
return ActionStyledValuesDescribed(vals...).Prefix(c.Value).NoSpace(nospace...)
return batch.ToA().Prefix(c.Value).NoSpace(nospace...)
}
return ActionStyledValuesDescribed(vals...).Prefix(c.Value)
return batch.ToA().Prefix(c.Value)
}
return ActionStyledValuesDescribed(vals...).MultiParts(".") // multiparts completion for flags grouped with `.`
}).Tag("flags")
return batch.ToA().MultiParts(".") // multiparts completion for flags grouped with `.`
})
}

func initHelpCompletion(cmd *cobra.Command) {
Expand Down
4 changes: 2 additions & 2 deletions pkg/sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,14 @@ func TestPreRun(t *testing.T) {
Expect(carapace.ActionValuesDescribed(
"--root", "root flag").
NoSpace('.').
Tag("flags"))
Tag("longhand flags"))

s.Run("--root", "sub", "--").
Expect(carapace.ActionStyledValuesDescribed(
"--help", "help for sub", style.Default,
"--sub", "sub flag", style.Blue).
NoSpace('.').
Tag("flags"))
Tag("longhand flags"))

s.Run("--root", "sub", "--sub", "").
Expect(carapace.ActionValues("true").
Expand Down

0 comments on commit b0b7d7c

Please sign in to comment.