Last active
June 23, 2022 21:59
-
-
Save kangasta/b571122daaf2e71b27849c38165114f3 to your computer and use it in GitHub Desktop.
Cobra shell completion with whitespace
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
package main | |
import ( | |
"fmt" | |
"os" | |
"github.com/spf13/cobra" | |
) | |
var RootCmd = &cobra.Command{ | |
Use: "sh-completion-demo", | |
Long: "Demo shell completion with whitespace", | |
} | |
var ArgWithWhitespace = &cobra.Command{ | |
Use: "arg", | |
Short: "Command with completion that includes whitespace", | |
RunE: func(cmd *cobra.Command, args []string) error { | |
if args[0] != "with whitespace" && args[0] != "no-whitespace" { | |
return fmt.Errorf(`argument must be "with whitespace" or "no-whitespace"`) | |
} | |
return nil | |
}, | |
SilenceUsage: true, | |
ValidArgsFunction: func(cmd *cobra.Command, args []string, toComplete string) ([]string, cobra.ShellCompDirective) { | |
return []string{"with whitespace", "no-whitespace"}, cobra.ShellCompDirectiveNoFileComp | |
}, | |
} | |
var ArgsWithWhitespaceAndDescription = &cobra.Command{ | |
Use: "desc", | |
Short: "Command with completions that includes whitespace and descriptions", | |
RunE: func(cmd *cobra.Command, args []string) error { | |
if args[0] != "a a a" && args[0] != "b b b" { | |
return fmt.Errorf(`argument must be "with whitespace" or "no-whitespace"`) | |
} | |
return nil | |
}, | |
SilenceUsage: true, | |
ValidArgs: []string{"a a a\tDescription for a a a", "b b b\tDescription for b b b"}, | |
} | |
func init() { | |
RootCmd.AddCommand(ArgWithWhitespace) | |
RootCmd.AddCommand(ArgsWithWhitespaceAndDescription) | |
} | |
func main() { | |
if err := RootCmd.Execute(); err != nil { | |
os.Exit(1) | |
} | |
} |
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
module sh-completion-demo | |
go 1.18 | |
require github.com/spf13/cobra v1.5.0 | |
replace github.com/spf13/cobra => github.com/kangasta/cobra v1.5.1-0.20220623214343-ac8216228887 | |
require ( | |
github.com/inconshreveable/mousetrap v1.0.0 // indirect | |
github.com/spf13/pflag v1.0.5 // indirect | |
) |
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
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o= | |
github.com/inconshreveable/mousetrap v1.0.0 h1:Z8tu5sraLXCXIcARxBp/8cbvlwVa7Z1NHg9XEKhtSvM= | |
github.com/inconshreveable/mousetrap v1.0.0/go.mod h1:PxqpIevigyE2G7u3NXJIT2ANytuPF1OarO4DADm73n8= | |
github.com/kangasta/cobra v1.5.1-0.20220623214343-ac8216228887 h1:JB+TsFCi5e3YCYSKJFw2LYtWINC757ba+rXVfGGguVI= | |
github.com/kangasta/cobra v1.5.1-0.20220623214343-ac8216228887/go.mod h1:dWXEIy2H428czQCjInthrTRUg7yKbok+2Qi/yBIJoUM= | |
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM= | |
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA= | |
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg= | |
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= | |
gopkg.in/yaml.v2 v2.4.0/go.mod h1:RDklbk79AGWmwhnvt/jBztapEOGDOx6ZbXqjP6csGnQ= |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment