Adding Flags ๐ฉ¶
Forgot to add a flag when creating a command? No problem! The add-flag utility is designed to inject new flags into existing commands effortlessly, keeping your manifest and code in sync.
Usage¶
To add a new flag to an existing command:
This command performs three key actions:
- Updates
.gtb/manifest.yamlwith the new flag definition. - Regenerates
pkg/cmd/my-command/cmd.goto include the flag registration and struct fields. - Leaves your
pkg/cmd/my-command/main.gountouched, ready for you to use the new flag!
Separation of Concerns
We separate the boilerplate (cmd.go) from your logic (main.go) so that we can regenerate the boilerplate as you add flags or change configurations without overwriting your hard work!
Supported Types¶
You can use any of the following types for your flags:
stringboolintfloat64stringSliceintSlice
Examples¶
Adding a Boolean Flag¶
Adding a Slice Flag¶
Adding a Persistent Flag¶
Persistent flags are available to the command they are defined on AND all of its subcommands.
# Add a persistent config flag to the root command
go run main.go generate add-flag -c root -n config -t string -d "Config file" --persistent
Project Path¶
If your project root is not the current directory, use the --path (-p) flag:
go run main.go generate add-flag -c my-command -n retry -t int -d "Number of retries" -p /path/to/project
Targeting Nested Commands¶
To add a flag to a nested command, provide the full path in the -c argument: