Documentation ¶
Overview ¶
Package clibase offers an all-in-one solution for a highly configurable CLI application. Within Coder, we use it for all of our subcommands, which demands more functionality than cobra/viber offers.
The Command interface is loosely based on the chi middleware pattern and http.Handler/HandlerFunc.
Index ¶
- Variables
- type Annotations
- type Bool
- type Cmd
- type Duration
- type Enum
- type EnvVar
- type Environ
- type Group
- type HandlerFunc
- type HostPort
- func (hp *HostPort) MarshalJSON() ([]byte, error)
- func (hp *HostPort) MarshalYAML() (interface{}, error)
- func (hp *HostPort) Set(v string) error
- func (hp *HostPort) String() string
- func (*HostPort) Type() string
- func (hp *HostPort) UnmarshalJSON(b []byte) error
- func (hp *HostPort) UnmarshalYAML(n *yaml.Node) error
- type Int64
- type Invocation
- type MiddlewareFunc
- type NoOptDefValuer
- type Option
- type OptionSet
- func (s *OptionSet) Add(opts ...Option)
- func (s *OptionSet) ByName(name string) *Option
- func (s OptionSet) Filter(filter func(opt Option) bool) OptionSet
- func (s *OptionSet) FlagSet() *pflag.FlagSet
- func (s *OptionSet) MarshalYAML() (any, error)
- func (s *OptionSet) ParseEnv(vs []EnvVar) error
- func (s *OptionSet) SetDefaults() error
- func (s *OptionSet) UnmarshalYAML(rootNode *yaml.Node) error
- type RunCommandError
- type String
- type StringArray
- type Struct
- func (s *Struct[T]) MarshalJSON() ([]byte, error)
- func (s *Struct[T]) MarshalYAML() (interface{}, error)
- func (s *Struct[T]) Set(v string) error
- func (s *Struct[T]) String() string
- func (s *Struct[T]) Type() string
- func (s *Struct[T]) UnmarshalJSON(b []byte) error
- func (s *Struct[T]) UnmarshalYAML(n *yaml.Node) error
- type URL
- func (u *URL) MarshalJSON() ([]byte, error)
- func (u *URL) MarshalYAML() (interface{}, error)
- func (u *URL) Set(v string) error
- func (u *URL) String() string
- func (*URL) Type() string
- func (u *URL) UnmarshalJSON(b []byte) error
- func (u *URL) UnmarshalYAML(n *yaml.Node) error
- func (u *URL) Value() *url.URL
- type ValueSource
- type YAMLConfigPath
Constants ¶
This section is empty.
Variables ¶
var DiscardValue discardValue
DiscardValue does nothing but implements the pflag.Value interface. It's useful in cases where you want to accept an option, but access the underlying value directly instead of through the Option methods.
Functions ¶
This section is empty.
Types ¶
type Annotations ¶
Annotations is an arbitrary key-mapping used to extend the Option and Command types. Its methods won't panic if the map is nil.
func (Annotations) Get ¶
func (a Annotations) Get(key string) (string, bool)
Get retrieves a key from the map, returning false if the key is not found or the map is nil.
func (Annotations) IsSet ¶
func (a Annotations) IsSet(key string) bool
IsSet returns true if the key is set in the annotations map.
func (Annotations) Mark ¶
func (a Annotations) Mark(key string, value string) Annotations
Mark sets a value on the annotations map, creating one if it doesn't exist. Mark does not mutate the original and returns a copy. It is suitable for chaining.
type Cmd ¶
type Cmd struct { // Parent is the direct parent of the command. Parent *Cmd // Children is a list of direct descendants. Children []*Cmd // Use is provided in form "command [flags] [args...]". Use string // Aliases is a list of alternative names for the command. Aliases []string // Short is a one-line description of the command. Short string // Hidden determines whether the command should be hidden from help. Hidden bool // RawArgs determines whether the command should receive unparsed arguments. // No flags are parsed when set, and the command is responsible for parsing // its own flags. RawArgs bool // Long is a detailed description of the command, // presented on its help page. It may contain examples. Long string Options OptionSet Annotations Annotations // Middleware is called before the Handler. // Use Chain() to combine multiple middlewares. Middleware MiddlewareFunc Handler HandlerFunc HelpHandler HandlerFunc }
Cmd describes an executable command.
func (*Cmd) AddSubcommands ¶ added in v0.21.0
AddSubcommands adds the given subcommands, setting their Parent field automatically.
func (*Cmd) FullName ¶
FullName returns the full invocation name of the command, as seen on the command line.
func (*Cmd) FullUsage ¶
FullName returns usage of the command, preceded by the usage of its parents.
func (*Cmd) Invoke ¶ added in v0.20.0
func (c *Cmd) Invoke(args ...string) *Invocation
Invoke creates a new invocation of the command, with stdio discarded.
The returned invocation is not live until Run() is called.
func (*Cmd) PrepareAll ¶ added in v0.21.0
PrepareAll performs initialization and linting on the command and all its children.
type Duration ¶
func DurationOf ¶ added in v0.20.0
func (*Duration) MarshalYAML ¶ added in v0.22.1
func (*Duration) UnmarshalYAML ¶ added in v0.22.1
type Environ ¶ added in v0.20.0
type Environ []EnvVar
func ParseEnviron ¶ added in v0.20.0
ParseEnviron returns all environment variables starting with prefix without said prefix.
type Group ¶
type Group struct { Parent *Group `json:"parent,omitempty"` Name string `json:"name,omitempty"` YAML string `json:"yaml,omitempty"` Description string `json:"description,omitempty"` }
Group describes a hierarchy of groups that an option or command belongs to.
type HandlerFunc ¶ added in v0.20.0
type HandlerFunc func(i *Invocation) error
HandlerFunc handles an Invocation of a command.
type HostPort ¶
HostPort is a host:port pair.
func (*HostPort) MarshalJSON ¶
func (*HostPort) MarshalYAML ¶ added in v0.22.1
func (*HostPort) UnmarshalJSON ¶
func (*HostPort) UnmarshalYAML ¶ added in v0.22.1
type Invocation ¶ added in v0.20.0
type Invocation struct { Command *Cmd Args []string // Environ is a list of environment variables. Use EnvsWithPrefix to parse // os.Environ. Environ Environ Stdout io.Writer Stderr io.Writer Stdin io.Reader // contains filtered or unexported fields }
Invocation represents an instance of a command being executed.
func (*Invocation) Context ¶ added in v0.20.0
func (inv *Invocation) Context() context.Context
func (*Invocation) ParsedFlags ¶ added in v0.20.0
func (inv *Invocation) ParsedFlags() *pflag.FlagSet
func (*Invocation) Run ¶ added in v0.20.0
func (inv *Invocation) Run() (err error)
Run executes the command. If two command share a flag name, the first command wins.
func (*Invocation) WithContext ¶ added in v0.20.0
func (inv *Invocation) WithContext(ctx context.Context) *Invocation
WithContext returns a copy of the Invocation with the given context.
func (*Invocation) WithOS ¶ added in v0.20.0
func (inv *Invocation) WithOS() *Invocation
WithOS returns the invocation as a main package, filling in the invocation's unset fields with OS defaults.
type MiddlewareFunc ¶ added in v0.20.0
type MiddlewareFunc func(next HandlerFunc) HandlerFunc
MiddlewareFunc returns the next handler in the chain, or nil if there are no more.
func Chain ¶ added in v0.20.0
func Chain(ms ...MiddlewareFunc) MiddlewareFunc
Chain returns a Handler that first calls middleware in order.
func RequireNArgs ¶ added in v0.20.0
func RequireNArgs(want int) MiddlewareFunc
func RequireRangeArgs ¶ added in v0.20.0
func RequireRangeArgs(start, end int) MiddlewareFunc
RequireRangeArgs returns a Middleware that requires the number of arguments to be between start and end (inclusive). If end is -1, then the number of arguments must be at least start.
type NoOptDefValuer ¶
type NoOptDefValuer interface {
NoOptDefValue() string
}
NoOptDefValuer describes behavior when no option is passed into the flag.
This is useful for boolean or otherwise binary flags.
type Option ¶
type Option struct { Name string `json:"name,omitempty"` Description string `json:"description,omitempty"` // Flag is the long name of the flag used to configure this option. If unset, // flag configuring is disabled. Flag string `json:"flag,omitempty"` // FlagShorthand is the one-character shorthand for the flag. If unset, no // shorthand is used. FlagShorthand string `json:"flag_shorthand,omitempty"` // Env is the environment variable used to configure this option. If unset, // environment configuring is disabled. Env string `json:"env,omitempty"` // YAML is the YAML key used to configure this option. If unset, YAML // configuring is disabled. YAML string `json:"yaml,omitempty"` // Default is parsed into Value if set. Default string `json:"default,omitempty"` // Value includes the types listed in values.go. Value pflag.Value `json:"value,omitempty"` // Annotations enable extensions to clibase higher up in the stack. It's useful for // help formatting and documentation generation. Annotations Annotations `json:"annotations,omitempty"` // Group is a group hierarchy that helps organize this option in help, configs // and other documentation. Group *Group `json:"group,omitempty"` // UseInstead is a list of options that should be used instead of this one. // The field is used to generate a deprecation warning. UseInstead []Option `json:"use_instead,omitempty"` Hidden bool `json:"hidden,omitempty"` ValueSource ValueSource `json:"value_source,omitempty"` }
Option is a configuration option for a CLI application.
type OptionSet ¶
type OptionSet []Option
OptionSet is a group of options that can be applied to a command.
func (*OptionSet) ByName ¶ added in v0.22.1
ByName returns the Option with the given name, or nil if no such option exists.
func (OptionSet) Filter ¶ added in v0.23.0
Filter will only return options that match the given filter. (return true)
func (*OptionSet) MarshalYAML ¶ added in v0.22.1
MarshalYAML converts the option set to a YAML node, that can be converted into bytes via yaml.Marshal.
The node is returned to enable post-processing higher up in the stack.
It is isomorphic with FromYAML.
func (*OptionSet) ParseEnv ¶
ParseEnv parses the given environment variables into the OptionSet. Use EnvsWithPrefix to filter out prefixes.
func (*OptionSet) SetDefaults ¶
SetDefaults sets the default values for each Option, skipping values that already have a value source.
func (*OptionSet) UnmarshalYAML ¶ added in v0.22.1
UnmarshalYAML converts the given YAML node into the option set. It is isomorphic with ToYAML.
type RunCommandError ¶ added in v0.21.0
func (*RunCommandError) Error ¶ added in v0.21.0
func (e *RunCommandError) Error() string
func (*RunCommandError) Unwrap ¶ added in v0.21.0
func (e *RunCommandError) Unwrap() error
type StringArray ¶ added in v0.21.0
type StringArray []string
StringArray is a slice of strings that implements pflag.Value and pflag.SliceValue.
func StringArrayOf ¶ added in v0.21.0
func StringArrayOf(ss *[]string) *StringArray
func (*StringArray) Append ¶ added in v0.21.0
func (s *StringArray) Append(v string) error
func (*StringArray) GetSlice ¶ added in v0.21.0
func (s *StringArray) GetSlice() []string
func (*StringArray) Replace ¶ added in v0.21.0
func (s *StringArray) Replace(vals []string) error
func (*StringArray) Set ¶ added in v0.21.0
func (s *StringArray) Set(v string) error
func (StringArray) String ¶ added in v0.21.0
func (s StringArray) String() string
func (StringArray) Type ¶ added in v0.21.0
func (StringArray) Type() string
func (StringArray) Value ¶ added in v0.21.0
func (s StringArray) Value() []string
type Struct ¶
type Struct[T any] struct { Value T }
Struct is a special value type that encodes an arbitrary struct. It implements the flag.Value interface, but in general these values should only be accepted via config for ergonomics.
The string encoding type is YAML.
func (*Struct[T]) MarshalJSON ¶
func (*Struct[T]) MarshalYAML ¶
func (*Struct[T]) UnmarshalJSON ¶
func (*Struct[T]) UnmarshalYAML ¶
type URL ¶
func (*URL) MarshalJSON ¶
func (*URL) MarshalYAML ¶ added in v0.22.1
func (*URL) UnmarshalJSON ¶
func (*URL) UnmarshalYAML ¶ added in v0.22.1
type ValueSource ¶ added in v0.22.1
type ValueSource string
const ( ValueSourceNone ValueSource = "" ValueSourceFlag ValueSource = "flag" ValueSourceEnv ValueSource = "env" ValueSourceYAML ValueSource = "yaml" ValueSourceDefault ValueSource = "default" )
type YAMLConfigPath ¶ added in v0.22.1
type YAMLConfigPath string
YAMLConfigPath is a special value type that encodes a path to a YAML configuration file where options are read from.
func (*YAMLConfigPath) Set ¶ added in v0.22.1
func (p *YAMLConfigPath) Set(v string) error
func (*YAMLConfigPath) String ¶ added in v0.22.1
func (p *YAMLConfigPath) String() string
func (*YAMLConfigPath) Type ¶ added in v0.22.1
func (*YAMLConfigPath) Type() string