Skip to content

Commit

Permalink
tmp
Browse files Browse the repository at this point in the history
  • Loading branch information
rsteube committed Sep 26, 2023
1 parent 698cd22 commit 823aa80
Show file tree
Hide file tree
Showing 8 changed files with 73 additions and 11 deletions.
13 changes: 7 additions & 6 deletions compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func registerFlagCompletion(cmd *cobra.Command) {

err := cmd.RegisterFlagCompletionFunc(f.Name, func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
a := storage.getFlag(cmd, f.Name)
action := a.Invoke(Context{Args: args, Value: toComplete})
action := a.Invoke(Context{Args: args, Value: toComplete}) // TODO cmd might differ for persistentflags and either way args or cmd will be wrong
return cobraValuesFor(action), cobraDirectiveFor(action)
})
if err != nil {
Expand Down Expand Up @@ -64,14 +64,15 @@ func cobraDirectiveFor(action InvokedAction) cobra.ShellCompDirective {
return directive
}

func actionCobra(cmd *cobra.Command, f func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)) Action {
func ActionCobra(f func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective)) Action {
return ActionCallback(func(c Context) Action {
if f == nil {
switch {
case f == nil:
return ActionValues()
case c.cmd == nil:
return ActionMessage("TODO: cmd is nil [ActionCobra]") // TODO test
}

c.Args = cmd.Flags().Args() // TODO verify - ensure it contains all args, this might not be correct at all times (e.g. changed Context.Args)
values, directive := f(cmd, c.Args, c.Value)
values, directive := f(c.cmd, c.cmd.Flags().Args(), c.Value)
return compDirective(directive).ToA(values...)
})
}
Expand Down
4 changes: 2 additions & 2 deletions compat_test.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package carapace

import (
"io/ioutil"
"io"
"os"
"strings"
"testing"
Expand Down Expand Up @@ -54,7 +54,7 @@ func TestRegisterFlagCompletion(t *testing.T) {
_ = cmd.Execute()

w.Close()
out, _ := ioutil.ReadAll(r)
out, _ := io.ReadAll(r)
os.Stdout = rescueStdout

if lines := strings.Split(string(out), "\n"); lines[0] != "1\tone" {
Expand Down
2 changes: 2 additions & 0 deletions context.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/rsteube/carapace/pkg/util"
"github.com/rsteube/carapace/third_party/github.com/drone/envsubst"
"github.com/rsteube/carapace/third_party/golang.org/x/sys/execabs"
"github.com/spf13/cobra"
)

// Context provides information during completion.
Expand All @@ -28,6 +29,7 @@ type Context struct {
Dir string

mockedReplies map[string]string
cmd *cobra.Command
}

// NewContext creates a new context for given arguments.
Expand Down
9 changes: 9 additions & 0 deletions example/cmd/compat.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func init() {
compatCmd.Flags().String("default", "", "ShellCompDirectiveDefault")

compatCmd.Flags().String("unset", "", "no completions defined")
compatCmd.PersistentFlags().String("persistent-compat", "", "persistent flag defined with cobra")

rootCmd.AddCommand(compatCmd)

Expand Down Expand Up @@ -61,7 +62,15 @@ func init() {
return nil, cobra.ShellCompDirectiveDefault
})

_ = compatCmd.RegisterFlagCompletionFunc("persistent-compat", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {
return []string{
fmt.Sprintf("args: %#v toComplete: %#v", args, toComplete),
"alternative",
}, cobra.ShellCompDirectiveNoFileComp
})

compatCmd.ValidArgsFunction = func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) {

Check failure on line 72 in example/cmd/compat.go

View workflow job for this annotation

GitHub Actions / lint

SA4009: argument args is overwritten before first use (staticcheck)
args = cmd.Flags().Args()

Check failure on line 73 in example/cmd/compat.go

View workflow job for this annotation

GitHub Actions / lint

SA4009(related information): assignment to args (staticcheck)
switch len(args) {
case 0:
return []string{"p1", "positional1"}, cobra.ShellCompDirectiveDefault
Expand Down
19 changes: 19 additions & 0 deletions example/cmd/compat_sub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package cmd

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

var compat_subCmd = &cobra.Command{
Use: "sub",
Short: "",
Run: func(cmd *cobra.Command, args []string) {},
}

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


compatCmd.AddCommand(compat_subCmd)
}
30 changes: 30 additions & 0 deletions example/cmd/compat_sub_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package cmd

import (
"testing"

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

func TestCompatPersistent(t *testing.T) {
sandbox.Package(t, "github.com/rsteube/carapace/example")(func(s *sandbox.Sandbox) {
s.Run("compat", "sub", "--persistent-compat", "").
Expect(carapace.ActionValues(
`args: []string(nil) toComplete: ""`,
"alternative",
).Usage("persistent flag defined with cobra"))

s.Run("compat", "sub", "one", "--persistent-compat", "").
Expect(carapace.ActionValues(
`args: []string{"one"} toComplete: ""`,
"alternative",
).Usage("persistent flag defined with cobra"))

s.Run("compat", "sub", "one", "two", "--persistent-compat", "a").
Expect(carapace.ActionValues(
`args: []string{"one", "two"} toComplete: "a"`,
"alternative",
).Usage("persistent flag defined with cobra"))
})
}
6 changes: 3 additions & 3 deletions storage.go
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ func (s _storage) getFlag(cmd *cobra.Command, name string) Action {
flagAction, ok := entry.flag[name]
if !ok {
if f, ok := cmd.GetFlagCompletionByName(name); ok {
flagAction = actionCobra(cmd, f)
flagAction = ActionCobra(f)
}
}

Expand Down Expand Up @@ -162,15 +162,15 @@ func (s _storage) getPositional(cmd *cobra.Command, index int) Action {
if entry.positionalAny != nil {
a = *entry.positionalAny
} else {
a = actionCobra(cmd, cmd.ValidArgsFunction)
a = ActionCobra(cmd.ValidArgsFunction)
}
case len(entry.dash) > index:
a = entry.dash[index]
default:
if entry.dashAny != nil {
a = *entry.dashAny
} else {
a = actionCobra(cmd, cmd.ValidArgsFunction)
a = ActionCobra(cmd.ValidArgsFunction)
}
}
a = s.preinvoke(cmd, nil, a)
Expand Down
1 change: 1 addition & 0 deletions traverse.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ func traverse(c *cobra.Command, args []string) (Action, Context) {
fs := pflagfork.FlagSet{FlagSet: c.Flags()}

context := NewContext(args...)
context.cmd = c
loop:
for i, arg := range context.Args {
switch {
Expand Down

0 comments on commit 823aa80

Please sign in to comment.