Skip to content

Commit

Permalink
Merge pull request #706 from rsteube/fix-persistentflag
Browse files Browse the repository at this point in the history
fix persistentflag
  • Loading branch information
rsteube authored Jan 24, 2023
2 parents 6a0f632 + da6b2a0 commit 0031719
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 1 deletion.
13 changes: 13 additions & 0 deletions example/cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"))
})
}
4 changes: 3 additions & 1 deletion example/cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
2 changes: 2 additions & 0 deletions example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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"))

Expand Down
1 change: 1 addition & 0 deletions traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -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...)
Expand Down

0 comments on commit 0031719

Please sign in to comment.