Documentation
¶
Index ¶
- Variables
- func BindConfig(cmd *cobra.Command, cfg any, opts ...Option)
- func RegisterType[T any](parse func(value string) (T, error), serialize func(value T) string)
- func RootCommand[T any](hooks Hooks[T], cmd cobra.Command, cfg T, opts ...Option) *cobra.Command
- func RootGroup(cmdTmpl cobra.Command, sub ...func(*cobra.Command)) *cobra.Command
- func SubCommand[T any](parent *cobra.Command, hooks Hooks[T], cmd cobra.Command, cfg T, ...) *cobra.Command
- func SubGroup(parent *cobra.Command, cmdTmpl cobra.Command, sub ...func(*cobra.Command)) *cobra.Command
- func UnregisterType[T any]()
- type ErrInvalidEnvironment
- type ErrUnboundEnvironment
- type FlagError
- type Hook
- type Hooks
- type Option
Constants ¶
This section is empty.
Variables ¶
var Environment = true
Environment determines whether environment variables are bound and processed. Change this globally if you use another library for environment variables, e.g. Viper.
Functions ¶
func BindConfig ¶
BindConfig maps fields of cfg to flag sets of cmd. A field's value is set with the following precedence: Explicit flag, environment variable, then whatever is already set in cfg.
Struct tags:
- flag: Set of the flags defined above, separated by commas.
- param: "foo,f" for --foo=bar or -f x. Defaults to kebab-case of field name, long opt only.
- encoding: Type-specific encoding, e.g. "base64" for []byte.
- env: Environment variable name, "-" for none, defaults to prefixed screaming snake case.
- usage: Flag usage string. Environment variable name is appended if set.
func RegisterType ¶ added in v0.2.0
RegisterType registers a custom type for BindConfig. The typical use-case for RegisterType is to make third-party types embeddable into configuration structs. Implementing pflag.Value is the nicer solution for first-party types, because it does not rely on global state.
func RootCommand ¶ added in v0.2.0
RootCommand adds nicecmd-specific persistent arguments to the given command, e.g. --env-file for loading dotenv files. It is otherwise identical to Command.
func RootGroup ¶ added in v0.2.1
RootGroup is a convenience function to construct a command group at the application's root. The command group does not take any special configuration.
func SubCommand ¶ added in v0.2.0
func SubCommand[T any](parent *cobra.Command, hooks Hooks[T], cmd cobra.Command, cfg T, opts ...Option) *cobra.Command
SubCommand wraps a cobra.Command to pass a config bound via BindConfig to a set of cobra run functions.
func SubGroup ¶ added in v0.2.1
func SubGroup(parent *cobra.Command, cmdTmpl cobra.Command, sub ...func(*cobra.Command)) *cobra.Command
SubGroup is a convenience function to construct a command group without special configuration.
func UnregisterType ¶ added in v0.2.0
func UnregisterType[T any]()
UnregisterType removes a custom type registration. Useful for testing.
Types ¶
type ErrInvalidEnvironment ¶ added in v0.2.0
type ErrInvalidEnvironment struct {
FlagErrors []FlagError
}
func (ErrInvalidEnvironment) Error ¶ added in v0.2.0
func (e ErrInvalidEnvironment) Error() string
type ErrUnboundEnvironment ¶ added in v0.2.0
type ErrUnboundEnvironment struct {
Names []string
}
func (ErrUnboundEnvironment) Error ¶ added in v0.2.0
func (e ErrUnboundEnvironment) Error() string
type Hook ¶ added in v0.2.0
Hook matches cobra.Command's various RunE functions, plus a config to be bound via Command.
type Hooks ¶ added in v0.2.0
type Hooks[T any] struct { PersistentPreRun Hook[T] PreRun Hook[T] Run Hook[T] PostRun Hook[T] PersistentPostRun Hook[T] }
Hooks provides various RunE functions of cobra.Command. Cobra's naming scheme is reused here. For clarity, only the "persistent" hooks run even when a subcommand is called.
func Run ¶
Run is a convenience function to create Hooks with only the Run function set. This function is executed only when the user runs this specific command.
type Option ¶ added in v0.2.0
type Option func(*config)
func WithEnvPrefix ¶ added in v0.2.0
WithEnvPrefix sets a prefix to prepend to env vars, separated by an underscore. For sub-structs, the prefix is further extended with the screaming snake case of the field name under which the struct is embedded. When no prefix is set, then environment variables are unavailable unless set explicitly.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
nicecmd-fizzbuzz
command
nicecmd-fizzbuzz is an enterprise-grade, highly configurable fizz buzzer (everything in this tool is nonsense, but should demonstrate nicecmd usage)
|
nicecmd-fizzbuzz is an enterprise-grade, highly configurable fizz buzzer (everything in this tool is nonsense, but should demonstrate nicecmd usage) |
|
nicecmd-readme
command
|