Documentation
¶
Overview ¶
Package clip is a library for building command-line applications.
Index ¶
- func CommandHidden(c *commandConfig)
- func FlagHidden(c *flagConfig)
- func NewExitError(code int, message string) error
- func NewExitErrorf(code int, format string, a ...any) error
- func WithExitCode(code int, err error) error
- func WriteHelp(w io.Writer, ctx *Context) error
- type Command
- type CommandOption
- func BoolFlag(value *bool, name string, options ...FlagOption) CommandOption
- func CommandAction(action func(*Context) error) CommandOption
- func CommandDescription(description string) CommandOption
- func CommandStderr(writer io.Writer) CommandOption
- func CommandStdout(writer io.Writer) CommandOption
- func CommandSummary(summary string) CommandOption
- func StringFlag(value *string, name string, options ...FlagOption) CommandOption
- func SubCommand(name string, options ...CommandOption) CommandOption
- func TextVarFlag(value TextVar, name string, options ...FlagOption) CommandOption
- func ToggleFlag(name string, options ...FlagOption) CommandOption
- type Context
- type FlagOption
- func FlagAction(action func(*Context) error) FlagOption
- func FlagDeprecated(derepcation string) FlagOption
- func FlagDescription(description string) FlagOption
- func FlagEnv(env ...string) FlagOption
- func FlagHelpDefault(value string) FlagOption
- func FlagPlaceholder(name string) FlagOption
- func FlagShort(name string) FlagOption
- type TextVar
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CommandHidden ¶
func CommandHidden(c *commandConfig)
CommandHidden hides a command from documentation.
func NewExitError ¶
NewExitError creates an error with an associated exit code.
func NewExitErrorf ¶
NewExitErrorf creates an error with an associated exit code using fmt.Errorf semantics.
func WithExitCode ¶
WithExitCode wraps an existing error with an associated exit code.
Types ¶
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
Command is a command or sub-command that can be run from the command-line.
To create a new command with the default settings, use NewCommand.
func NewCommand ¶
func NewCommand(name string, options ...CommandOption) *Command
NewCommand creates a new command given a name and command options.
By default, commands will print their help documentation when invoked.
func (*Command) Description ¶
Description is a multi-line description of the command.
func (*Command) Execute ¶
Execute runs a command using given args and returns the raw error.
This function provides more fine-grained control than Run, and can be used in situations where handling arguments or errors needs more granular control.
type CommandOption ¶
type CommandOption func(*commandConfig)
An CommandOption is used to commandConfigure a new command.
func BoolFlag ¶
func BoolFlag(value *bool, name string, options ...FlagOption) CommandOption
BoolFlag creates a new boolean flag.
func CommandAction ¶
func CommandAction(action func(*Context) error) CommandOption
CommandAction sets a Command's behavior when invoked.
func CommandDescription ¶
func CommandDescription(description string) CommandOption
CommandDescription adds a multi-line description to a command.
func CommandStderr ¶
func CommandStderr(writer io.Writer) CommandOption
CommandStderr sets the writer for command error output.
func CommandStdout ¶
func CommandStdout(writer io.Writer) CommandOption
CommandStdout sets the writer for command output.
func CommandSummary ¶
func CommandSummary(summary string) CommandOption
CommandSummary adds a one-line description to a command.
func StringFlag ¶
func StringFlag(value *string, name string, options ...FlagOption) CommandOption
StringFlag creates a new string flag.
func SubCommand ¶
func SubCommand(name string, options ...CommandOption) CommandOption
SubCommand adds a sub-command.
func TextVarFlag ¶
func TextVarFlag(value TextVar, name string, options ...FlagOption) CommandOption
TextVarFlag creates a new flag based on encoding.TextMarshaler and encoding.TextUnmarshaler.
func ToggleFlag ¶
func ToggleFlag(name string, options ...FlagOption) CommandOption
ToggleFlag creates a new toggle flag.
Toggle flags have no associated value, but can be passed like boolean flags to toggle something on. This is the simplest way to create an action flag.
type Context ¶
type Context struct {
// contains filtered or unexported fields
}
Context is a command context with runtime metadata.
func (*Context) Description ¶
Description is a multi-line description of the command.
type FlagOption ¶
type FlagOption func(*flagConfig)
FlagOption is an option for creating a Flag.
func FlagAction ¶
func FlagAction(action func(*Context) error) FlagOption
FlagAction sets the behavior of the flag to replace the command's action when set.
The action will occur if the flag is passed, regardless of the value.
func FlagDeprecated ¶
func FlagDeprecated(derepcation string) FlagOption
FlagDeprecated adds a deprecation to a flag.
func FlagDescription ¶
func FlagDescription(description string) FlagOption
FlagDescription adds a description to a flag.
Descriptions can span multiple lines.
func FlagEnv ¶
func FlagEnv(env ...string) FlagOption
FlagEnv sets the list of environment variables for a flag.
Successive calls will replace earlier values.
func FlagHelpDefault ¶
func FlagHelpDefault(value string) FlagOption
FlagHelpDefault sets the default value of a flag in help docs.
Help text will display non-zero values when possible. To disable, pass an empty string to this function.
func FlagPlaceholder ¶
func FlagPlaceholder(name string) FlagOption
FlagPlaceholder sets the name of the flag's placeholder value.
func FlagShort ¶
func FlagShort(name string) FlagOption
FlagShort adds a short name to a flag. Panics if the name is not exactly one ASCII character.
type TextVar ¶
type TextVar interface {
encoding.TextMarshaler
encoding.TextUnmarshaler
}
TextVar can be marshaled to and from text.
This is the recommended interface to implement for custom types.