cli

package
v1.2.6 Latest Latest
Warning

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

Go to latest
Published: Oct 8, 2023 License: MIT Imports: 12 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var HelpTemplate = `` /* 724-byte string literal not displayed */

HelpTemplate is the text template for the Default help topic. go-cli uses text/template to render templates. You can render custom help text by setting this variable.

Functions

This section is empty.

Types

type App

type App struct {
	// The name of the program. Defaults to path.Base(os.Args[0])
	Name string
	// The version of the program
	Version string
	// Short description of the program.
	Usage string
	// Text to override the USAGE section of help
	UsageText string
	// Long description of the program
	Description string
	// Authors of the program
	Authors string
	// Examples of the program
	Examples string
	// SeeAlso of the program
	SeeAlso string

	// build information, show in --version
	BuildInfo *BuildInfo

	// List of flags to parse
	Flags []*Flag
	// List of commands to execute
	Commands []*Command

	// Hidden --help and --version from usage
	HiddenHelp    bool
	HiddenVersion bool

	// Display full help
	ShowHelp func(*HelpContext)
	// Display full version
	ShowVersion func(*App)

	// The action to execute when no subcommands are specified
	Action func(*Context)

	// Execute this function if the proper command cannot be found
	OnCommandNotFound func(*Context, string)

	// Handler if panic in app.Action() and command.Action()
	OnActionPanic func(*Context, error)
}

App is the main structure of a cli application

func NewApp

func NewApp() *App

NewApp creates a new cli Application

func (*App) Run

func (a *App) Run(arguments []string)

Run is the entry point to the cli app, parse argument and call Execute() or command.Execute()

type BuildInfo

type BuildInfo struct {
	Timestamp   string
	GitBranch   string
	GitCommit   string
	GitRevCount string
}

BuildInfo stores app build info

func ParseBuildInfo

func ParseBuildInfo(info string) *BuildInfo

ParseBuildInfo parse a buildinfo string info struct

type Command

type Command struct {
	// The name of the program. Defaults to path.Base(os.Args[0])
	Name string
	// Short description of the program.
	Usage string
	// Text to override the USAGE section of help
	UsageText string
	// Long description of the program
	Description string
	// Examples of the program
	Examples string
	// SeeAlso of the program
	SeeAlso string

	// List of flags to parse
	Flags []*Flag
	// List of commands to execute
	Commands []*Command

	// hidden --help from usage
	HiddenHelp bool

	// Treat all flags as normal arguments if true
	SkipFlagParsing bool

	// Boolean to hide this command from help
	Hidden bool

	// Display full help
	ShowHelp func(*HelpContext)

	// The action to execute when no subcommands are specified
	Action func(*Context)

	// Execute this function if the proper command cannot be found
	OnCommandNotFound func(*Context, string)
}

Command is a subcommand for a cli.App

func (*Command) Names

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

Names returns the names including short names and aliases

func (*Command) Run

func (c *Command) Run(ctx *Context)

Run is the entry point to the command, parse argument and call Execute() or subcommand.Execute()

type Context

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

Context is a type that is passed through to each Handler action in a cli application. Context can be used to retrieve context-specific Args and parsed command-line options.

func (*Context) Arg

func (c *Context) Arg(n int) string

Arg returns the i'th non-flag argument

func (*Context) Args

func (c *Context) Args() []string

Args returns the non-flag arguments.

func (*Context) GetBool

func (c *Context) GetBool(name string) bool

GetBool returns flag value as bool

func (*Context) GetFloat32

func (c *Context) GetFloat32(name string) float32

GetFloat32 returns flag value as float32

func (*Context) GetFloat64

func (c *Context) GetFloat64(name string) float64

GetFloat64 returns flag value as float64

func (*Context) GetInt

func (c *Context) GetInt(name string) int

GetInt returns flag value as int

func (*Context) GetInt16

func (c *Context) GetInt16(name string) int16

GetInt16 returns flag value as int16

func (*Context) GetInt32

func (c *Context) GetInt32(name string) int32

GetInt32 returns flag value as int32

func (*Context) GetInt64

func (c *Context) GetInt64(name string) int64

GetInt64 returns flag value as int64

func (*Context) GetInt8

