mirror of
https://github.com/spf13/cobra
synced 2024-11-04 21:07:19 +00:00
Compare commits
1 commit
80334cad87
...
17854aa32f
Author | SHA1 | Date | |
---|---|---|---|
|
17854aa32f |
3 changed files with 7 additions and 13 deletions
2
go.mod
2
go.mod
|
@ -3,7 +3,7 @@ module github.com/spf13/cobra
|
||||||
go 1.15
|
go 1.15
|
||||||
|
|
||||||
require (
|
require (
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.4
|
github.com/cpuguy83/go-md2man/v2 v2.0.3
|
||||||
github.com/inconshreveable/mousetrap v1.1.0
|
github.com/inconshreveable/mousetrap v1.1.0
|
||||||
github.com/spf13/pflag v1.0.5
|
github.com/spf13/pflag v1.0.5
|
||||||
gopkg.in/yaml.v3 v3.0.1
|
gopkg.in/yaml.v3 v3.0.1
|
||||||
|
|
4
go.sum
4
go.sum
|
@ -1,5 +1,5 @@
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.4 h1:wfIWP927BUkWJb2NmU/kNDYIBTh/ziUX91+lVfRxZq4=
|
github.com/cpuguy83/go-md2man/v2 v2.0.3 h1:qMCsGGgs+MAzDFyp9LpAe1Lqy/fY/qCovCm0qnXZOBM=
|
||||||
github.com/cpuguy83/go-md2man/v2 v2.0.4/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
github.com/cpuguy83/go-md2man/v2 v2.0.3/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
|
||||||
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
github.com/inconshreveable/mousetrap v1.1.0/go.mod h1:vpF70FUmC8bwa3OWnCshd2FqLfsEA9PFc4w1p2J65bw=
|
||||||
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
github.com/russross/blackfriday/v2 v2.1.0 h1:JIOH55/0cWyOuilr9/qlrm0BSXldqnqwMsf35Ld67mk=
|
||||||
|
|
|
@ -3,7 +3,7 @@
|
||||||
While you are welcome to provide your own organization, typically a Cobra-based
|
While you are welcome to provide your own organization, typically a Cobra-based
|
||||||
application will follow the following organizational structure:
|
application will follow the following organizational structure:
|
||||||
|
|
||||||
```test
|
```
|
||||||
▾ appName/
|
▾ appName/
|
||||||
▾ cmd/
|
▾ cmd/
|
||||||
add.go
|
add.go
|
||||||
|
@ -301,7 +301,6 @@ command := cobra.Command{
|
||||||
### Bind Flags with Config
|
### Bind Flags with Config
|
||||||
|
|
||||||
You can also bind your flags with [viper](https://github.com/spf13/viper):
|
You can also bind your flags with [viper](https://github.com/spf13/viper):
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var author string
|
var author string
|
||||||
|
|
||||||
|
@ -321,14 +320,12 @@ More in [viper documentation](https://github.com/spf13/viper#working-with-flags)
|
||||||
|
|
||||||
Flags are optional by default. If instead you wish your command to report an error
|
Flags are optional by default. If instead you wish your command to report an error
|
||||||
when a flag has not been set, mark it as required:
|
when a flag has not been set, mark it as required:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
|
rootCmd.Flags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
|
||||||
rootCmd.MarkFlagRequired("region")
|
rootCmd.MarkFlagRequired("region")
|
||||||
```
|
```
|
||||||
|
|
||||||
Or, for persistent flags:
|
Or, for persistent flags:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
rootCmd.PersistentFlags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
|
rootCmd.PersistentFlags().StringVarP(&Region, "region", "r", "", "AWS region (required)")
|
||||||
rootCmd.MarkPersistentFlagRequired("region")
|
rootCmd.MarkPersistentFlagRequired("region")
|
||||||
|
@ -338,7 +335,6 @@ rootCmd.MarkPersistentFlagRequired("region")
|
||||||
|
|
||||||
If you have different flags that must be provided together (e.g. if they provide the `--username` flag they MUST provide the `--password` flag as well) then
|
If you have different flags that must be provided together (e.g. if they provide the `--username` flag they MUST provide the `--password` flag as well) then
|
||||||
Cobra can enforce that requirement:
|
Cobra can enforce that requirement:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)")
|
rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)")
|
||||||
rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)")
|
rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)")
|
||||||
|
@ -347,7 +343,6 @@ rootCmd.MarkFlagsRequiredTogether("username", "password")
|
||||||
|
|
||||||
You can also prevent different flags from being provided together if they represent mutually
|
You can also prevent different flags from being provided together if they represent mutually
|
||||||
exclusive options such as specifying an output format as either `--json` or `--yaml` but never both:
|
exclusive options such as specifying an output format as either `--json` or `--yaml` but never both:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")
|
rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")
|
||||||
rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")
|
rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")
|
||||||
|
@ -356,7 +351,6 @@ rootCmd.MarkFlagsMutuallyExclusive("json", "yaml")
|
||||||
|
|
||||||
If you want to require at least one flag from a group to be present, you can use `MarkFlagsOneRequired`.
|
If you want to require at least one flag from a group to be present, you can use `MarkFlagsOneRequired`.
|
||||||
This can be combined with `MarkFlagsMutuallyExclusive` to enforce exactly one flag from a given group:
|
This can be combined with `MarkFlagsMutuallyExclusive` to enforce exactly one flag from a given group:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")
|
rootCmd.Flags().BoolVar(&ofJson, "json", false, "Output in JSON")
|
||||||
rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")
|
rootCmd.Flags().BoolVar(&ofYaml, "yaml", false, "Output in YAML")
|
||||||
|
@ -434,7 +428,7 @@ by not providing a 'Run' for the 'rootCmd'.
|
||||||
|
|
||||||
We have only defined one flag for a single command.
|
We have only defined one flag for a single command.
|
||||||
|
|
||||||
More documentation about flags is available at https://github.com/spf13/pflag.
|
More documentation about flags is available at https://github.com/spf13/pflag
|
||||||
|
|
||||||
```go
|
```go
|
||||||
package main
|
package main
|
||||||
|
@ -728,7 +722,7 @@ command.SuggestionsMinimumDistance = 1
|
||||||
You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but make sense in your set of commands but for which
|
You can also explicitly set names for which a given command will be suggested using the `SuggestFor` attribute. This allows suggestions for strings that are not close in terms of string distance, but make sense in your set of commands but for which
|
||||||
you don't want aliases. Example:
|
you don't want aliases. Example:
|
||||||
|
|
||||||
```bash
|
```
|
||||||
$ kubectl remove
|
$ kubectl remove
|
||||||
Error: unknown command "remove" for "kubectl"
|
Error: unknown command "remove" for "kubectl"
|
||||||
|
|
||||||
|
@ -793,7 +787,7 @@ func main() {
|
||||||
|
|
||||||
Example run as a kubectl plugin:
|
Example run as a kubectl plugin:
|
||||||
|
|
||||||
```bash
|
```
|
||||||
$ kubectl myplugin
|
$ kubectl myplugin
|
||||||
Usage:
|
Usage:
|
||||||
kubectl myplugin [command]
|
kubectl myplugin [command]
|
||||||
|
|
Loading…
Reference in a new issue