Documentation
¶
Index ¶
- func GetThirdPartyCL(flagSet *pflag.FlagSet, knownBy mamba.KnownByCollection) mamba.ThirdPartyCommandLine
- type CobraCommandSpec
- type CobraContainer
- func (container *CobraContainer) Command(name string) *cobra.Command
- func (container *CobraContainer) IsPresent(name string) bool
- func (container *CobraContainer) MustGetParamSet(name string) any
- func (container *CobraContainer) MustRegisterCommand(parent string, command *cobra.Command)
- func (container *CobraContainer) MustRegisterCommands(parent string, specs ...*CobraCommandSpec)
- func (container *CobraContainer) MustRegisterParamSet(name string, ps any)
- func (container *CobraContainer) MustRegisterRootedCommand(command *cobra.Command)
- func (container *CobraContainer) Native(name string) any
- func (container *CobraContainer) Root() *cobra.Command
- type FlagDefinitions
- type FlagInfo
- type LongFlagName
- type ParamSet
- func (params *ParamSet[N]) BindBool(info *FlagInfo, to *bool) *ParamSet[N]
- func (params *ParamSet[N]) BindBoolSlice(info *FlagInfo, to *[]bool) *ParamSet[N]
- func (params *ParamSet[N]) BindDuration(info *FlagInfo, to *time.Duration) *ParamSet[N]
- func (params *ParamSet[N]) BindDurationSlice(info *FlagInfo, to *[]time.Duration) *ParamSet[N]
- func (params *ParamSet[N]) BindEnum(info *FlagInfo, to *string) *ParamSet[N]
- func (params *ParamSet[N]) BindFloat32(info *FlagInfo, to *float32) *ParamSet[N]
- func (params *ParamSet[N]) BindFloat32Slice(info *FlagInfo, to *[]float32) *ParamSet[N]
- func (params *ParamSet[N]) BindFloat64(info *FlagInfo, to *float64) *ParamSet[N]
- func (params *ParamSet[N]) BindFloat64Slice(info *FlagInfo, to *[]float64) *ParamSet[N]
- func (params *ParamSet[N]) BindIPMask(info *FlagInfo, to *net.IPMask) *ParamSet[N]
- func (params *ParamSet[N]) BindIPNet(info *FlagInfo, to *net.IPNet) *ParamSet[N]
- func (params *ParamSet[N]) BindInt(info *FlagInfo, to *int) *ParamSet[N]
- func (params *ParamSet[N]) BindInt8(info *FlagInfo, to *int8) *ParamSet[N]
- func (params *ParamSet[N]) BindInt16(info *FlagInfo, to *int16) *ParamSet[N]
- func (params *ParamSet[N]) BindInt32(info *FlagInfo, to *int32) *ParamSet[N]
- func (params *ParamSet[N]) BindInt32Slice(info *FlagInfo, to *[]int32) *ParamSet[N]
- func (params *ParamSet[N]) BindInt64(info *FlagInfo, to *int64) *ParamSet[N]
- func (params *ParamSet[N]) BindInt64Slice(info *FlagInfo, to *[]int64) *ParamSet[N]
- func (params *ParamSet[N]) BindIntSlice(info *FlagInfo, to *[]int) *ParamSet[N]
- func (params *ParamSet[N]) BindString(info *FlagInfo, to *string) *ParamSet[N]
- func (params *ParamSet[N]) BindStringSlice(info *FlagInfo, to *[]string) *ParamSet[N]
- func (params *ParamSet[N]) BindUint(info *FlagInfo, to *uint) *ParamSet[N]
- func (params *ParamSet[N]) BindUint8(info *FlagInfo, to *uint8) *ParamSet[N]
- func (params *ParamSet[N]) BindUint16(info *FlagInfo, to *uint16) *ParamSet[N]
- func (params *ParamSet[N]) BindUint32(info *FlagInfo, to *uint32) *ParamSet[N]
- func (params *ParamSet[N]) BindUint64(info *FlagInfo, to *uint64) *ParamSet[N]
- func (params *ParamSet[N]) BindUintSlice(info *FlagInfo, to *[]uint) *ParamSet[N]
- func (params *ParamSet[N]) ResolveFlagSet(info *FlagInfo) *pflag.FlagSet
- type ShortFlagName
- type ShortFlagOverride
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func GetThirdPartyCL ¶
func GetThirdPartyCL( flagSet *pflag.FlagSet, knownBy mamba.KnownByCollection, ) mamba.ThirdPartyCommandLine
GetThirdPartyCL creates a command line from the flag set. If there are any short form flags, they are resolved using the knownBy map, which the client provides, mapping long form flag names to their short form. The client can choose to compose a command line consisting of all available flags or just the ones changed by the user (ie, they are explicitly specified on the command line as opposed to be defaulted).
Types ¶
type CobraCommandSpec ¶
type CobraCommandSpec struct {
// Command: a pointer to the underlying cobra command
//
Command *cobra.Command
}
CobraCommandSpec is a wrapper around the cobra command, require to register multiple commands at he same time, see MustRegisterCommands.
type CobraContainer ¶
type CobraContainer struct {
// contains filtered or unexported fields
}
CobraContainer is a wrapper around the collection of cobra commands. Please see unit tests for examples of how to use the CobraContainer.
func NewCobraContainer ¶
func NewCobraContainer(root *cobra.Command) *CobraContainer
NewCobraContainer is a factory function for the CobraContainer. The client must pass in the root Cobra command.
- root: the root Cobra command.
func (*CobraContainer) Command ¶
func (container *CobraContainer) Command(name string) *cobra.Command
Command returns the command registered with the name specified
- name: the name of the Cobra command to check. The name can be derived by calling the Name() function on the cobra command.
Returns the command identified by the name, nil if the command does not exist.
func (*CobraContainer) IsPresent ¶
func (container *CobraContainer) IsPresent(name string) bool
IsPresent checks whether a command has been registered anywhere within the command tree. NB, the container stores all commands in a flat hierarchy as opposed to Cobra which stores commands in a tree like hierarchy.
- name: the name of the command to check.
Returns true if present, false otherwise.
func (*CobraContainer) MustGetParamSet ¶
func (container *CobraContainer) MustGetParamSet(name string) any
MustGetParamSet like Native, except that it returns the parameter set wrapper. The client must perform a type assertion on the returned pointer to translate it back into the native type, ie MustGetParamSet[N] (as opposed to N).
func (*CobraContainer) MustRegisterCommand ¶
func (container *CobraContainer) MustRegisterCommand(parent string, command *cobra.Command)
MustRegisterCommand stores a command inside the container. The client passes in the name of the parent command and the command is added to that parent.
- parent: the name of the parent command. The name can be derived by calling the Name() member function of the Cobra command.
- command: the Cobra command to register.
panics if the there is no command currently registered with the name of parent.
func (*CobraContainer) MustRegisterCommands ¶
func (container *CobraContainer) MustRegisterCommands(parent string, specs ...*CobraCommandSpec)
MustRegisterCommands invokes MustRegisterCommand for each command in the list.
func (*CobraContainer) MustRegisterParamSet ¶
func (container *CobraContainer) MustRegisterParamSet(name string, ps any)
MustRegisterParamSet stores the parameter set under the provided name. Used to reduce the number of floating global variables that the client needs to manage when using cobra.
panics if param set already registered, or attempt to register with an inappropriate type.
func (*CobraContainer) MustRegisterRootedCommand ¶
func (container *CobraContainer) MustRegisterRootedCommand(command *cobra.Command)
MustRegisterRootedCommand stores a command inside the container as a direct descendent of the root Cobra command and is added to the root command itself.
- command: the Cobra command to register.
panics if the command with the same name has already been registered.
func (*CobraContainer) Native ¶
func (container *CobraContainer) Native(name string) any
Native retrieves the Native parameter set that was previously registered.
func (*CobraContainer) Root ¶
func (container *CobraContainer) Root() *cobra.Command
Root returns the root command.
type FlagDefinitions ¶
type FlagDefinitions map[LongFlagName]ShortFlagName
FlagDefinitions
func MergeFlagDefinitions ¶
func MergeFlagDefinitions(families ...FlagDefinitions) FlagDefinitions
func (FlagDefinitions) Clone ¶
func (fd FlagDefinitions) Clone() FlagDefinitions
type FlagInfo ¶
type FlagInfo struct {
// Name of flag derived from the Usage
//
Name string
// Usage provides a description of the flag, the first word should eb the name
// of the flag.
//
Usage string
// Short is the 1 letter character shortcut for the flag.
//
Short string
// Default is the default value for the flag if the user does not provide a
// value.
//
Default any
// AlternativeFlagSet defines the flag set to use. Allows the user to specify which flag
// to define this flag on. By default, it is on command.Flags()
//
AlternativeFlagSet *pflag.FlagSet
}
FlagInfo collates together the parameters passed into the bind methods The Bind methods are just a wrapper around invoking the type based methods on the cobra flag set in order to define flags.
func NewFlagInfo ¶
NewFlagInfo factory function for FlagInfo. Use this function if the flag is to be defined on the default flag set, ie the one on command.Flags().
func NewFlagInfoOnFlagSet ¶
func NewFlagInfoOnFlagSet(usage, short string, def any, alternativeFlagSet *pflag.FlagSet, ) *FlagInfo
NewFlagInfoOnFlagSet factory function for FlagInfo, with an alternative flag set. This function need only be usd to enable defining flags on the flag set other than that of command.Flags(), eg command.PersistentFlags().
type ParamSet ¶
type ParamSet[N any] struct { // Native is the native client defined parameter set instance, which // must be a struct. // Native *N // FlagSet is the default Cobra FlagSet // FlagSet *pflag.FlagSet // Command is the cobra command that the parameter set is bound to. // Command *cobra.Command }
ParamSet represents a set of flags/options/positional args for a command. The term 'parameter set' is used really to distinguish from other established abstractions (flags/options/positional args, otherwise to be referred to as inputs). The ParamSet is used to ensure that all these inputs are collated into a single entity that the application can refer to as required. A command can have multiple parameter sets associated with it, but will probably best be used with a single parameter set, where inputs not provided by the end user are defaulted, perhaps from config. If its essential to distinguish between different activation scenarios (ie which set of parameters that the user provides) then the client can define multiple parameter sets to reflect this.
The binder methods are defined explicitly for each type as 'go' does not allow for generic parameters defined at the method level as opposed to being defined on the receiver struct.
The generic parameter N represents the client defined native parameter set. Eg:
type WidgetParameterSet struct {
Directory string
Output string
Format OutputFormatEnum
Shape InfexionShapeEnum
Concise bool
Strategy TraversalStrategyEnum
Overwrite bool
Pattern string}
... is known as the 'native' parameter set for a 'widget' command which would be used to instantiate ParamSet in a declaration as follows:
var paramSet *ParamSet[WidgetParameterSet].
func NewParamSet ¶
func NewParamSet[N any](command *cobra.Command, overrides ...ShortFlagOverride) (ps *ParamSet[N])
NewParamSet is the factory function, which creates a 'parameter set' for a command. Each command can have multiple command sets, reflecting the different ways a command can be used
paramSet = NewParamSet[WidgetParameterSet](widgetCommand)
The default flag set is defined, ie command.Flags(). If an alternative flag set is required, then the client should use
The generic parameter N represents the client defined native parameter set.
func (*ParamSet[N]) BindBool ¶
BindBool binds bool slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindBoolSlice ¶
BindBoolSlice binds []bool slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindDuration ¶
BindDuration binds time.Duration slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindDurationSlice ¶
BindDurationSlice binds []time.Duration slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindEnum ¶
BindEnum binds enum slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindFloat32 ¶
BindFloat32 binds float32 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindFloat32Slice ¶
BindFloat32Slice binds []float32 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindFloat64 ¶
BindFloat64 binds float64 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindFloat64Slice ¶
BindFloat64Slice binds []float64 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindIPMask ¶
BindIPMask binds net.IPMask slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindIPNet ¶
BindIPNet binds net.IPNet slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindInt ¶
BindInt binds int slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindInt8 ¶
BindInt8 binds int8 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindInt16 ¶
BindInt16 binds int16 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindInt32 ¶
BindInt32 binds int32 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindInt32Slice ¶
BindInt32Slice binds []int32 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindInt64 ¶
BindInt64 binds int64 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindInt64Slice ¶
BindInt64Slice binds []int64 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindIntSlice ¶
BindIntSlice binds []int slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindString ¶
BindString binds string slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindStringSlice ¶
BindStringSlice binds []string slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindUint ¶
BindUint binds uint slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindUint8 ¶
BindUint8 binds uint8 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindUint16 ¶
BindUint16 binds uint16 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindUint32 ¶
BindUint32 binds uint32 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindUint64 ¶
BindUint64 binds uint64 slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) BindUintSlice ¶
BindUintSlice binds []uint slice flag with a shorthand if 'info.Short' has been set otherwise binds without a short name.
func (*ParamSet[N]) ResolveFlagSet ¶
ResolveFlagSet resolves between the default flag set on the param set and the optional one defined on the FlagInfo. If there is no default flag set, then there must be one on the flag info, otherwise a panic will occur due dereferencing a nil pointer.
type ShortFlagOverride ¶
type ShortFlagOverride func(FlagDefinitions)
ShortFlagOverride allows a caller to override a specific short flag name at bind time, without affecting the package-level definitions. Use this when two parameter sets in the same command have competing short codes.
fam.Native.BindAll(fam, store.WithShortFlag("files-glob", "F"))
func WithShortFlag ¶
func WithShortFlag(long LongFlagName, short ShortFlagName) ShortFlagOverride
WithShortFlag returns a ShortFlagOverride that reassigns the short code for a given long flag name.