func (c *Context) GetInt8(name string) int8

GetInt8 returns flag value as int8

func (*Context) GetString

func (c *Context) GetString(name string) string

GetString returns flag value as string

func (*Context) GetStringSlice

func (c *Context) GetStringSlice(name string) []string

GetStringSlice returns flag value as string slice

func (*Context) GetUint

func (c *Context) GetUint(name string) uint

GetUint returns flag value as uint

func (*Context) GetUint16

func (c *Context) GetUint16(name string) uint16

GetUint16 returns flag value as uint16

func (*Context) GetUint32

func (c *Context) GetUint32(name string) uint32

GetUint32 returns flag value as uint32

func (*Context) GetUint64

func (c *Context) GetUint64(name string) uint64

GetUint64 returns flag value as uint64

func (*Context) GetUint8

func (c *Context) GetUint8(name string) uint8

GetUint8 returns flag value as uint8

func (*Context) Global

func (c *Context) Global() *Context

Global returns top context if exists

func (*Context) IsSet

func (c *Context) IsSet(name string) bool

IsSet returns flag is visited in cli args

func (*Context) NArg

func (c *Context) NArg() int

NArg returns number of non-flag arguments

func (*Context) Name

func (c *Context) Name() string

Name returns app/command full name

func (*Context) Parent

func (c *Context) Parent() *Context

Parent returns parent context if exists

func (*Context) ShowError

func (c *Context) ShowError(err error)

ShowError shows error and exit(1)

func (*Context) ShowHelp

func (c *Context) ShowHelp()

ShowHelp shows help and

func (*Context) ShowHelpAndExit

func (c *Context) ShowHelpAndExit(code int)

ShowHelpAndExit shows help and exit

type Flag

type Flag struct {
	Name        string // name as it appears on command line
	Usage       string // help message
	Placeholder string // placeholder in usage
	Hidden      bool   // allow flags to be hidden from help/usage text

	IsBool        bool   // if the flag is bool value
	DefValue      string // default value (as text); for usage message
	NoOptDefValue string // default value (as text); if the flag is on the command line without any options
	EnvVar        string // default value load from environ

	Value interface{} // returns final value
	// contains filtered or unexported fields
}

Flag represents the state of a flag

func (*Flag) GetValue

func (f *Flag) GetValue() string

GetValue returns the string value of flag

func (*Flag) Names

func (f *Flag) Names() []string

Names returns the names including short names and aliases

func (*Flag) SetValue

func (f *Flag) SetValue(value string) error

SetValue sets the value of the named flag

type HelpContext

type HelpContext struct {
	Name        string
	Version     string
	Usage       string
	UsageText   string
	Description string
	Authors     string
	Examples    string
	SeeAlso     string
	Flags       []*Flag
	Commands    []*Command
}

HelpContext is a struct for output help

func (*HelpContext) AuthorLines

func (c *HelpContext) AuthorLines() []string

AuthorLines splits line for authors

func (*HelpContext) ExampleLines

func (c *HelpContext) ExampleLines() []string

ExampleLines splits line for examples

func (*HelpContext) Level

func (c *HelpContext) Level() int

Level return command/subcommand's level

func (*HelpContext) SeeAlsoLines

func (c *HelpContext) SeeAlsoLines() []string

SeeAlsoLines splits line for see also

func (*HelpContext) UsageTextLines

func (c *HelpContext) UsageTextLines() []string

UsageTextLines splits line for usage

func (*HelpContext) VisibleCommands

func (c *HelpContext) VisibleCommands() []*Command

VisibleCommands returns commands which are visible

func (*HelpContext) VisibleCommandsUsageLines

func (c *HelpContext) VisibleCommandsUsageLines() []string

VisibleCommandsUsageLines splits line for commands

func (*HelpContext) VisibleFlags

func (c *HelpContext) VisibleFlags() []*Flag

VisibleFlags returns flags which are visible

func (*HelpContext) VisibleFlagsUsageLines

func (c *HelpContext) VisibleFlagsUsageLines() []string

VisibleFlagsUsageLines splits line for flags

type Value

type Value interface {
	String() string
	Set(string) error
}

Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)

Jump to

Keyboard shortcuts

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