Repository: rsteube/carapace
Pull request #769 (2023-04-27 07:44:19)

actionFlags: init default flags


Patch:
diff --git a/example/cmd/root.go b/example/cmd/root.go
index ba8927f..b6a0759 100644
--- a/example/cmd/root.go
+++ b/example/cmd/root.go
@@ -6,8 +6,9 @@ import (
 )
 
 var rootCmd = &cobra.Command{
-	Use:   "example",
-	Short: "example completion",
+	Use:     "example",
+	Short:   "example completion",
+	Version: "example",
 }
 
 // Execute executes cmd.
diff --git a/internalActions.go b/internalActions.go
index 84897db..322d10f 100644
--- a/internalActions.go
+++ b/internalActions.go
@@ -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)
 


Test patch:
diff --git a/carapace_test.go b/carapace_test.go
index de3e567..c43fae6 100644
--- a/carapace_test.go
+++ b/carapace_test.go
@@ -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)
 	}
 }
diff --git a/defaultActions_test.go b/defaultActions_test.go
index 1e75d65..14fc8fd 100644
--- a/defaultActions_test.go
+++ b/defaultActions_test.go
@@ -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) {
diff --git a/example/cmd/action_test.go b/example/cmd/action_test.go
index 7b64707..c15f617 100644
--- a/example/cmd/action_test.go
+++ b/example/cmd/action_test.go
@@ -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"))
@@ -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"))
diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go
index a94f42b..19bb1de 100644
--- a/example/cmd/root_test.go
+++ b/example/cmd/root_test.go
@@ -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").
diff --git a/pkg/sandbox/sandbox_test.go b/pkg/sandbox/sandbox_test.go
index bcd9f67..7f2a5cf 100644
--- a/pkg/sandbox/sandbox_test.go
+++ b/pkg/sandbox/sandbox_test.go
@@ -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"))