Skip to content

Commit

Permalink
Merge pull request #963 from rsteube/fix-flagparsing-disabled
Browse files Browse the repository at this point in the history
FlagParsingDisabled: fix positional completion
  • Loading branch information
rsteube authored Dec 19, 2023
2 parents 1ac5077 + 6709ffd commit 2c373be
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 1 deletion.
6 changes: 5 additions & 1 deletion defaultActions.go
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,11 @@ func ActionCobra(f func(cmd *cobra.Command, args []string, toComplete string) ([
LOG.Print("cmd is nil [ActionCobra]")
c.cmd = &cobra.Command{Use: "_carapace_actioncobra", Hidden: true, Deprecated: "dummy command for ActionCobra"}
}
values, directive := f(c.cmd, c.cmd.Flags().Args(), c.Value)

if !c.cmd.DisableFlagParsing {
c.Args = c.cmd.Flags().Args()
}
values, directive := f(c.cmd, c.Args, c.Value)
return compDirective(directive).ToA(values...)
})
}
24 changes: 24 additions & 0 deletions example/cmd/flag_disabled.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package cmd

import (
"github.com/rsteube/carapace"
"github.com/spf13/cobra"
)

var flag_disabledCmd = &cobra.Command{
Use: "disabled",
Short: "flag parsing disabled",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {},
}

func init() {
carapace.Gen(flag_disabledCmd).Standalone()

flagCmd.AddCommand(flag_disabledCmd)

carapace.Gen(flag_disabledCmd).PositionalCompletion(
carapace.ActionValues("-p1", "positional1"),
carapace.ActionValues("p2", "--positional2"),
)
}
27 changes: 27 additions & 0 deletions example/cmd/flag_disabled_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package cmd

import (
"testing"

"github.com/rsteube/carapace"
"github.com/rsteube/carapace/pkg/sandbox"
)

func TestFlagDisabled(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Run("flag", "disabled", "").
Expect(carapace.ActionValues(
"-p1",
"positional1",
))

s.Run("flag", "disabled", "-p1", "").
Expect(carapace.ActionValues(
"p2",
"--positional2",
))

s.Run("flag", "disabled", "-p1", "p2", "").
Expect(carapace.ActionValues())
})
}

0 comments on commit 2c373be

Please sign in to comment.