cli

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jul 3, 2025 License: MPL-2.0 Imports: 8 Imported by: 2

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ErrNotImplemented = errors.New("not implemented")
	ErrNoArgsAllowed  = errors.New("no args allowed")
)

Functions

func FlagFuncsAll

func FlagFuncsAll(fs *flag.FlagSet, flagFuncs ...func(*flag.FlagSet))

func FlagsAll

func FlagsAll(fs *flag.FlagSet, objs ...Flags)

func InitAll

func InitAll(objs ...Init) error

func ReadEnvAll

func ReadEnvAll(objs ...Env) error

func TabWrite

func TabWrite[T any](w io.Writer, data []T, printer func(datum T) string) error

Types

type Arg

type Arg struct {
	// contains filtered or unexported fields
}

type ArgDefiner

type ArgDefiner interface {
	Args(*ArgList)
}

type ArgList

type ArgList []Arg

func (*ArgList) Optional

func (al *ArgList) Optional(val *string, name, defaultVal string)

func (*ArgList) OptionalVariadic

func (al *ArgList) OptionalVariadic(vals *[]string, name string, defaultVals ...string)

func (*ArgList) Required

func (al *ArgList) Required(val *string, name string)

func (*ArgList) RequiredVariadic

func (al *ArgList) RequiredVariadic(vals *[]string, name string, minimumVals int)

type Args

type Args interface {
	ParseArgs([]string) error
}

type Command

type Command struct {
	// contains filtered or unexported fields
}

Command represents a command in the CLI graph. Don't construct Commands manually, instead use the RootCommand and LeafCommand functions to construct root and leaf commands.

func LeafCommand

func LeafCommand[T any](name, desc string, run func(opts *T) error) *Command

LeafCommand is a command that runs something. The run function can accept a opts argument, which should be a pointer to a type that implements Flags or Args, or both. If no flags or args are needed, it can accept None instead.

When the command is run, a new instance of opts (*T) is created first, then its flags and args are handled. The resultant opts is passed to the run function.

If opts implements Flags, then its flags are registered with the flag set. The Flag set is parsed before the args. If opts implements Args, then its ParseArgs method is called on args remaining after the flag set is parsed. If opts implements Env, then its ReadEnv method is called to populate it with config from the environment. The run function is called after flags and args have been parsed, and passed the resultant opts.

func RootCommand

func RootCommand(name, desc string, subcommands ...*Command) *Command

RootCommand is a command that only contains subcommands and doesn't do anything by itself.

func (*Command) Args

func (c *Command) Args() Args

func (*Command) Description

func (c *Command) Description() string

func (*Command) Env

func (c *Command) Env() Env

func (*Command) Execute

func (c *Command) Execute(args []string) error

Execute should be called on the root command, it is the starting point for evaluating args and routing to the requested command.

func (*Command) Flags

func (c *Command) Flags() Flags

func (*Command) Help

func (c *Command) Help() string

func (*Command) Init

func (c *Command) Init() Init

func (*Command) Name

func (c *Command) Name() string

func (*Command) Path

func (c *Command) Path() []string

func (*Command) PathString

func (c *Command) PathString() string

func (*Command) Run

func (c *Command) Run() func() error

func (*Command) SetStderr

func (c *Command) SetStderr(w io.Writer)

func (*Command) SetStdin

func (c *Command) SetStdin(r io.Reader)

func (*Command) SetStdout

func (c *Command) SetStdout(w io.Writer)

func (*Command) Subcommands

func (c *Command) Subcommands() []*Command

func (*Command) Synopsis

func (c *Command) Synopsis() string

func (*Command) Usage

func (c *Command) Usage() string

func (*Command) WithHelp

func (c *Command) WithHelp(h string) *Command

type Env

type Env interface {
	ReadEnv() error
}

Env should be implemented by options structs that read from the environment.

type FlagHider

type FlagHider interface {
	HideFlags() []string
}

type Flags

type Flags interface {
	Flags(*flag.FlagSet)
}

Flags represents a type that sets options based on a set of command line flags.

type Init

type Init interface {
	Init() error
}

type None

type None = *any

None can be used as the parameter for leaf commands that have no options.

Jump to

Keyboard shortcuts

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