Documentation
¶
Overview ¶
Package cli provides a lightweight CLI framework with support for positional arguments, named parameters, and flags.
Index ¶
- func Confirm(questionOpt ...string) (ok bool)
- func Err(s string, args ...any)
- func Gray(s string, args ...any) string
- func Info(s string, args ...any)
- func OK(s string, args ...any)
- type App
- type Command
- type Handler
- type SampleCommandWithFeaturesCmd
- type SampleCommandWithFlagsCmd
- type SampleCommandWithNamedParamsCmd
- type SampleCommandWithSignatureCmd
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Confirm ¶
Confirm asks for y/n confirmation from the user via stdin. It returns true if the user enters 'y' or 'yes' (case-insensitive).
func Err ¶
Err prints an error message to stdout in red color. Used to highlight errors or critical problems for the user.
Types ¶
type App ¶
App represents the main CLI application structure.
func (*App) PromptOrRun ¶
func (a *App) PromptOrRun()
PromptOrRun executes the CLI either from provided command-line arguments or starts interactive mode. If arguments are provided (beyond the binary name), it runs the specified command. Otherwise, it enters the interactive mode.
func (*App) WaitForCommand ¶
func (a *App) WaitForCommand()
WaitForCommand starts the interactive CLI loop waiting for user input.
type Command ¶
type Command struct {
Name string
Alias string
Help []string
Handler Handler
// contains filtered or unexported fields
}
Command represents a CLI command configuration.
func NewSampleCommandWithFeaturesCmd ¶
func NewSampleCommandWithFeaturesCmd() Command
NewSampleCommandWithFeaturesCmd returns a sample command that demonstrates: - positional arguments - named options (-key=value) - boolean flags
Example (without optional arguments):
htf my-proj PROD -token=do-not-hack-me -vars='foo="bar",bar="beer"'
func NewSampleCommandWithFlagsCmd ¶
func NewSampleCommandWithFlagsCmd() Command
NewSampleCommandWithFlagsCmd creates a new command instance demonstrating flag usage. Usage: how_to_flags [env] [-v] [-y].
func NewSampleCommandWithNamedParamsCmd ¶
func NewSampleCommandWithNamedParamsCmd() Command
NewSampleCommandWithNamedParamsCmd creates a new command instance demonstrating named parameters.
func NewSampleCommandWithSignatureCmd ¶
func NewSampleCommandWithSignatureCmd() Command
NewSampleCommandWithSignatureCmd creates a new command instance for demonstrating signature-based commands.
type SampleCommandWithFeaturesCmd ¶
type SampleCommandWithFeaturesCmd struct {
// Project is a required positional argument.
Project string `arg:"project"`
// Env is an optional positional argument (default: STAGING).
Env *string `arg:"env" default:"STAGING"`
// Named options: provided as {name}={value}.
// Unlike positional arguments, they can appear in any order.
AuthToken string `arg:"-token"` //nolint:gosec
TmpPath *string `arg:"-tmp-path" default:"/tmp/"`
Variables []string `arg:"-vars"`
Timeout *int `arg:"-t" default:"300"`
// Flags: simple switches. If a flag is present, its value becomes true.
// They can appear in any order.
Verbose bool `arg:"-v"`
Confirm bool `arg:"-y"`
}
SampleCommandWithFeaturesCmd demonstrates a complex command with various argument types.
type SampleCommandWithFlagsCmd ¶
type SampleCommandWithFlagsCmd struct {
Env string `arg:"env" default:"STAGING"`
Verbose bool `arg:"-v"`
Confirm bool `arg:"-y"`
}
SampleCommandWithFlagsCmd demonstrates a command with boolean flags.
type SampleCommandWithNamedParamsCmd ¶
type SampleCommandWithNamedParamsCmd struct {
Name string `arg:"name"`
Mood *string `arg:"-m"`
Age *int `arg:"-a"`
}
SampleCommandWithNamedParamsCmd demonstrates a command with named parameters.
type SampleCommandWithSignatureCmd ¶
type SampleCommandWithSignatureCmd struct {
Name string `arg:"name"`
Mood *string `arg:"mood" default:"Happy"`
Age *int `arg:"age"`
}
SampleCommandWithSignatureCmd demonstrates a command with positional arguments.