Documentation
¶
Index ¶
- Constants
- type App
- func (a *App) Action(fn func(ctx *Context)) *App
- func (a *App) AddCommand(command *Command) *App
- func (a *App) AddGlobalFlag(flag FlagInfo) *App
- func (a *App) CommandHelp(cmd *Command) string
- func (a *App) Commands() []*Command
- func (a *App) Description() string
- func (a *App) GlobalFlags() []FlagInfo
- func (a *App) HandleMessage(messageType messageType, fn func(msgCtx MessageContext) string) *App
- func (a *App) Help() string
- func (a *App) MaxArg() int
- func (a *App) MinArg() int
- func (a *App) Name() string
- func (a *App) Run() int
- func (a *App) RunWithArgs(args []string) int
- func (a *App) Stderr() io.Writer
- func (a *App) Stdout() io.Writer
- func (a *App) Version() string
- func (a *App) WithDescription(description string) *App
- func (a *App) WithMaxArg(max int) *App
- func (a *App) WithMinArg(min int) *App
- func (a *App) WithStderr(err io.Writer) *App
- func (a *App) WithStdout(out io.Writer) *App
- func (a *App) WithVersion(version string) *App
- type CLIMessage
- type Command
- func (c *Command) Action(fn func(ctx *Context)) *Command
- func (c *Command) AddFlag(flag FlagInfo) *Command
- func (c *Command) AddSubcommand(subcommand *Command) *Command
- func (c *Command) Alias() string
- func (c *Command) Flags() []FlagInfo
- func (c *Command) Long() string
- func (c *Command) MaxArg() int
- func (c *Command) MinArg() int
- func (c *Command) Name() string
- func (c *Command) Parent() *Command
- func (c *Command) Short() string
- func (c *Command) Subcommands() []*Command
- func (c *Command) WithAlias(alias string) *Command
- func (c *Command) WithLong(long string) *Command
- func (c *Command) WithMaxArg(max int) *Command
- func (c *Command) WithMinArg(min int) *Command
- func (c *Command) WithShort(short string) *Command
- type Context
- func (c *Context) App() *App
- func (c *Context) Args() []string
- func (c *Context) Bool(name string) bool
- func (c *Context) Command() *Command
- func (c *Context) Flags() map[string]FlagValue
- func (c *Context) Float64(name string) float64
- func (c *Context) Int(name string) int
- func (c *Context) Lookup(name string) FlagValue
- func (c *Context) String(name string) string
- func (c *Context) StringSlice(name string) []string
- type Flag
- func NewBoolFlag(name string) *Flag[bool]
- func NewBoolFlagVar(name string, variable *bool) *Flag[bool]
- func NewCustomFlagVar(name string, variable FlagValue) *Flag[FlagValue]
- func NewFloatFlag(name string) *Flag[float64]
- func NewFloatFlagVar(name string, variable *float64) *Flag[float64]
- func NewIntFlag(name string) *Flag[int]
- func NewIntFlagVar(name string, variable *int) *Flag[int]
- func NewStringFlag(name string) *Flag[string]
- func NewStringFlagVar(name string, variable *string) *Flag[string]
- func NewStringSliceFlag(name string) *Flag[[]string]
- func NewStringSliceFlagVar(name string, variable *[]string) *Flag[[]string]
- func (f *Flag[T]) Alias() string
- func (f *Flag[T]) Description() string
- func (f *Flag[T]) Metavar() string
- func (f *Flag[T]) Name() string
- func (f *Flag[T]) Validate(ctx *Context) error
- func (f *Flag[T]) Value() FlagValue
- func (f *Flag[T]) WithAlias(alias string) *Flag[T]
- func (f *Flag[T]) WithDescription(description string) *Flag[T]
- func (f *Flag[T]) WithMetavar(metavar string) *Flag[T]
- func (f *Flag[T]) WithValidator(fn func(ctx *Context, value T) error) *Flag[T]
- type FlagInfo
- type FlagValue
- type MessageContext
Constants ¶
const ( MsgHelp messageType = iota MsgCommandHelp MsgVersion MsgNoCommand MsgUnknownCommand MsgSubcommandRequired MsgInvalidFlag MsgFlagValueMissing MsgUnexpectedArgument MsgTooFewArguments MsgTooManyArguments MsgUnsupportedFlagType )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type App ¶
type App struct {
// contains filtered or unexported fields
}
App represents the main CLI application. It manages application commands, global flags, configurations, and I/O streams.
func NewApp ¶ added in v0.2.0
NewApp creates and returns a new App instance with the given name and default settings.
func (*App) Action ¶ added in v0.3.0
Action assigns the default action to be executed when the application is run without specifying any command.
func (*App) AddCommand ¶ added in v0.2.0
AddCommand registers a top-level command to the application.
func (*App) AddGlobalFlag ¶ added in v0.3.0
AddGlobalFlag registers a global flag to the application. Global flags apply to all commands.
func (*App) CommandHelp ¶ added in v0.2.0
CommandHelp generates and returns a help menu for a specific command. It includes the full command path, command description, registered subcommands and flags.
func (*App) Description ¶ added in v0.2.0
Description returns the description of the application.
func (*App) GlobalFlags ¶ added in v0.3.0
GlobalFlags returns all registered global flags.
func (*App) HandleMessage ¶ added in v0.2.0
func (a *App) HandleMessage(messageType messageType, fn func(msgCtx MessageContext) string) *App
HandleMessage registers a custom message handler for a specific message type. The handler function runs whenever an event of the given type occurs.
func (*App) Help ¶ added in v0.2.0
Help generates and returns the global help menu for the application. It includes usage instructions, application description, registered commands and global flags.
func (*App) MaxArg ¶ added in v0.3.0
MaxArg returns the maximum number of positional arguments allowed for the application. If not set, it returns 0.
func (*App) MinArg ¶ added in v0.3.0
MinArg returns the minimum number of positional arguments required by the application. If not set, it returns 0.
func (*App) Run ¶
Run starts the application with os.Args. It is the main entry point when the CLI is executed.
func (*App) RunWithArgs ¶
RunWithArgs starts the application with a custom set of arguments. It is useful for testing or integrating the CLI with another program.
func (*App) Stderr ¶
Stderr returns the output writer used for standard error. If nil, it falls back to os.Stderr.
func (*App) Stdout ¶
Stdout returns the output writer used for standard output. If nil, it falls back to os.Stdout.
func (*App) WithDescription ¶ added in v0.2.0
WithDescription sets the description for the application. The description is shown in application help menu.
func (*App) WithMaxArg ¶ added in v0.3.0
WithMaxArg sets the maximum number of positional arguments allowed for the application.
func (*App) WithMinArg ¶ added in v0.3.0
WithMinArg sets the minimum number of positional arguments required by the application.
func (*App) WithStderr ¶ added in v0.2.0
WithStderr sets the writer used for error output.
func (*App) WithStdout ¶ added in v0.2.0
WithStdout sets the writer used for standard output.
func (*App) WithVersion ¶ added in v0.2.0
WithVersion sets the version for the application. This value is displayed when the version flag is used.
type CLIMessage ¶ added in v0.3.0
type CLIMessage struct {
// contains filtered or unexported fields
}
CLIMessage represents a message object used by the CLI. It includes an exit code, message, message type, and optional command pointer, as well as extra metadata.
func (*CLIMessage) Code ¶ added in v0.3.0
func (m *CLIMessage) Code() int
Code returns the exit code associated with the event.
func (*CLIMessage) Command ¶ added in v0.3.0
func (m *CLIMessage) Command() *Command
Command returns the command where the event occured.
func (*CLIMessage) Data ¶ added in v0.3.0
func (m *CLIMessage) Data() map[string]string
Data returns a map of metadata related to the event.
func (*CLIMessage) Message ¶ added in v0.3.0
func (m *CLIMessage) Message() string
Message returns the message of the event.
func (*CLIMessage) MessageType ¶ added in v0.3.0
func (m *CLIMessage) MessageType() messageType
MessageType returns the categorized type of the event.
type Command ¶
type Command struct {
// contains filtered or unexported fields
}
Command represents a single CLI command. It includes command name, alias, descriptions, subcommands, flags, argument constraints, and an action function called when the command is run.
func NewCommand ¶ added in v0.3.0
NewCommand creates a new command with the given name.
func (*Command) Action ¶ added in v0.2.0
Action assigns the function to be executed when the command is run. The handler receives a Context containing positional arguments and parsed flags.
func (*Command) AddSubcommand ¶ added in v0.2.0
AddSubcommand registers a subcommand to the current command.
func (*Command) Alias ¶
Alias returns the alias of the command. If not set, it returns an empty string.
func (*Command) Long ¶
Long returns the long description of the command. If not set, it returns an empty string.
func (*Command) MaxArg ¶
MaxArg returns the maximum number of positional arguments allowed for the command. If not set, it returns 0.
func (*Command) MinArg ¶
MinArg returns the minimum number of positional arguments required by the command. If not set, it returns 0.
func (*Command) Parent ¶ added in v0.2.0
Parent returns the parent command in the hierarchy. If the command has no parent command, it returns nil.
func (*Command) Short ¶
Short returns the short description of the command. If not set, it returns an empty string.
func (*Command) Subcommands ¶ added in v0.2.0
Subcommands returns all subcommands registered under the command.
func (*Command) WithAlias ¶ added in v0.2.0
WithAlias sets the short alias for the command. Alias provides alternative way for users to run the command.
func (*Command) WithLong ¶ added in v0.2.0
WithLong sets the detailed description for the command. This is shown in command help menu.
func (*Command) WithMaxArg ¶ added in v0.2.0
WithMaxArg sets the maximum number of positional arguments allowed for the command.
func (*Command) WithMinArg ¶ added in v0.2.0
WithMinArg sets the minimum number of positional arguments required by the command.
type Context ¶ added in v0.3.0
type Context struct {
// contains filtered or unexported fields
}
Context holds positional arguments and parsed flags for a command execution.
func (*Context) Bool ¶ added in v0.3.0
Bool returns the named flag as bool. It panics if not found or invalid type.
func (*Context) Float64 ¶ added in v0.3.0
Float64 returns the named flag as float64. It panics if not found or invalid type.
func (*Context) Int ¶ added in v0.3.0
Int returns the named flag as int. It panics if not found or invalid type.
func (*Context) Lookup ¶ added in v0.3.0
Lookup returns the FlagValue interface of the flag with the given name.
func (*Context) String ¶ added in v0.3.0
String returns the named flag as string. It panics if not found or invalid type.
func (*Context) StringSlice ¶ added in v0.3.0
StringSlice returns the named flag as []string. It panics if not found or invalid type.
type Flag ¶ added in v0.2.0
type Flag[T any] struct { // contains filtered or unexported fields }
Flag represents a single flag for the CLI. It includes flag name, alias, parsed value, description, metavariable, and an optional validator function.
func NewBoolFlag ¶ added in v0.3.0
NewBoolFlag creates a new bool flag with the given name.
func NewBoolFlagVar ¶ added in v0.3.0
NewBoolFlagVar creates a new bool flag using the provided variable.
func NewCustomFlagVar ¶ added in v0.3.0
NewCustomFlagVar creates a new flag using custom FlagValue implementations.
func NewFloatFlag ¶ added in v0.3.0
NewFloatFlag creates a new float64 flag with the given name.
func NewFloatFlagVar ¶ added in v0.3.0
NewFloatFlagVar creates a new float64 flag using the provided variable.
func NewIntFlag ¶ added in v0.3.0
NewIntFlag creates a new int flag with the given name.
func NewIntFlagVar ¶ added in v0.3.0
NewIntFlagVar creates a new int flag using the provided variable.
func NewStringFlag ¶ added in v0.3.0
NewStringFlag creates a new string flag with the given name.
func NewStringFlagVar ¶ added in v0.3.0
NewStringFlagVar creates a new string flag using the provided variable.
func NewStringSliceFlag ¶ added in v0.3.0
NewStringSliceFlag creates a new string slice flag with the given name.
func NewStringSliceFlagVar ¶ added in v0.3.0
NewStringSliceFlagVar creates a new string slice flag using the provided variable.
func (*Flag[T]) Alias ¶ added in v0.2.0
Alias returns the alias of the flag. If not set, it returns an empty string.
func (*Flag[T]) Description ¶ added in v0.2.0
Description returns the description of the flag. If not set, it returns an empty string.
func (*Flag[T]) WithDescription ¶ added in v0.3.0
WithDescription sets the description for the flag. This is shown in flags section within help menu.
func (*Flag[T]) WithMetavar ¶ added in v0.3.0
WithMetavar sets the metavariable for the flag. This is shown next to the flag and indicates the type of the flag value.
type FlagInfo ¶ added in v0.3.0
type FlagInfo interface {
Name() string // Name returns the name of the flag.
Alias() string // Alias returns the optional alias of the flag.
Value() FlagValue // Value returns the parsed value of the flag.
Description() string // Description returns the description of the flag.
Metavar() string // Metavar returns the metavariable of the flag.
Validate(ctx *Context) error // Validate runs the flag validator function, if set.
}
FlagInfo provides access to flag metadata and behaviour.
type FlagValue ¶ added in v0.3.0
type FlagValue interface {
Set(value string) error // Set parses and assigns the given string to the underlying typed value.
Get() any // Get returns the underlying typed value.
String() string // String returns the string representation of the value.
}
FlagValue defines an interface for all flag values.
type MessageContext ¶ added in v0.3.0
type MessageContext struct {
// contains filtered or unexported fields
}
MessageContext holds the application instance and message object providing details about the event.
func (*MessageContext) App ¶ added in v0.3.0
func (m *MessageContext) App() *App
App returns the application instance.
func (*MessageContext) Msg ¶ added in v0.3.0
func (m *MessageContext) Msg() *CLIMessage
Msg returns the message object providing details about the event.