Skip to content

Commit

Permalink
FlagParsingDisabled: fix positional completion
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Dec 19, 2023
1 parent 1ac5077 commit 12b62d4
Show file tree
Hide file tree
Showing 3 changed files with 55 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/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 disabledCmd = &cobra.Command{
Use: "disabled",
Short: "",
DisableFlagParsing: true,
Run: func(cmd *cobra.Command, args []string) {},
}

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

rootCmd.AddCommand(disabledCmd)

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

import (
"testing"

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

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

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

0 comments on commit 12b62d4

Please sign in to comment.