Skip to content

Commit

Permalink
traverse: fix partial optarg completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jul 22, 2023
1 parent 5d03e18 commit a1fa2bb
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
29 changes: 28 additions & 1 deletion example/cmd/root_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,38 @@ func TestRoot(t *testing.T) {
Expect(carapace.ActionStyledValuesDescribed(
"--array", "multiflag", style.Blue,
).NoSpace('.').Tag("flags"))
})
}

func TestOptarg(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Run("--persistentFlag=").
Expect(carapace.ActionValues(
"p1",
"p2",
"p3",
).Prefix("--persistentFlag=").
Usage("Help message for persistentFlag"))

s.Run("--persistentFlag=p").
Expect(carapace.ActionValues(
"p1",
"p2",
"p3",
).Prefix("--persistentFlag=").
Usage("Help message for persistentFlag"))

s.Run("--toggle=").
Expect(carapace.ActionStyledValues(
"true", style.Green,
"false", style.Red,
).Prefix("--toggle=").
Usage("Help message for toggle"))

s.Run("--toggle=tru").
Expect(carapace.ActionStyledValues(
"true", style.Green,
).Prefix("--toggle="))
).Prefix("--toggle=").
Usage("Help message for toggle"))
})
}
5 changes: 2 additions & 3 deletions traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,11 @@ loop:
case !c.DisableFlagParsing && strings.HasPrefix(context.Value, "-") && (fs.IsInterspersed() || len(inPositionals) == 0):
if f := fs.LookupArg(context.Value); f != nil && f.IsOptarg() && strings.Contains(context.Value, string(f.OptargDelimiter())) {
LOG.Printf("completing optional flag argument for arg %#v\n", context.Value)
prefix, optarg := f.Split(context.Value)
context.Value = optarg
prefix, _ := f.Split(context.Value)

switch f.Value.Type() {
case "bool":
return ActionValues("true", "false").StyleF(style.ForKeyword).Prefix(prefix), context
return ActionValues("true", "false").StyleF(style.ForKeyword).Usage(f.Usage).Prefix(prefix), context
default:
return storage.getFlag(c, f.Name).Prefix(prefix), context
}
Expand Down

0 comments on commit a1fa2bb

Please sign in to comment.