clibase

package
v0.19.1 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 10, 2023 License: AGPL-3.0 Imports: 16 Imported by: 0

Documentation

Overview

Package clibase offers an all-in-one solution for a highly configurable CLI application. Within Coder, we use it for our `server` subcommand, which demands more functionality than cobra/viper can offer.

We will extend its usage to the rest of our application, completely replacing cobra/viper. It's also a candidate to be broken out into its own open-source library, so we avoid deep coupling with Coder concepts.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Annotations

type Annotations map[string]string

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 Bool

type Bool bool

func (*Bool) NoOptDefValue

func (*Bool) NoOptDefValue() string

func (*Bool) Set

func (b *Bool) Set(s string) error

func (Bool) String

func (b Bool) String() string

func (Bool) Type

func (Bool) Type() string

func (Bool) Value

func (b Bool) Value() bool

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
	// Short is a one-line description of the command.
	Short string
	// Long is a detailed description of the command,
	// presented on its help page. It may contain examples.
	Long        string
	Options     OptionSet
	Annotations Annotations
}

Cmd describes an executable command.

func (*Cmd) FullName

func (c *Cmd) FullName() string

FullName returns the full invocation name of the command, as seen on the command line.

func (*Cmd) FullUsage

func (c *Cmd) FullUsage() string

FullName returns usage of the command, preceded by the usage of its parents.

func (*Cmd) Name

func (c *Cmd) Name() string

Name returns the first word in the Use string.

type DiscardValue

type DiscardValue struct{}

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.

func (DiscardValue) Set

func (DiscardValue) Set(string) error

func (DiscardValue) String

func (DiscardValue) String() string

func (DiscardValue) Type

func (DiscardValue) Type() string

type Duration

type Duration time.Duration

func (*Duration) MarshalJSON

func (d *Duration) MarshalJSON() ([]byte, error)

func (*Duration) Set

func (d *Duration) Set(v string) error

func (*Duration) String

func (d *Duration) String() string

func (Duration) Type

func (Duration) Type() string

func (*Duration) UnmarshalJSON

func (d *Duration) UnmarshalJSON(b []byte) error

func (*Duration) Value

func (d *Duration) Value() time.Duration

type EnvVar

type EnvVar struct {
	Name  string
	Value string
}

Var represents a single environment variable of form NAME=VALUE.

func EnvsWithPrefix

func EnvsWithPrefix(environ []string, prefix string) []EnvVar

EnvsWithPrefix 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"`
	Children    []Group `json:"children,omitempty"`
	Description string  `json:"description,omitempty"`
}

Group describes a hierarchy of groups that an option or command belongs to.

func (*Group) AddChild

func (g *Group) AddChild(child Group)

func (*Group) Ancestry

func (g *Group) Ancestry() []Group

Ancestry returns the group and all of its parents, in order.

func (*Group) FullName

func (g *Group) FullName() string

type HostPort

type HostPort struct {
	Host string
	Port string
}

HostPort is a host:port pair.

func (*HostPort) MarshalJSON

func (hp *HostPort) MarshalJSON() ([]byte, error)

func (*HostPort) Set

func (hp *HostPort) Set(v string) error

func (*HostPort) String

func (hp *HostPort) String() string

func (*HostPort) Type

func (*HostPort) Type() string

func (*HostPort) UnmarshalJSON

func (hp *HostPort) UnmarshalJSON(b []byte) error

type Int64

type Int64 int64

func (*Int64) Set

func (i *Int64) Set(s string) error

func (Int64) String

func (i Int64) String() string

func (Int64) Type

func (Int64) Type() string

func (Int64) Value

func (i Int64) Value() int64

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"`
}

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) Add

func (s *OptionSet) Add(opts ...Option)

Add adds the given Options to the OptionSet.

func (*OptionSet) FlagSet

func (s *OptionSet) FlagSet() *pflag.FlagSet

FlagSet returns a pflag.FlagSet for the OptionSet.

func (*OptionSet) ParseEnv

func (s *OptionSet) ParseEnv(globalPrefix string, environ []string) error

ParseEnv parses the given environment variables into the OptionSet.

func (*OptionSet) SetDefaults

func (s *OptionSet) SetDefaults() error

SetDefaults sets the default values for each Option. It should be called before all parsing (e.g. ParseFlags, ParseEnv).

func (OptionSet) ToYAML

func (s OptionSet) ToYAML() (*yaml.Node, error)

ToYAML 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.

type String

type String string

func (*String) NoOptDefValue

func (*String) NoOptDefValue() string

func (*String) Set

func (s *String) Set(v string) error

func (String) String

func (s String) String() string

func (String) Type

func (String) Type() string

func (String) Value

func (s String) Value() string

type Strings

type Strings []string

Strings is a slice of strings that implements pflag.Value and pflag.SliceValue.

func (*Strings) Append

func (s *Strings) Append(v string) error

func (*Strings) GetSlice

func (s *Strings) GetSlice() []string

func (*Strings) Replace

func (s *Strings) Replace(vals []string) error

func (*Strings) Set

func (s *Strings) Set(v string) error

func (Strings) String

func (s Strings) String() string

func (Strings) Type

func (Strings) Type() string

func (Strings) Value

func (s Strings) 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 (s *Struct[T]) MarshalJSON() ([]byte, error)

func (*Struct[T]) MarshalYAML

func (s *Struct[T]) MarshalYAML() (interface{}, error)

func (*Struct[T]) Set

func (s *Struct[T]) Set(v string) error

func (*Struct[T]) String

func (s *Struct[T]) String() string

func (*Struct[T]) Type

func (s *Struct[T]) Type() string

func (*Struct[T]) UnmarshalJSON

func (s *Struct[T]) UnmarshalJSON(b []byte) error

func (*Struct[T]) UnmarshalYAML

func (s *Struct[T]) UnmarshalYAML(n *yaml.Node) error

type URL

type URL url.URL

func (*URL) MarshalJSON

func (u *URL) MarshalJSON() ([]byte, error)

func (*URL) Set

func (u *URL) Set(v string) error

func (*URL) String

func (u *URL) String() string

func (*URL) Type

func (*URL) Type() string

func (*URL) UnmarshalJSON

func (u *URL) UnmarshalJSON(b []byte) error

func (*URL) Value

func (u *URL) Value() *url.URL

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL