-
-
Notifications
You must be signed in to change notification settings - Fork 11
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
14 changed files
with
402 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
package cmd | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/rsteube/carapace" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var compatCmd = &cobra.Command{ | ||
Use: "compat", | ||
Short: "", | ||
Run: func(cmd *cobra.Command, args []string) {}, | ||
} | ||
|
||
func init() { | ||
carapace.Gen(compatCmd).Standalone() | ||
|
||
compatCmd.Flags() | ||
|
||
compatCmd.Flags().String("error", "", "ShellCompDirectiveError") | ||
compatCmd.Flags().String("nospace", "", "ShellCompDirectiveNoSpace") | ||
compatCmd.Flags().String("nofilecomp", "", "ShellCompDirectiveNoFileComp") | ||
compatCmd.Flags().String("filterfileext", "", "ShellCompDirectiveFilterFileExt") | ||
compatCmd.Flags().String("filterdirs", "", "ShellCompDirectiveFilterDirs") | ||
compatCmd.Flags().String("filterdirs-chdir", "", "ShellCompDirectiveFilterDirs") | ||
compatCmd.Flags().String("keeporder", "", "ShellCompDirectiveKeepOrder") | ||
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) | ||
|
||
_ = compatCmd.RegisterFlagCompletionFunc("error", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return nil, cobra.ShellCompDirectiveError | ||
}) | ||
_ = compatCmd.RegisterFlagCompletionFunc("nospace", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"one", "two"}, cobra.ShellCompDirectiveNoSpace | ||
}) | ||
_ = compatCmd.RegisterFlagCompletionFunc("nofilecomp", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
}) | ||
|
||
_ = compatCmd.RegisterFlagCompletionFunc("filterfileext", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"mod", "sum"}, cobra.ShellCompDirectiveFilterFileExt | ||
}) | ||
|
||
_ = compatCmd.RegisterFlagCompletionFunc("filterdirs", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return nil, cobra.ShellCompDirectiveFilterDirs | ||
}) | ||
|
||
_ = compatCmd.RegisterFlagCompletionFunc("filterdirs-chdir", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"subdir"}, cobra.ShellCompDirectiveFilterDirs | ||
}) | ||
|
||
_ = compatCmd.RegisterFlagCompletionFunc("keeporder", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
return []string{"one", "three", "two"}, cobra.ShellCompDirectiveKeepOrder | ||
}) | ||
|
||
_ = compatCmd.RegisterFlagCompletionFunc("default", func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | ||
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) { | ||
args = cmd.Flags().Args() | ||
switch len(args) { | ||
case 0: | ||
return []string{"p1", "positional1"}, cobra.ShellCompDirectiveDefault | ||
case 1: | ||
return nil, cobra.ShellCompDirectiveDefault | ||
case 2: | ||
return []string{ | ||
fmt.Sprintf("args: %#v toComplete: %#v", args, toComplete), | ||
"alternative", | ||
}, cobra.ShellCompDirectiveNoFileComp | ||
default: | ||
return nil, cobra.ShellCompDirectiveNoFileComp | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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")) | ||
}) | ||
} |
Oops, something went wrong.