Repository: rsteube/carapace
Pull request #706 (2023-01-24 02:24:52)

fix persistentflag


Patch:
diff --git a/example/cmd/root.go b/example/cmd/root.go
index b0ce9bc..2619ead 100644
--- a/example/cmd/root.go
+++ b/example/cmd/root.go
@@ -21,12 +21,14 @@ func Execute() error {
 func init() {
 	rootCmd.Flags().BoolP("toggle", "t", false, "Help message for toggle")
 	rootCmd.PersistentFlags().StringP("persistentFlag", "p", "", "Help message for persistentFlag")
+	rootCmd.PersistentFlags().String("persistentFlag2", "", "Help message for persistentFlag2")
 	rootCmd.Flag("persistentFlag").NoOptDefVal = "defaultValue" // no argument required
 
 	rootCmd.Flags().StringArrayP("array", "a", []string{}, "multiflag")
 
 	carapace.Gen(rootCmd).FlagCompletion(carapace.ActionMap{
-		"persistentFlag": carapace.ActionValues("p1", "p2", "p3"),
+		"persistentFlag":  carapace.ActionValues("p1", "p2", "p3"),
+		"persistentFlag2": carapace.ActionValues("p4", "p5", "p6"),
 	})
 
 	rootCmd.AddGroup(
diff --git a/traverse.go b/traverse.go
index 78c1446..7df7428 100644
--- a/traverse.go
+++ b/traverse.go
@@ -42,6 +42,7 @@ func traverse(c *cobra.Command, args []string) (Action, Context) {
 
 	inArgs := []string{} // args consumed by current command
 	var inFlag *InFlag   // last encountered flag that still expects arguments
+	c.LocalFlags()       // TODO force  c.mergePersistentFlags() which is missing from c.Flags()
 	fs := pflagfork.FlagSet{FlagSet: c.Flags()}
 
 	context := NewContext(args...)


Test patch:
diff --git a/example/cmd/action_test.go b/example/cmd/action_test.go
index 7f2f47f..b118434 100644
--- a/example/cmd/action_test.go
+++ b/example/cmd/action_test.go
@@ -175,3 +175,16 @@ func TestUnknownFlag(t *testing.T) {
 				Usage("action [pos1] [pos2] [--] [dashAny]..."))
 	})
 }
+
+func TestPersistentFlag(t *testing.T) {
+	sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
+		s.Run("action", "--persistentFlag=").
+			Expect(carapace.ActionValues("p1", "p2", "p3").
+				Prefix("--persistentFlag=").
+				Usage("Help message for persistentFlag"))
+
+		s.Run("action", "--persistentFlag2", "").
+			Expect(carapace.ActionValues("p4", "p5", "p6").
+				Usage("Help message for persistentFlag2"))
+	})
+}
diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go
index eb8a3fd..8942a6d 100644
--- a/example/cmd/root_test.go
+++ b/example/cmd/root_test.go
@@ -95,6 +95,7 @@ func TestRoot(t *testing.T) {
 				"--array", "multiflag", style.Blue,
 				"-a", "multiflag", style.Blue,
 				"--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,
@@ -104,6 +105,7 @@ func TestRoot(t *testing.T) {
 			Expect(carapace.ActionStyledValuesDescribed(
 				"--array", "multiflag", style.Blue,
 				"--persistentFlag", "Help message for persistentFlag", style.Yellow,
+				"--persistentFlag2", "Help message for persistentFlag2", style.Blue,
 				"--toggle", "Help message for toggle", style.Default,
 			).NoSpace('.').Tag("flags"))