| name | add-command |
| description | Add a new cobra subcommand to the CLI |
| user_invocable | true |
Add Command
Add a new cobra subcommand following the project's patterns.
Steps
- Read existing commands in
cmd/ to understand patterns
- Create
cmd/<name>.go with a New<Name>Cmd factory function
- The factory accepts a parent options struct or
*iostreams.IOStreams
- Register the command with its parent via
AddCommand in the parent's factory
- Add
--format output support (json, table, yaml)
- Implement both interactive (TTY prompts) and non-interactive (flags-only) paths
- Create
cmd/<name>_test.go with tests using iostreams.Test()
Command Pattern
func NewExampleCmd(ios *iostreams.IOStreams) *cobra.Command {
opts := &ExampleOptions{IOStreams: ios}
cmd := &cobra.Command{
Use: "example",
Short: "One-line description",
RunE: func(_ *cobra.Command, _ []string) error {
return runExample(opts)
},
}
cmd.Flags().StringVar(&opts.Format, "format", "", "Output format: json, table, yaml")
return cmd
}
func runExample(opts *ExampleOptions) error {
return nil
}
Checklist