Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

actionFlags: split tag into shorthand and longhand #1025

Merged
merged 1 commit into from
Jul 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion defaultActions_test.go
Original file line number Diff line number Diff line change
@@ -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) {
32 changes: 20 additions & 12 deletions example-nonposix/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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())
@@ -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('.'))
})
}

@@ -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").
@@ -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
@@ -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(
@@ -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").
10 changes: 5 additions & 5 deletions example/cmd/chain_test.go
Original file line number Diff line number Diff line change
@@ -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(
@@ -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(
@@ -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(
@@ -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
@@ -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(
4 changes: 2 additions & 2 deletions example/cmd/modifier_test.go
Original file line number Diff line number Diff line change
@@ -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(
@@ -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>").
44 changes: 24 additions & 20 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
@@ -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(
@@ -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"))
})
}

18 changes: 9 additions & 9 deletions internalActions.go
Original file line number Diff line number Diff line change
@@ -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():
@@ -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) {
4 changes: 2 additions & 2 deletions pkg/sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
@@ -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").