Skip to content

Commit

Permalink
traverse: complete values attached to flags for non-optarg as well
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Jul 22, 2023
1 parent 1fad45e commit f4c0933
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 5 deletions.
20 changes: 20 additions & 0 deletions example/cmd/action_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,26 @@ func TestAction(t *testing.T) {
Expect(carapace.ActionMessage("values flag is not set").
Usage("ActionCallback()"))

s.Run("action", "--values=").
Expect(carapace.ActionValues(
"first",
"second",
"third",
).Prefix("--values=").
Usage("ActionValues()"))

s.Run("action", "--values=f").
Expect(carapace.ActionValues(
"first",
).Prefix("--values=").
Usage("ActionValues()"))

s.Run("action", "--values=first", "").
Expect(carapace.ActionValues(
"embeddedP1",
"embeddedPositional1",
).Usage("action [pos1] [pos2] [--] [dashAny]..."))

s.Run("action", "--values", "first", "--callback", "").
Expect(carapace.ActionMessage("values flag is set to: 'first'").
Usage("ActionCallback()"))
Expand Down
5 changes: 2 additions & 3 deletions internal/pflagfork/flag.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,10 +53,9 @@ func (f Flag) IsRepeatable() bool {
return false
}

func (f Flag) Split(arg string) (prefix, optarg string) {
func (f Flag) Split(arg string) []string {
delimiter := string(f.OptargDelimiter())
splitted := strings.SplitN(arg, delimiter, 2)
return splitted[0] + delimiter, splitted[1]
return strings.SplitAfterN(arg, delimiter, 2)
}

func (f Flag) Matches(arg string, posix bool) bool {
Expand Down
6 changes: 4 additions & 2 deletions traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,8 @@ loop:

if inFlag.Flag == nil {
LOG.Printf("flag %#v is unknown", arg)
} else if splitted := inFlag.Flag.Split(arg); len(splitted) > 1 {
inFlag.Args = append(inFlag.Args, splitted[1])
}
continue

Expand Down Expand Up @@ -160,9 +162,9 @@ loop:

// flag
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())) {
if f := fs.LookupArg(context.Value); f != nil && strings.Contains(context.Value, string(f.OptargDelimiter())) {
LOG.Printf("completing optional flag argument for arg %#v\n", context.Value)
prefix, _ := f.Split(context.Value)
prefix := f.Split(context.Value)[0]

switch f.Value.Type() {
case "bool":
Expand Down

0 comments on commit f4c0933

Please sign in to comment.