{"modulePath":"github.com/coder/serpent","version":"v0.15.0","isLatest":true,"isStandardLibrary":false,"goos":"all","goarch":"all","docs":"# package serpent\n\nPackage serpent 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.\n\nThe Command interface is loosely based on the chi middleware pattern and http.Handler/HandlerFunc.\n\n## Constants\n\n```go\nconst CompletionModeEnv = \"COMPLETION_MODE\"\n```\nCompletionModeEnv is a special environment variable that is set when the command is being run in completion mode.\n\n## Variables\n\n```go\nvar DiscardValue discardValue\n```\nDiscardValue 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.\n\n## Functions\n\n```go\nfunc DefaultCompletionHandler(inv *Invocation) []string\n```\nDefaultCompletionHandler is a handler that prints all the subcommands, or all the options that haven't been exhaustively set, if the current word starts with a dash.\n\n## Types\n\n```go\ntype Annotations map[string]string\n```\nAnnotations is an arbitrary key-mapping used to extend the Option and Command types. Its methods won't panic if the map is nil.\n\n```go\nfunc (a Annotations) Get(key string) (string, bool)\n```\nGet retrieves a key from the map, returning false if the key is not found or the map is nil.\n\n```go\nfunc (a Annotations) IsSet(key string) bool\n```\nIsSet returns true if the key is set in the annotations map.\n\n```go\nfunc (a Annotations) Mark(key string, value string) Annotations\n```\nMark 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.\n\n```go\ntype Bool bool\n```\n\n```go\nfunc BoolOf(b *bool) *Bool\n```\n\n```go\nfunc (*Bool) NoOptDefValue() string\n```\n\n```go\nfunc (b *Bool) Set(s string) error\n```\n\n```go\nfunc (b Bool) String() string\n```\n\n```go\nfunc (Bool) Type() string\n```\n\n```go\nfunc (b Bool) Value() bool\n```\n\n```go\ntype Command struct {\n\t// Parent is the direct parent of the command.\n\t//\n\t// It is set automatically when an invokation runs.\n\tParent *Command\n\n\t// Children is a list of direct descendants.\n\tChildren []*Command\n\n\t// Use is provided in form \"command [flags] [args...]\".\n\tUse string\n\n\t// Aliases is a list of alternative names for the command.\n\tAliases []string\n\n\t// Short is a one-line description of the command.\n\tShort string\n\n\t// Hidden determines whether the command should be hidden from help.\n\tHidden bool\n\n\t// Deprecated indicates whether this command is deprecated.\n\t// If empty, the command is not deprecated.\n\t// If set, the value is used as the deprecation message.\n\tDeprecated string `json:\"deprecated,omitempty\"`\n\n\t// RawArgs determines whether the command should receive unparsed arguments.\n\t// No flags are parsed when set, and the command is responsible for parsing\n\t// its own flags.\n\tRawArgs bool\n\n\t// Long is a detailed description of the command,\n\t// presented on its help page. It may contain examples.\n\tLong        string\n\tOptions     OptionSet\n\tAnnotations Annotations\n\n\t// Middleware is called before the Handler.\n\t// Use Chain() to combine multiple middlewares.\n\tMiddleware  MiddlewareFunc\n\tHandler     HandlerFunc\n\tHelpHandler HandlerFunc\n\t// CompletionHandler is called when the command is run in completion\n\t// mode. If nil, only the default completion handler is used.\n\t//\n\t// Flag and option parsing is best-effort in this mode, so even if an Option\n\t// is \"required\" it may not be set.\n\tCompletionHandler CompletionHandlerFunc\n}\n```\nCommand describes an executable command.\n\n```go\nfunc (c *Command) AddSubcommands(cmds ...*Command)\n```\nAddSubcommands adds the given subcommands, setting their Parent field automatically.\n\n```go\nfunc (c *Command) FullName() string\n```\nFullName returns the full invocation name of the command, as seen on the command line.\n\n```go\nfunc (c *Command) FullOptions() OptionSet\n```\nFullOptions returns the options of the command and its parents.\n\n```go\nfunc (c *Command) FullUsage() string\n```\nFullName returns usage of the command, preceded by the usage of its parents.\n\n```go\nfunc (c *Command) Invoke(args ...string) *Invocation\n```\nInvoke creates a new invocation of the command, with stdio discarded.\n\nThe returned invocation is not live until Run() is called.\n\n```go\nfunc (c *Command) Name() string\n```\nName returns the first word in the Use string.\n\n```go\nfunc (c *Command) Walk(fn func(*Command))\n```\nWalk calls fn for the command and all its children.\n\n```go\ntype CompletionHandlerFunc func(i *Invocation) []string\n```\n\n```go\ntype Duration time.Duration\n```\n\n```go\nfunc DurationOf(d *time.Duration) *Duration\n```\n\n```go\nfunc (d *Duration) MarshalYAML() (interface{}, error)\n```\n\n```go\nfunc (d *Duration) Set(v string) error\n```\n\n```go\nfunc (d *Duration) String() string\n```\n\n```go\nfunc (Duration) Type() string\n```\n\n```go\nfunc (d *Duration) UnmarshalYAML(n *yaml.Node) error\n```\n\n```go\nfunc (d *Duration) Value() time.Duration\n```\n\n```go\ntype Enum struct {\n\tChoices []string\n\tValue   *string\n}\n```\n\n```go\nfunc EnumOf(v *string, choices ...string) *Enum\n```\n\n```go\nfunc (e *Enum) MarshalYAML() (interface{}, error)\n```\n\n```go\nfunc (e *Enum) Set(v string) error\n```\n\n```go\nfunc (e *Enum) String() string\n```\n\n```go\nfunc (e *Enum) Type() string\n```\n\n```go\nfunc (e *Enum) UnmarshalYAML(n *yaml.Node) error\n```\n\n```go\ntype EnumArray struct {\n\tChoices []string\n\tValue   *[]string\n}\n```\n\n```go\nfunc EnumArrayOf(v *[]string, choices ...string) *EnumArray\n```\n\n```go\nfunc (e *EnumArray) Append(s string) error\n```\n\n```go\nfunc (e *EnumArray) GetSlice() []string\n```\n\n```go\nfunc (e *EnumArray) Replace(ss []string) error\n```\n\n```go\nfunc (e *EnumArray) Set(v string) error\n```\n\n```go\nfunc (e *EnumArray) String() string\n```\n\n```go\nfunc (e *EnumArray) Type() string\n```\n\n```go\ntype EnvVar struct {\n\tName  string\n\tValue string\n}\n```\nVar represents a single environment variable of form NAME=VALUE.\n\n```go\ntype Environ []EnvVar\n```\n\n```go\nfunc ParseEnviron(environ []string, prefix string) Environ\n```\nParseEnviron returns all environment variables starting with prefix without said prefix.\n\n```go\nfunc (e Environ) Get(name string) string\n```\n\n```go\nfunc (e Environ) Lookup(name string) (string, bool)\n```\n\n```go\nfunc (e *Environ) Set(name, value string)\n```\n\n```go\nfunc (e Environ) ToOS() []string\n```\n\n```go\ntype Float64 float64\n```\n\n```go\nfunc Float64Of(f *float64) *Float64\n```\n\n```go\nfunc (f *Float64) Set(s string) error\n```\n\n```go\nfunc (f Float64) String() string\n```\n\n```go\nfunc (Float64) Type() string\n```\n\n```go\nfunc (f Float64) Value() float64\n```\n\n```go\ntype Group struct {\n\tParent      *Group `json:\"parent,omitempty\"`\n\tName        string `json:\"name,omitempty\"`\n\tYAML        string `json:\"yaml,omitempty\"`\n\tDescription string `json:\"description,omitempty\"`\n}\n```\nGroup describes a hierarchy of groups that an option or command belongs to.\n\n```go\nfunc (g *Group) Ancestry() []Group\n```\nAncestry returns the group and all of its parents, in order.\n\n```go\nfunc (g *Group) FullName() string\n```\n\n```go\ntype HandlerFunc func(i *Invocation) error\n```\nHandlerFunc handles an Invocation of a command.\n\n```go\nfunc DefaultHelpFn() HandlerFunc\n```\nDefaultHelpFn returns a function that generates usage (help) output for a given command.\n\n```go\ntype HostPort struct {\n\tHost string\n\tPort string\n}\n```\nHostPort is a host:port pair.\n\n```go\nfunc (hp *HostPort) MarshalJSON() ([]byte, error)\n```\n\n```go\nfunc (hp *HostPort) MarshalYAML() (interface{}, error)\n```\n\n```go\nfunc (hp *HostPort) Set(v string) error\n```\n\n```go\nfunc (hp *HostPort) String() string\n```\n\n```go\nfunc (*HostPort) Type() string\n```\n\n```go\nfunc (hp *HostPort) UnmarshalJSON(b []byte) error\n```\n\n```go\nfunc (hp *HostPort) UnmarshalYAML(n *yaml.Node) error\n```\n\n```go\ntype Int64 int64\n```\n\n```go\nfunc Int64Of(i *int64) *Int64\n```\n\n```go\nfunc (i *Int64) Set(s string) error\n```\n\n```go\nfunc (i Int64) String() string\n```\n\n```go\nfunc (Int64) Type() string\n```\n\n```go\nfunc (i Int64) Value() int64\n```\n\n```go\ntype Invocation struct {\n\tCommand *Command\n\n\t// Args is reduced into the remaining arguments after parsing flags\n\t// during Run.\n\tArgs []string\n\n\t// Environ is a list of environment variables. Use EnvsWithPrefix to parse\n\t// os.Environ.\n\tEnviron Environ\n\tStdout  io.Writer\n\tStderr  io.Writer\n\tStdin   io.Reader\n\n\t// Deprecated\n\tLogger slog.Logger\n\t// Deprecated\n\tNet Net\n\t// contains filtered or unexported fields\n}\n```\nInvocation represents an instance of a command being executed.\n\n```go\nfunc (inv *Invocation) Context() context.Context\n```\n\n```go\nfunc (inv *Invocation) CurWords() (prev string, cur string)\n```\n\n```go\nfunc (inv *Invocation) IsCompletionMode() bool\n```\nIsCompletionMode returns true if the command is being run in completion mode.\n\n```go\nfunc (inv *Invocation) ParsedFlags() *pflag.FlagSet\n```\n\n```go\nfunc (inv *Invocation) Run() (err error)\n```\nRun executes the command. If two command share a flag name, the first command wins.\n\n```go\nfunc (inv *Invocation) SignalNotifyContext(parent context.Context, signals ...os.Signal) (ctx context.Context, stop context.CancelFunc)\n```\nSignalNotifyContext is equivalent to signal.NotifyContext, but supports being overridden in tests.\n\n```go\nfunc (inv *Invocation) WithContext(ctx context.Context) *Invocation\n```\nWithContext returns a copy of the Invocation with the given context.\n\n```go\nfunc (inv *Invocation) WithOS() *Invocation\n```\nWithOS returns the invocation as a main package, filling in the invocation's unset fields with OS defaults.\n\n```go\nfunc (inv *Invocation) WithTestParsedFlags(\n\t_ testing.TB,\n\tparsedFlags *pflag.FlagSet,\n) *Invocation\n```\n\n```go\nfunc (inv *Invocation) WithTestSignalNotifyContext(\n\t_ testing.TB,\n\tf func(parent context.Context, signals ...os.Signal) (ctx context.Context, stop context.CancelFunc),\n) *Invocation\n```\nWithTestSignalNotifyContext allows overriding the default implementation of SignalNotifyContext. This should only be used in testing.\n\n```go\ntype MiddlewareFunc func(next HandlerFunc) HandlerFunc\n```\nMiddlewareFunc returns the next handler in the chain, or nil if there are no more.\n\n```go\nfunc Chain(ms ...MiddlewareFunc) MiddlewareFunc\n```\nChain returns a Handler that first calls middleware in order.\n\n```go\nfunc RequireNArgs(want int) MiddlewareFunc\n```\n\n```go\nfunc RequireRangeArgs(start, end int) MiddlewareFunc\n```\nRequireRangeArgs 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.\n\n```go\ntype Net interface {\n\t// Listen has the same semantics as `net.Listen` but also supports `udp`\n\tListen(network, address string) (net.Listener, error)\n}\n```\nNet abstracts CLI commands interacting with the operating system networking.\n\nAt present, it covers opening local listening sockets, since doing this in testing is a challenge without flakes, since it's hard to pick a port we know a priori will be free.\n\n```go\ntype NoOptDefValuer interface {\n\tNoOptDefValue() string\n}\n```\nNoOptDefValuer describes behavior when no option is passed into the flag.\n\nThis is useful for boolean or otherwise binary flags.\n\n```go\ntype Option struct {\n\tName        string `json:\"name,omitempty\"`\n\tDescription string `json:\"description,omitempty\"`\n\t// Required means this value must be set by some means. It requires\n\t// `ValueSource != ValueSourceNone`\n\t// If `Default` is set, then `Required` is ignored.\n\tRequired bool `json:\"required,omitempty\"`\n\n\t// Flag is the long name of the flag used to configure this option. If unset,\n\t// flag configuring is disabled.\n\tFlag string `json:\"flag,omitempty\"`\n\t// FlagShorthand is the one-character shorthand for the flag. If unset, no\n\t// shorthand is used.\n\tFlagShorthand string `json:\"flag_shorthand,omitempty\"`\n\n\t// Env is the environment variable used to configure this option. If unset,\n\t// environment configuring is disabled.\n\tEnv string `json:\"env,omitempty\"`\n\n\t// YAML is the YAML key used to configure this option. If unset, YAML\n\t// configuring is disabled.\n\tYAML string `json:\"yaml,omitempty\"`\n\n\t// Default is parsed into Value if set.\n\t// Must be `\"\"` if `DefaultFn` != nil\n\tDefault string `json:\"default,omitempty\"`\n\t// DefaultFn is called to compute the default value dynamically.\n\t// It is evaluated during SetDefaults(), after env/flag/yaml parsing,\n\t// but BEFORE static Default values are applied. This means if your\n\t// DefaultFn depends on another option, that option must get its value\n\t// from env, flag, or yaml - not from a static Default or DefaultFn.\n\t//\n\t// The result populates the Default field (for JSON marshalling) and\n\t// is applied to Value if no higher-priority source set it.\n\tDefaultFn func() string `json:\"-\"`\n\t// Value includes the types listed in values.go.\n\tValue pflag.Value `json:\"value,omitempty\"`\n\n\t// Annotations enable extensions to serpent higher up in the stack. It's useful for\n\t// help formatting and documentation generation.\n\tAnnotations Annotations `json:\"annotations,omitempty\"`\n\n\t// Group is a group hierarchy that helps organize this option in help, configs\n\t// and other documentation.\n\tGroup *Group `json:\"group,omitempty\"`\n\n\t// UseInstead is a list of options that should be used instead of this one.\n\t// The field is used to generate a deprecation warning.\n\tUseInstead []Option `json:\"use_instead,omitempty\"`\n\n\tHidden bool `json:\"hidden,omitempty\"`\n\n\tValueSource ValueSource `json:\"value_source,omitempty\"`\n\n\tCompletionHandler CompletionHandlerFunc `json:\"-\"`\n}\n```\nOption is a configuration option for a CLI application.\n\n```go\nfunc (o *Option) UnmarshalJSON(data []byte) error\n```\n\n```go\nfunc (o Option) YAMLPath() string\n```\n\n```go\ntype OptionSet []Option\n```\nOptionSet is a group of options that can be applied to a command.\n\n```go\nfunc (optSet *OptionSet) Add(opts ...Option)\n```\nAdd adds the given Options to the OptionSet.\n\n```go\nfunc (optSet OptionSet) ByFlag(flag string) *Option\n```\n\n```go\nfunc (optSet OptionSet) ByName(name string) *Option\n```\nByName returns the Option with the given name, or nil if no such option exists.\n\n```go\nfunc (optSet OptionSet) Filter(filter func(opt Option) bool) OptionSet\n```\nFilter will only return options that match the given filter. (return true)\n\n```go\nfunc (optSet *OptionSet) FlagSet() *pflag.FlagSet\n```\nFlagSet returns a pflag.FlagSet for the OptionSet.\n\n```go\nfunc (optSet *OptionSet) MarshalYAML() (any, error)\n```\nMarshalYAML converts the option set to a YAML node, that can be converted into bytes via yaml.Marshal.\n\nThe node is returned to enable post-processing higher up in the stack.\n\nIt is isomorphic with FromYAML.\n\n```go\nfunc (optSet *OptionSet) ParseEnv(vs []EnvVar) error\n```\nParseEnv parses the given environment variables into the OptionSet. Use EnvsWithPrefix to filter out prefixes.\n\n```go\nfunc (optSet *OptionSet) SetDefaults() error\n```\nSetDefaults sets the default values for each Option, skipping values that already have a value source.\n\n```go\nfunc (optSet *OptionSet) UnmarshalJSON(data []byte) error\n```\nUnmarshalJSON implements json.Unmarshaler for OptionSets. Options have an interface Value type that cannot handle unmarshalling because the types cannot be inferred. Since it is a slice, instantiating the Options first does not help.\n\nHowever, we typically do instantiate the slice to have the correct types. So this unmarshaller will attempt to find the named option in the existing set, if it cannot, the value is discarded. If the option exists, the value is unmarshalled into the existing option, and replaces the existing option.\n\nThe value is discarded if it's type cannot be inferred. This behavior just feels \"safer\", although it should never happen if the correct option set is passed in. The situation where this could occur is if a client and server are on different versions with different options.\n\n```go\nfunc (optSet *OptionSet) UnmarshalYAML(rootNode *yaml.Node) error\n```\nUnmarshalYAML converts the given YAML node into the option set. It is isomorphic with ToYAML.\n\n```go\ntype Regexp regexp.Regexp\n```\n\n```go\nfunc (r *Regexp) MarshalJSON() ([]byte, error)\n```\n\n```go\nfunc (r *Regexp) MarshalYAML() (interface{}, error)\n```\n\n```go\nfunc (r *Regexp) Set(v string) error\n```\n\n```go\nfunc (r Regexp) String() string\n```\n\n```go\nfunc (Regexp) Type() string\n```\n\n```go\nfunc (r *Regexp) UnmarshalJSON(data []byte) error\n```\n\n```go\nfunc (r *Regexp) UnmarshalYAML(n *yaml.Node) error\n```\n\n```go\nfunc (r *Regexp) Value() *regexp.Regexp\n```\n\n```go\ntype RunCommandError struct {\n\tCmd *Command\n\tErr error\n}\n```\n\n```go\nfunc (e *RunCommandError) Error() string\n```\n\n```go\nfunc (e *RunCommandError) Unwrap() error\n```\n\n```go\ntype String string\n```\n\n```go\nfunc StringOf(s *string) *String\n```\n\n```go\nfunc (*String) NoOptDefValue() string\n```\n\n```go\nfunc (s *String) Set(v string) error\n```\n\n```go\nfunc (s String) String() string\n```\n\n```go\nfunc (String) Type() string\n```\n\n```go\nfunc (s String) Value() string\n```\n\n```go\ntype StringArray []string\n```\nStringArray is a slice of strings that implements pflag.Value and pflag.SliceValue.\n\n```go\nfunc StringArrayOf(ss *[]string) *StringArray\n```\n\n```go\nfunc (s *StringArray) Append(v string) error\n```\n\n```go\nfunc (s *StringArray) GetSlice() []string\n```\n\n```go\nfunc (s *StringArray) Replace(vals []string) error\n```\n\n```go\nfunc (s *StringArray) Set(v string) error\n```\n\n```go\nfunc (s StringArray) String() string\n```\n\n```go\nfunc (StringArray) Type() string\n```\n\n```go\nfunc (s StringArray) Value() []string\n```\n\n```go\ntype Struct[T any] struct {\n\tValue T\n}\n```\nStruct 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.\n\nThe string encoding type is YAML.\n\n```go\nfunc (s *Struct[T]) MarshalJSON() ([]byte, error)\n```\nnolint:revive\n\n```go\nfunc (s *Struct[T]) MarshalYAML() (interface{}, error)\n```\nnolint:revive\n\n```go\nfunc (s *Struct[T]) Set(v string) error\n```\n\n```go\nfunc (s *Struct[T]) String() string\n```\n\n```go\nfunc (s *Struct[T]) Type() string\n```\n\n```go\nfunc (s *Struct[T]) UnmarshalJSON(b []byte) error\n```\nnolint:revive\n\n```go\nfunc (s *Struct[T]) UnmarshalYAML(n *yaml.Node) error\n```\nnolint:revive\n\n```go\ntype URL url.URL\n```\n\n```go\nfunc URLOf(u *url.URL) *URL\n```\n\n```go\nfunc (u *URL) MarshalJSON() ([]byte, error)\n```\n\n```go\nfunc (u *URL) MarshalYAML() (interface{}, error)\n```\n\n```go\nfunc (u *URL) Set(v string) error\n```\n\n```go\nfunc (u *URL) String() string\n```\n\n```go\nfunc (*URL) Type() string\n```\n\n```go\nfunc (u *URL) UnmarshalJSON(b []byte) error\n```\n\n```go\nfunc (u *URL) UnmarshalYAML(n *yaml.Node) error\n```\n\n```go\nfunc (u *URL) Value() *url.URL\n```\n\n```go\ntype UnknownSubcommandError struct {\n\tArgs []string\n}\n```\n\n```go\nfunc (e *UnknownSubcommandError) Error() string\n```\n\n```go\ntype Validator[T pflag.Value] struct {\n\tValue T\n\t// contains filtered or unexported fields\n}\n```\nValidator is a wrapper around a pflag.Value that allows for validation of the value after or before it has been set.\n\n```go\nfunc Validate[T pflag.Value](opt T, validate func(value T) error) *Validator[T]\n```\n\n```go\nfunc (i *Validator[T]) MarshalJSON() ([]byte, error)\n```\n\n```go\nfunc (i *Validator[T]) MarshalYAML() (interface{}, error)\n```\n\n```go\nfunc (i *Validator[T]) Set(input string) error\n```\n\n```go\nfunc (i *Validator[T]) String() string\n```\n\n```go\nfunc (i *Validator[T]) Type() string\n```\n\n```go\nfunc (i *Validator[T]) Underlying() pflag.Value\n```\n\n```go\nfunc (i *Validator[T]) UnmarshalJSON(b []byte) error\n```\n\n```go\nfunc (i *Validator[T]) UnmarshalYAML(n *yaml.Node) error\n```\n\n```go\ntype ValueSource string\n```\n\n```go\nconst (\n\tValueSourceNone    ValueSource = \"\"\n\tValueSourceFlag    ValueSource = \"flag\"\n\tValueSourceEnv     ValueSource = \"env\"\n\tValueSourceYAML    ValueSource = \"yaml\"\n\tValueSourceDefault ValueSource = \"default\"\n)\n```\n\n```go\ntype YAMLConfigPath string\n```\nYAMLConfigPath is a special value type that encodes a path to a YAML configuration file where options are read from.\n\n```go\nfunc (p *YAMLConfigPath) Set(v string) error\n```\n\n```go\nfunc (p *YAMLConfigPath) String() string\n```\n\n```go\nfunc (*YAMLConfigPath) Type() string\n```\n\n","path":"github.com/coder/serpent","name":"serpent","synopsis":"Package serpent offers an all-in-one solution for a highly configurable CLI application.","isRedistributable":true}
