300 rootCmd.MarkPersistentFlagRequired("region") |
300 rootCmd.MarkPersistentFlagRequired("region") |
301 ``` |
301 ``` |
302 |
302 |
303 ### Flag Groups |
303 ### Flag Groups |
304 |
304 |
305 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 |
305 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 |
306 Cobra can enforce that requirement: |
306 Cobra can enforce that requirement: |
307 ```go |
307 ```go |
308 rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)") |
308 rootCmd.Flags().StringVarP(&u, "username", "u", "", "Username (required if password is set)") |
309 rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)") |
309 rootCmd.Flags().StringVarP(&pw, "password", "p", "", "Password (required if username is set)") |
310 rootCmd.MarkFlagsRequiredTogether("username", "password") |
310 rootCmd.MarkFlagsRequiredTogether("username", "password") |
311 ``` |
311 ``` |
312 |
312 |
313 You can also prevent different flags from being provided together if they represent mutually |
313 You can also prevent different flags from being provided together if they represent mutually |
314 exclusive options such as specifying an output format as either `--json` or `--yaml` but never both: |
314 exclusive options such as specifying an output format as either `--json` or `--yaml` but never both: |
315 ```go |
315 ```go |
316 rootCmd.Flags().BoolVar(&u, "json", false, "Output in JSON") |
316 rootCmd.Flags().BoolVar(&u, "json", false, "Output in JSON") |
317 rootCmd.Flags().BoolVar(&pw, "yaml", false, "Output in YAML") |
317 rootCmd.Flags().BoolVar(&pw, "yaml", false, "Output in YAML") |
318 rootCmd.MarkFlagsMutuallyExclusive("json", "yaml") |
318 rootCmd.MarkFlagsMutuallyExclusive("json", "yaml") |
325 - a group may contain any number of flags |
325 - a group may contain any number of flags |
326 |
326 |
327 ## Positional and Custom Arguments |
327 ## Positional and Custom Arguments |
328 |
328 |
329 Validation of positional arguments can be specified using the `Args` field of `Command`. |
329 Validation of positional arguments can be specified using the `Args` field of `Command`. |
|
330 The following validators are built in: |
|
331 |
|
332 - Number of arguments: |
|
333 - `NoArgs` - report an error if there are any positional args. |
|
334 - `ArbitraryArgs` - accept any number of args. |
|
335 - `MinimumNArgs(int)` - report an error if less than N positional args are provided. |
|
336 - `MaximumNArgs(int)` - report an error if more than N positional args are provided. |
|
337 - `ExactArgs(int)` - report an error if there are not exactly N positional args. |
|
338 - `RangeArgs(min, max)` - report an error if the number of args is not between `min` and `max`. |
|
339 - Content of the arguments: |
|
340 - `OnlyValidArgs` - report an error if there are any positional args not specified in the `ValidArgs` field of `Command`, which can optionally be set to a list of valid values for positional args. |
|
341 |
330 If `Args` is undefined or `nil`, it defaults to `ArbitraryArgs`. |
342 If `Args` is undefined or `nil`, it defaults to `ArbitraryArgs`. |
331 |
343 |
332 The following validators are built in: |
344 Moreover, `MatchAll(pargs ...PositionalArgs)` enables combining existing checks with arbitrary other checks. |
333 |
345 For instance, if you want to report an error if there are not exactly N positional args OR if there are any positional |
334 - `NoArgs` - the command will report an error if there are any positional args. |
346 args that are not in the `ValidArgs` field of `Command`, you can call `MatchAll` on `ExactArgs` and `OnlyValidArgs`, as |
335 - `ArbitraryArgs` - the command will accept any args. |
347 shown below: |
336 - `OnlyValidArgs` - the command will report an error if there are any positional args that are not in the `ValidArgs` field of `Command`. |
348 |
337 - `MinimumNArgs(int)` - the command will report an error if there are not at least N positional args. |
349 ```go |
338 - `MaximumNArgs(int)` - the command will report an error if there are more than N positional args. |
350 var cmd = &cobra.Command{ |
339 - `ExactArgs(int)` - the command will report an error if there are not exactly N positional args. |
351 Short: "hello", |
340 - `ExactValidArgs(int)` - the command will report an error if there are not exactly N positional args OR if there are any positional args that are not in the `ValidArgs` field of `Command` |
352 Args: MatchAll(ExactArgs(2), OnlyValidArgs), |
341 - `RangeArgs(min, max)` - the command will report an error if the number of args is not between the minimum and maximum number of expected args. |
353 Run: func(cmd *cobra.Command, args []string) { |
342 - `MatchAll(pargs ...PositionalArgs)` - enables combining existing checks with arbitrary other checks (e.g. you want to check the ExactArgs length along with other qualities). |
354 fmt.Println("Hello, World!") |
343 |
355 }, |
344 An example of setting the custom validator: |
356 } |
|
357 ``` |
|
358 |
|
359 It is possible to set any custom validator that satisfies `func(cmd *cobra.Command, args []string) error`. |
|
360 For example: |
345 |
361 |
346 ```go |
362 ```go |
347 var cmd = &cobra.Command{ |
363 var cmd = &cobra.Command{ |
348 Short: "hello", |
364 Short: "hello", |
349 Args: func(cmd *cobra.Command, args []string) error { |
365 Args: func(cmd *cobra.Command, args []string) error { |
350 if len(args) < 1 { |
366 // Optionally run one of the validators provided by cobra |
351 return errors.New("requires a color argument") |
367 if err := cobra.MinimumNArgs(1)(cmd, args); err != nil { |
|
368 return err |
352 } |
369 } |
|
370 // Run the custom validation logic |
353 if myapp.IsValidColor(args[0]) { |
371 if myapp.IsValidColor(args[0]) { |
354 return nil |
372 return nil |
355 } |
373 } |
356 return fmt.Errorf("invalid color specified: %s", args[0]) |
374 return fmt.Errorf("invalid color specified: %s", args[0]) |
357 }, |
375 }, |
442 ### Example |
460 ### Example |
443 |
461 |
444 The following output is automatically generated by Cobra. Nothing beyond the |
462 The following output is automatically generated by Cobra. Nothing beyond the |
445 command and flag definitions are needed. |
463 command and flag definitions are needed. |
446 |
464 |
447 $ cobra help |
465 $ cobra-cli help |
448 |
466 |
449 Cobra is a CLI library for Go that empowers applications. |
467 Cobra is a CLI library for Go that empowers applications. |
450 This application is a tool to generate the needed files |
468 This application is a tool to generate the needed files |
451 to quickly create a Cobra application. |
469 to quickly create a Cobra application. |
452 |
470 |
453 Usage: |
471 Usage: |
454 cobra [command] |
472 cobra-cli [command] |
455 |
473 |
456 Available Commands: |
474 Available Commands: |
457 add Add a command to a Cobra Application |
475 add Add a command to a Cobra Application |
|
476 completion Generate the autocompletion script for the specified shell |
458 help Help about any command |
477 help Help about any command |
459 init Initialize a Cobra Application |
478 init Initialize a Cobra Application |
460 |
479 |
461 Flags: |
480 Flags: |
462 -a, --author string author name for copyright attribution (default "YOUR NAME") |
481 -a, --author string author name for copyright attribution (default "YOUR NAME") |
463 --config string config file (default is $HOME/.cobra.yaml) |
482 --config string config file (default is $HOME/.cobra.yaml) |
464 -h, --help help for cobra |
483 -h, --help help for cobra-cli |
465 -l, --license string name of license for the project |
484 -l, --license string name of license for the project |
466 --viper use Viper for configuration (default true) |
485 --viper use Viper for configuration |
467 |
486 |
468 Use "cobra [command] --help" for more information about a command. |
487 Use "cobra-cli [command] --help" for more information about a command. |
469 |
488 |
470 |
489 |
471 Help is just a command like any other. There is no special logic or behavior |
490 Help is just a command like any other. There is no special logic or behavior |
472 around it. In fact, you can provide your own if you want. |
491 around it. In fact, you can provide your own if you want. |
473 |
492 |
|
493 ### Grouping commands in help |
|
494 |
|
495 Cobra supports grouping of available commands in the help output. To group commands, each group must be explicitly |
|
496 defined using `AddGroup()` on the parent command. Then a subcommand can be added to a group using the `GroupID` element |
|
497 of that subcommand. The groups will appear in the help output in the same order as they are defined using different |
|
498 calls to `AddGroup()`. If you use the generated `help` or `completion` commands, you can set their group ids using |
|
499 `SetHelpCommandGroupId()` and `SetCompletionCommandGroupId()` on the root command, respectively. |
|
500 |
474 ### Defining your own help |
501 ### Defining your own help |
475 |
502 |
476 You can provide your own Help command or your own template for the default command to use |
503 You can provide your own Help command or your own template for the default command to use |
477 with following functions: |
504 with the following functions: |
478 |
505 |
479 ```go |
506 ```go |
480 cmd.SetHelpCommand(cmd *Command) |
507 cmd.SetHelpCommand(cmd *Command) |
481 cmd.SetHelpFunc(f func(*Command, []string)) |
508 cmd.SetHelpFunc(f func(*Command, []string)) |
482 cmd.SetHelpTemplate(s string) |
509 cmd.SetHelpTemplate(s string) |
491 |
518 |
492 ### Example |
519 ### Example |
493 You may recognize this from the help above. That's because the default help |
520 You may recognize this from the help above. That's because the default help |
494 embeds the usage as part of its output. |
521 embeds the usage as part of its output. |
495 |
522 |
496 $ cobra --invalid |
523 $ cobra-cli --invalid |
497 Error: unknown flag: --invalid |
524 Error: unknown flag: --invalid |
498 Usage: |
525 Usage: |
499 cobra [command] |
526 cobra-cli [command] |
500 |
527 |
501 Available Commands: |
528 Available Commands: |
502 add Add a command to a Cobra Application |
529 add Add a command to a Cobra Application |
|
530 completion Generate the autocompletion script for the specified shell |
503 help Help about any command |
531 help Help about any command |
504 init Initialize a Cobra Application |
532 init Initialize a Cobra Application |
505 |
533 |
506 Flags: |
534 Flags: |
507 -a, --author string author name for copyright attribution (default "YOUR NAME") |
535 -a, --author string author name for copyright attribution (default "YOUR NAME") |
508 --config string config file (default is $HOME/.cobra.yaml) |
536 --config string config file (default is $HOME/.cobra.yaml) |
509 -h, --help help for cobra |
537 -h, --help help for cobra-cli |
510 -l, --license string name of license for the project |
538 -l, --license string name of license for the project |
511 --viper use Viper for configuration (default true) |
539 --viper use Viper for configuration |
512 |
540 |
513 Use "cobra [command] --help" for more information about a command. |
541 Use "cobra [command] --help" for more information about a command. |
514 |
542 |
515 ### Defining your own usage |
543 ### Defining your own usage |
516 You can provide your own usage function or template for Cobra to use. |
544 You can provide your own usage function or template for Cobra to use. |