Skip to content

Commit

Permalink
Merge pull request #769 from rsteube/default-flags
Browse files Browse the repository at this point in the history
actionFlags: init default flags
  • Loading branch information
rsteube authored Apr 27, 2023
2 parents 1252671 + a985c8f commit ca755b3
Show file tree
Hide file tree
Showing 7 changed files with 20 additions and 6 deletions.
2 changes: 1 addition & 1 deletion carapace_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ func TestComplete(t *testing.T) {
cmd.Flags().BoolP("a", "1", false, "")
cmd.Flags().BoolP("b", "2", false, "")

if s, err := complete(cmd, []string{"elvish", "_", "test", "-1"}); err != nil || s != `{"Usage":"","Messages":[],"DescriptionStyle":"dim white","Candidates":[{"Value":"-12","Display":"2","Description":"","CodeSuffix":"","Style":"default"}]}` {
if s, err := complete(cmd, []string{"elvish", "_", "test", "-1"}); err != nil || s != `{"Usage":"","Messages":[],"DescriptionStyle":"dim white","Candidates":[{"Value":"-12","Display":"2","Description":"","CodeSuffix":"","Style":"default"},{"Value":"-1h","Display":"h","Description":"help for test","CodeSuffix":"","Style":"default"}]}` {
t.Error(s)
}
}
Expand Down
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", "").Tag("flags").NoSpace().Invoke(Context{}).Prefix("-a"), a)
assertEqual(t, ActionValuesDescribed("b", "", "h", "help for this command").Tag("flags").NoSpace().Invoke(Context{}).Prefix("-a"), a)
}

func TestActionExecCommandEnv(t *testing.T) {
Expand Down
7 changes: 5 additions & 2 deletions example/cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,9 @@ func TestDash(t *testing.T) {
Expect(carapace.ActionStyledValuesDescribed(
"--embedded-bool", "embedded bool flag", style.Default,
"--embedded-optarg", "embedded optarg flag", style.Yellow,
"--embedded-string", "embedded string flag", style.Blue).
"--embedded-string", "embedded string flag", style.Blue,
"-h", "help for embedded", style.Default,
"--help", "help for embedded", style.Default).
NoSpace('.').
Usage("action [pos1] [pos2] [--] [dashAny]...").
Tag("flags"))
Expand All @@ -183,7 +185,8 @@ func TestDash(t *testing.T) {
Expect(carapace.ActionStyledValuesDescribed(
"--embedded-bool", "embedded bool flag", style.Default,
"--embedded-optarg", "embedded optarg flag", style.Yellow,
"--embedded-string", "embedded string flag", style.Blue).
"--embedded-string", "embedded string flag", style.Blue,
"--help", "help for embedded", style.Default).
NoSpace('.').
Usage("action [pos1] [pos2] [--] [dashAny]...").
Tag("flags"))
Expand Down
5 changes: 3 additions & 2 deletions example/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ import (
)

var rootCmd = &cobra.Command{
Use: "example",
Short: "example completion",
Use: "example",
Short: "example completion",
Version: "example",
}

// Execute executes cmd.
Expand Down
6 changes: 6 additions & 0 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,19 +94,25 @@ func TestRoot(t *testing.T) {
Expect(carapace.ActionStyledValuesDescribed(
"--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"))

s.Run("--").
Expect(carapace.ActionStyledValuesDescribed(
"--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,
).NoSpace('.').Tag("flags"))

s.Run("--a").
Expand Down
3 changes: 3 additions & 0 deletions internalActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,9 @@ func actionPath(fileSuffixes []string, dirOnly bool) Action {

func actionFlags(cmd *cobra.Command) Action {
return ActionCallback(func(c Context) Action {
cmd.InitDefaultHelpFlag()
cmd.InitDefaultVersionFlag()

flagSet := pflagfork.FlagSet{FlagSet: cmd.Flags()}
isShorthandSeries := flagSet.IsShorthandSeries(c.Value)

Expand Down
1 change: 1 addition & 0 deletions pkg/sandbox/sandbox_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ func TestPreRun(t *testing.T) {

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

0 comments on commit ca755b3

Please sign in to comment.