Repository: rsteube/carapace
Pull request #685 (2023-01-14 19:15:18)

flag: highlight styles


Patch:
diff --git a/internal/pflagfork/flag.go b/internal/pflagfork/flag.go
index a02b903..e883962 100644
--- a/internal/pflagfork/flag.go
+++ b/internal/pflagfork/flag.go
@@ -35,3 +35,16 @@ func (f Flag) IsRepeatable() bool {
 	}
 	return false
 }
+
+func (f Flag) TakesValue() bool {
+	switch f.Value.Type() {
+	case "bool", "boolSlice", "count":
+		return false
+	default:
+		return true
+	}
+}
+
+func (f Flag) IsOptarg() bool {
+	return f.NoOptDefVal != ""
+}
diff --git a/internalActions.go b/internalActions.go
index c455e77..f92065d 100644
--- a/internalActions.go
+++ b/internalActions.go
@@ -89,6 +89,15 @@ func actionFlags(cmd *cobra.Command) Action {
 				return // skip flag of group already set
 			}
 
+			s := style.Carapace.FlagNoArg
+			if f.TakesValue() {
+				if f.IsOptarg() {
+					s = style.Carapace.FlagOptArg
+				} else {
+					s = style.Carapace.FlagArg
+				}
+			}
+
 			if isShorthandSeries {
 				if f.Shorthand != "" && f.ShorthandDeprecated == "" {
 					for _, shorthand := range c.CallbackValue[1:] {
@@ -96,26 +105,26 @@ 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)
+					vals = append(vals, f.Shorthand, f.Usage, s)
 				}
 			} else {
 				if flagstyle := f.Style(); flagstyle != pflagfork.ShorthandOnly {
 					if flagstyle == pflagfork.NameAsShorthand {
-						vals = append(vals, "-"+f.Name, f.Usage)
+						vals = append(vals, "-"+f.Name, f.Usage, s)
 					} else {
-						vals = append(vals, "--"+f.Name, f.Usage)
+						vals = append(vals, "--"+f.Name, f.Usage, s)
 					}
 				}
 				if f.Shorthand != "" && f.ShorthandDeprecated == "" {
-					vals = append(vals, "-"+f.Shorthand, f.Usage)
+					vals = append(vals, "-"+f.Shorthand, f.Usage, s)
 				}
 			}
 		})
 
 		if isShorthandSeries {
-			return ActionValuesDescribed(vals...).Invoke(c).Prefix(c.CallbackValue).ToA().NoSpace('*')
+			return ActionStyledValuesDescribed(vals...).Invoke(c).Prefix(c.CallbackValue).ToA().NoSpace('*')
 		}
-		return ActionValuesDescribed(vals...).Invoke(c).ToMultiPartsA(".") // multiparts completion for flags grouped with `.`
+		return ActionStyledValuesDescribed(vals...).Invoke(c).ToMultiPartsA(".") // multiparts completion for flags grouped with `.`
 	}).Tag("flags")
 }
 
diff --git a/pkg/style/config.go b/pkg/style/config.go
index 496c5c1..947c57b 100644
--- a/pkg/style/config.go
+++ b/pkg/style/config.go
@@ -56,6 +56,10 @@ type carapace struct {
 
 	Highlight11 string `desc:"Highlight 11" tag:"highlight styles"`
 	Highlight12 string `desc:"Highlight 12" tag:"highlight styles"`
+
+	FlagArg    string `desc:"flag with argument" tag:"flag styles"`
+	FlagNoArg  string `desc:"flag without argument" tag:"flag styles"`
+	FlagOptArg string `desc:"flag with optional argument" tag:"flag styles"`
 }
 
 var Carapace = carapace{
@@ -90,6 +94,10 @@ var Carapace = carapace{
 
 	Highlight11: Bold,
 	Highlight12: Of(Bold, Dim),
+
+	FlagArg:    Blue,
+	FlagNoArg:  Default,
+	FlagOptArg: Yellow,
 }
 
 // Highlight returns the style for given level (0..n)


Test patch:
diff --git a/example/cmd/root_test.go b/example/cmd/root_test.go
index a78f2e2..241459f 100644
--- a/example/cmd/root_test.go
+++ b/example/cmd/root_test.go
@@ -91,40 +91,40 @@ func TestRoot(t *testing.T) {
 			).Tag("main commands"))
 
 		s.Run("-").
-			Expect(carapace.ActionValuesDescribed(
-				"--array", "multiflag",
-				"-a", "multiflag",
-				"--persistentFlag", "Help message for persistentFlag",
-				"-p", "Help message for persistentFlag",
-				"--toggle", "Help message for toggle",
-				"-t", "Help message for toggle",
+			Expect(carapace.ActionStyledValuesDescribed(
+				"--array", "multiflag", style.Blue,
+				"-a", "multiflag", style.Blue,
+				"--persistentFlag", "Help message for persistentFlag", style.Yellow,
+				"-p", "Help message for persistentFlag", style.Yellow,
+				"--toggle", "Help message for toggle", style.Default,
+				"-t", "Help message for toggle", style.Default,
 			).NoSpace('.').Tag("flags"))
 
 		s.Run("--").
-			Expect(carapace.ActionValuesDescribed(
-				"--array", "multiflag",
-				"--persistentFlag", "Help message for persistentFlag",
-				"--toggle", "Help message for toggle",
+			Expect(carapace.ActionStyledValuesDescribed(
+				"--array", "multiflag", style.Blue,
+				"--persistentFlag", "Help message for persistentFlag", style.Yellow,
+				"--toggle", "Help message for toggle", style.Default,
 			).NoSpace('.').Tag("flags"))
 
 		s.Run("--a").
-			Expect(carapace.ActionValuesDescribed(
-				"--array", "multiflag",
+			Expect(carapace.ActionStyledValuesDescribed(
+				"--array", "multiflag", style.Blue,
 			).NoSpace('.').Tag("flags"))
 
 		s.Run("--array").
-			Expect(carapace.ActionValuesDescribed(
-				"--array", "multiflag",
+			Expect(carapace.ActionStyledValuesDescribed(
+				"--array", "multiflag", style.Blue,
 			).NoSpace('.').Tag("flags"))
 
 		s.Run("--array", "", "--a").
-			Expect(carapace.ActionValuesDescribed(
-				"--array", "multiflag",
+			Expect(carapace.ActionStyledValuesDescribed(
+				"--array", "multiflag", style.Blue,
 			).NoSpace('.').Tag("flags"))
 
 		s.Run("-a", "", "--a").
-			Expect(carapace.ActionValuesDescribed(
-				"--array", "multiflag",
+			Expect(carapace.ActionStyledValuesDescribed(
+				"--array", "multiflag", style.Blue,
 			).NoSpace('.').Tag("flags"))
 	})
 }