Documentation ¶
Overview ¶
Package cli provides functionality for handling command-line arguments.
Package cli provides a command-line interface (CLI) framework for Go applications.
This package offers a set of utilities and abstractions to build command-line interfaces with ease. It includes features such as command parsing, flag handling, and subcommand support.
Usage: To use this package, import it in your Go code:
import "github.com/nandlabs/go-commons/cli"
Example: Here's a simple example that demonstrates how to use the `cli` package:
package main import ( "fmt" "github.com/nandlabs/go-commons/cli" ) func main() { app := cli.NewApp() app.Name = "myapp" app.Usage = "A simple CLI application" app.Action = func(c *cli.Context) error { fmt.Println("Hello, World!") return nil } err := app.Run(os.Args) if err != nil { fmt.Println(err) os.Exit(1) } }
For more information and examples, please refer to the package documentation at:
https://github.com/nandlabs/go-commons/cli
Index ¶
Constants ¶
This section is empty.
Variables ¶
var AppHelpTemplate = `` /* 242-byte string literal not displayed */
var CommandHelpTemplate = `` /* 170-byte string literal not displayed */
var HelpFlag = &Flag{ Name: "help", Usage: "show help", Aliases: []string{"-h", "--help"}, Default: "", }
HelpFlag is a built-in flag that represents the help flag.
var HelpFlags = [2]string{"--help", "-h"}
HelpFlags defines the flags used to display help.
var PrintCustomHelp helpCustomPrinter = printCustomHelp
PrintCustomHelp is a function that prints custom help information using the provided template, data, and custom functions.
var PrintHelp helpPrinter = printHelp
PrintHelp is a function that prints the help information using the provided template and data.
Functions ¶
func ShowAppHelp ¶
ShowAppHelp displays help information for the entire application.
func ShowCommandHelp ¶
ShowCommandHelp displays help information for a specific command.
Types ¶
type ActionFunc ¶
type App ¶
type App struct { // Name is the application name. Name string // Usage is the application usage information. Usage string // HelpName is the name used in the help command. HelpName string // ArgsUsage is the usage information for command arguments. ArgsUsage string // UsageText is the custom usage text for the application. UsageText string // Version is the application version. Version string // HideVersion determines whether to hide the version information. HideVersion bool // Action is the function to be invoked on default execution. Action ActionFunc // Flags are the global flags for the application. Flags []*Flag // Commands are the application commands. Commands []*Command // Writer is the output writer for the application. Writer io.Writer // HideHelp determines whether to hide the help command. HideHelp bool // HideHelpCommand determines whether to hide the help command in the list of commands. HideHelpCommand bool // CommandVisible determines whether the commands are visible. CommandVisible bool // contains filtered or unexported fields }
App represents a CLI application.
func (*App) ExecuteContext ¶
ExecuteContext executes the application with the given context and arguments.
func (*App) VisibleCommands ¶
VisibleCommands returns the visible commands of the application.
type ArgsCli ¶ added in v0.0.13
type ArgsCli struct {
// contains filtered or unexported fields
}
ArgsCli is a struct that holds the parsed command-line arguments.
type Command ¶
type Command struct { // command name used to invoke from CLI Name string // command usage information Usage string ArgsUsage string // the array of aliases to invoke the commands Aliases []string // execute on the command invocation Action ActionFunc // command specific flags Flags []*Flag // subcommands of the root command Commands []*Command HelpName string UsageText string SubCommandsAvailable bool }
func (*Command) VisibleCommands ¶
type Context ¶
type Context struct { context.Context App *App Command *Command // contains filtered or unexported fields }
func NewContext ¶
type Flag ¶ added in v0.0.13
type Flag struct { Name string // Name of the flag. Usage string // Usage description of the flag. Aliases []string // Aliases for the flag. Default interface{} // Default value of the flag. Value interface{} // Current value of the flag. }
Flag represents a command-line flag.
func (*Flag) AddFlagToSet ¶ added in v0.0.13
func (f *Flag) AddFlagToSet()
AddFlagToSet adds the flag to the flag set.
func (*Flag) AddHelpFlag ¶ added in v0.0.13
func (f *Flag) AddHelpFlag()
AddHelpFlag adds the help flag to the flag set.