Documentation ¶
Overview ¶
Package kingpin provides command line interfaces like this:
$ chat usage: chat [<flags>] <command> [<flags>] [<args> ...] Flags: --debug enable debug mode --help Show help. --server=127.0.0.1 server address Commands: help <command> Show help for a command. post [<flags>] <channel> Post a message to a channel. register <nick> <name> Register a new user. $ chat help post usage: chat [<flags>] post [<flags>] <channel> [<text>] Post a message to a channel. Flags: --image=IMAGE image to post Args: <channel> channel to post to [<text>] text to post $ chat post --image=~/Downloads/owls.jpg pics
From code like this:
package main import "gopkg.in/alecthomas/kingpin.v1" var ( debug = kingpin.Flag("debug", "enable debug mode").Default("false").Bool() serverIP = kingpin.Flag("server", "server address").Default("127.0.0.1").IP() register = kingpin.Command("register", "Register a new user.") registerNick = register.Arg("nick", "nickname for user").Required().String() registerName = register.Arg("name", "name of user").Required().String() post = kingpin.Command("post", "Post a message to a channel.") postImage = post.Flag("image", "image to post").ExistingFile() postChannel = post.Arg("channel", "channel to post to").Required().String() postText = post.Arg("text", "text to post").String() ) func main() { switch kingpin.Parse() { // Register user case "register": println(*registerNick) // Post message case "post": if *postImage != nil { } if *postText != "" { } } }
Index ¶
- Variables
- func Errorf(format string, args ...interface{})
- func ExpandArgsFromFile(filename string) (out []string, err error)
- func FatalIfError(err error, prefix string)
- func FatalUsage(format string, args ...interface{})
- func FatalUsageContext(context *ParseContext, format string, args ...interface{})
- func Fatalf(format string, args ...interface{})
- func MustParse(command string, err error) string
- func Parse() string
- func Usage()
- type Action
- type Application
- func (a *Application) Action(action Action) *Application
- func (a Application) Arg(name, help string) *ArgClause
- func (a *Application) Author(author string) *Application
- func (a *Application) Command(name, help string) *CmdClause
- func (a *Application) Errorf(format string, args ...interface{})
- func (a *Application) FatalIfError(err error, format string, args ...interface{})
- func (a *Application) FatalUsage(format string, args ...interface{})
- func (a *Application) FatalUsageContext(context *ParseContext, format string, args ...interface{})
- func (a *Application) Fatalf(format string, args ...interface{})
- func (f Application) Flag(name, help string) *FlagClause
- func (a *Application) Model() *ApplicationModel
- func (a *Application) Parse(args []string) (command string, err error)
- func (a *Application) ParseContext(args []string) (*ParseContext, error)
- func (a *Application) PreAction(action Action) *Application
- func (a *Application) Terminate(terminate func(int)) *Application
- func (a *Application) Usage(args []string)
- func (a *Application) UsageForContext(context *ParseContext) error
- func (a *Application) UsageForContextWithTemplate(context *ParseContext, indent int, tmpl string) error
- func (a *Application) UsageTemplate(template string) *Application
- func (a *Application) Validate(validator ApplicationValidator) *Application
- func (a *Application) Version(version string) *Application
- func (a *Application) Writer(w io.Writer) *Application
- type ApplicationModel
- type ApplicationValidator
- type ArgClause
- func (a *ArgClause) Action(action Action) *ArgClause
- func (p *ArgClause) Bool() (target *bool)
- func (p *ArgClause) BoolVar(target *bool)
- func (p *ArgClause) Bytes() (target *units.Base2Bytes)
- func (p *ArgClause) BytesVar(target *units.Base2Bytes)
- func (a *ArgClause) Default(value string) *ArgClause
- func (p *ArgClause) Duration() (target *time.Duration)
- func (p *ArgClause) DurationList() (target *[]time.Duration)
- func (p *ArgClause) DurationListVar(target *[]time.Duration)
- func (p *ArgClause) DurationVar(target *time.Duration)
- func (p *ArgClause) Enum(options ...string) (target *string)
- func (p *ArgClause) EnumVar(target **string, options ...string)
- func (p *ArgClause) Enums(options ...string) (target *[]string)
- func (p *ArgClause) EnumsVar(target *[]string, options ...string)
- func (p *ArgClause) ExistingDir() (target *string)
- func (p *ArgClause) ExistingDirVar(target *string)
- func (p *ArgClause) ExistingFile() (target *string)
- func (p *ArgClause) ExistingFileVar(target *string)
- func (p *ArgClause) File() (target **os.File)
- func (p *ArgClause) FileVar(target **os.File)
- func (p *ArgClause) Float() (target *float64)
- func (p *ArgClause) FloatVar(target *float64)
- func (p *ArgClause) IP() (target *net.IP)
- func (p *ArgClause) IPList() (target *[]net.IP)
- func (p *ArgClause) IPListVar(target *[]net.IP)
- func (p *ArgClause) IPVar(target *net.IP)
- func (p *ArgClause) Int() (target *int)
- func (p *ArgClause) Int64() (target *int64)
- func (p *ArgClause) Int64List() (target *[]int64)
- func (p *ArgClause) Int64ListVar(target *[]int64)
- func (p *ArgClause) Int64Var(target *int64)
- func (p *ArgClause) IntVar(target *int)
- func (a *ArgClause) Model() *ArgModel
- func (p *ArgClause) OpenFile(flag int, perm os.FileMode) (target **os.File)
- func (p *ArgClause) OpenFileVar(target **os.File, flag int, perm os.FileMode)
- func (a *ArgClause) PreAction(action Action) *ArgClause
- func (a *ArgClause) Required() *ArgClause
- func (p *ArgClause) SetValue(value Value)
- func (p *ArgClause) String() (target *string)
- func (p *ArgClause) StringMap() (target *map[string]string)
- func (p *ArgClause) StringMapVar(target *map[string]string)
- func (p *ArgClause) StringVar(target *string)
- func (p *ArgClause) Strings() (target *[]string)
- func (p *ArgClause) StringsVar(target *[]string)
- func (p *ArgClause) TCP() (target **net.TCPAddr)
- func (p *ArgClause) TCPList() (target *[]*net.TCPAddr)
- func (p *ArgClause) TCPListVar(target *[]*net.TCPAddr)
- func (p *ArgClause) TCPVar(target **net.TCPAddr)
- func (p *ArgClause) URL() (target **url.URL)
- func (p *ArgClause) URLList() (target *[]*url.URL)
- func (p *ArgClause) URLListVar(target *[]*url.URL)
- func (p *ArgClause) URLVar(target **url.URL)
- func (p *ArgClause) Uint64() (target *uint64)
- func (p *ArgClause) Uint64List() (target *[]uint64)
- func (p *ArgClause) Uint64ListVar(target *[]uint64)
- func (p *ArgClause) Uint64Var(target *uint64)
- type ArgGroupModel
- type ArgModel
- type CmdClause
- func (c *CmdClause) Action(action Action) *CmdClause
- func (a CmdClause) Arg(name, help string) *ArgClause
- func (c *CmdClause) Command(name, help string) *CmdClause
- func (f CmdClause) Flag(name, help string) *FlagClause
- func (c *CmdClause) FullCommand() string
- func (c *CmdClause) Model() *CmdModel
- func (c *CmdClause) PreAction(action Action) *CmdClause
- func (c *CmdClause) Validate(validator CmdClauseValidator) *CmdClause
- type CmdClauseValidator
- type CmdGroupModel
- type CmdModel
- type FlagClause
- func (f *FlagClause) Action(action Action) *FlagClause
- func (f *FlagClause) Bool() (target *bool)
- func (p *FlagClause) BoolVar(target *bool)
- func (p *FlagClause) Bytes() (target *units.Base2Bytes)
- func (p *FlagClause) BytesVar(target *units.Base2Bytes)
- func (f *FlagClause) Default(value string) *FlagClause
- func (p *FlagClause) Duration() (target *time.Duration)
- func (p *FlagClause) DurationList() (target *[]time.Duration)
- func (p *FlagClause) DurationListVar(target *[]time.Duration)
- func (p *FlagClause) DurationVar(target *time.Duration)
- func (p *FlagClause) Enum(options ...string) (target *string)
- func (p *FlagClause) EnumVar(target **string, options ...string)
- func (p *FlagClause) Enums(options ...string) (target *[]string)
- func (p *FlagClause) EnumsVar(target *[]string, options ...string)
- func (p *FlagClause) ExistingDir() (target *string)
- func (p *FlagClause) ExistingDirVar(target *string)
- func (p *FlagClause) ExistingFile() (target *string)
- func (p *FlagClause) ExistingFileVar(target *string)
- func (p *FlagClause) File() (target **os.File)
- func (p *FlagClause) FileVar(target **os.File)
- func (p *FlagClause) Float() (target *float64)
- func (p *FlagClause) FloatVar(target *float64)
- func (f *FlagClause) Hidden() *FlagClause
- func (p *FlagClause) IP() (target *net.IP)
- func (p *FlagClause) IPList() (target *[]net.IP)
- func (p *FlagClause) IPListVar(target *[]net.IP)
- func (p *FlagClause) IPVar(target *net.IP)
- func (p *FlagClause) Int() (target *int)
- func (p *FlagClause) Int64() (target *int64)
- func (p *FlagClause) Int64List() (target *[]int64)
- func (p *FlagClause) Int64ListVar(target *[]int64)
- func (p *FlagClause) Int64Var(target *int64)
- func (p *FlagClause) IntVar(target *int)
- func (f *FlagClause) Model() *FlagModel
- func (p *FlagClause) OpenFile(flag int, perm os.FileMode) (target **os.File)
- func (p *FlagClause) OpenFileVar(target **os.File, flag int, perm os.FileMode)
- func (f *FlagClause) OverrideDefaultFromEnvar(envar string) *FlagClause
- func (f *FlagClause) PlaceHolder(placeholder string) *FlagClause
- func (f *FlagClause) PreAction(action Action) *FlagClause
- func (f *FlagClause) Required() *FlagClause
- func (p *FlagClause) SetValue(value Value)
- func (f *FlagClause) Short(name byte) *FlagClause
- func (p *FlagClause) String() (target *string)
- func (p *FlagClause) StringMap() (target *map[string]string)
- func (p *FlagClause) StringMapVar(target *map[string]string)
- func (p *FlagClause) StringVar(target *string)
- func (p *FlagClause) Strings() (target *[]string)
- func (p *FlagClause) StringsVar(target *[]string)
- func (p *FlagClause) TCP() (target **net.TCPAddr)
- func (p *FlagClause) TCPList() (target *[]*net.TCPAddr)
- func (p *FlagClause) TCPListVar(target *[]*net.TCPAddr)
- func (p *FlagClause) TCPVar(target **net.TCPAddr)
- func (p *FlagClause) URL() (target **url.URL)
- func (p *FlagClause) URLList() (target *[]*url.URL)
- func (p *FlagClause) URLListVar(target *[]*url.URL)
- func (p *FlagClause) URLVar(target **url.URL)
- func (p *FlagClause) Uint64() (target *uint64)
- func (p *FlagClause) Uint64List() (target *[]uint64)
- func (p *FlagClause) Uint64ListVar(target *[]uint64)
- func (p *FlagClause) Uint64Var(target *uint64)
- type FlagGroupModel
- type FlagModel
- type Getter
- type ParseContext
- type ParseElement
- type Settings
- type Token
- type TokenType
- type Value
Examples ¶
Constants ¶
This section is empty.
Variables ¶
var ( // CommandLine is the default Kingpin parser. CommandLine = New(filepath.Base(os.Args[0]), "") )
var CompactUsageTemplate = `` /* 1202-byte string literal not displayed */
Usage template with compactly formatted commands.
var DefaultUsageTemplate = `` /* 1129-byte string literal not displayed */
Default usage template.
var (
ErrCommandNotSpecified = fmt.Errorf("command not specified")
)
var LongHelpTemplate = `` /* 896-byte string literal not displayed */
Default usage template.
var ManPageTemplate = `` /* 1031-byte string literal not displayed */
var (
TokenEOLMarker = Token{-1, TokenEOL, ""}
)
Functions ¶
func Errorf ¶
func Errorf(format string, args ...interface{})
Errorf prints an error message to stderr.
func ExpandArgsFromFile ¶
Expand arguments from a file. Lines starting with # will be treated as comments.
func FatalIfError ¶
FatalIfError prints an error and exits if err is not nil. The error is printed with the given prefix.
func FatalUsage ¶
func FatalUsage(format string, args ...interface{})
FatalUsage prints an error message followed by usage information, then exits with a non-zero status.
func FatalUsageContext ¶
func FatalUsageContext(context *ParseContext, format string, args ...interface{})
FatalUsageContext writes a printf formatted error message to stderr, then usage information for the given ParseContext, before exiting.
func Fatalf ¶
func Fatalf(format string, args ...interface{})
Fatalf prints an error message to stderr and exits.
Types ¶
type Action ¶
type Action func(*ParseContext) error
Action callback executed at various stages after all values are populated. The application, commands, arguments and flags all have corresponding actions.
type Application ¶
An Application contains the definitions of flags, arguments and commands for an application.
func UsageTemplate ¶
func UsageTemplate(template string) *Application
Set global usage template to use (defaults to DefaultUsageTemplate).
func Version ¶ added in v0.3.0
func Version(version string) *Application
Version adds a flag for displaying the application version number.
func (*Application) Action ¶
func (a *Application) Action(action Action) *Application
Action callback to call when all values are populated and parsing is complete, but before any command, flag or argument actions.
All Action() callbacks are called in the order they are encountered on the command line.
func (*Application) Author ¶
func (a *Application) Author(author string) *Application
func (*Application) Command ¶
func (a *Application) Command(name, help string) *CmdClause
Command adds a new top-level command.
func (*Application) Errorf ¶ added in v1.2.3
func (a *Application) Errorf(format string, args ...interface{})
Errorf prints an error message to w in the format "<appname>: error: <message>".
func (*Application) FatalIfError ¶ added in v1.2.3
func (a *Application) FatalIfError(err error, format string, args ...interface{})
FatalIfError prints an error and exits if err is not nil. The error is printed with the given formatted string, if any.
func (*Application) FatalUsage ¶
func (a *Application) FatalUsage(format string, args ...interface{})
FatalUsage prints an error message followed by usage information, then exits with a non-zero status.
func (*Application) FatalUsageContext ¶
func (a *Application) FatalUsageContext(context *ParseContext, format string, args ...interface{})
FatalUsageContext writes a printf formatted error message to w, then usage information for the given ParseContext, before exiting.
func (*Application) Fatalf ¶ added in v1.3.0
func (a *Application) Fatalf(format string, args ...interface{})
Fatalf writes a formatted error to w then terminates with exit status 1.
func (Application) Flag ¶
func (f Application) Flag(name, help string) *FlagClause
Flag defines a new flag with the given long name and help.
func (*Application) Model ¶
func (a *Application) Model() *ApplicationModel
func (*Application) Parse ¶
func (a *Application) Parse(args []string) (command string, err error)
Parse parses command-line arguments. It returns the selected command and an error. The selected command will be a space separated subcommand, if subcommands have been configured.
This will populate all flag and argument values, call all callbacks, and so on.
func (*Application) ParseContext ¶
func (a *Application) ParseContext(args []string) (*ParseContext, error)
ParseContext parses the given command line and returns the fully populated ParseContext.
func (*Application) PreAction ¶
func (a *Application) PreAction(action Action) *Application
Action called after parsing completes but before validation and execution.
func (*Application) Terminate ¶
func (a *Application) Terminate(terminate func(int)) *Application
Terminate specifies the termination handler. Defaults to os.Exit(status). If nil is passed, a no-op function will be used.
func (*Application) Usage ¶
func (a *Application) Usage(args []string)
Usage writes application usage to w. It parses args to determine appropriate help context, such as which command to show help for.
func (*Application) UsageForContext ¶
func (a *Application) UsageForContext(context *ParseContext) error
UsageForContext displays usage information from a ParseContext (obtained from Application.ParseContext() or Action(f) callbacks).
func (*Application) UsageForContextWithTemplate ¶
func (a *Application) UsageForContextWithTemplate(context *ParseContext, indent int, tmpl string) error
UsageForContextWithTemplate is the base usage function. You generally don't need to use this.
func (*Application) UsageTemplate ¶
func (a *Application) UsageTemplate(template string) *Application
UsageTemplate specifies the text template to use when displaying usage information. The default is UsageTemplate.
func (*Application) Validate ¶ added in v1.3.3
func (a *Application) Validate(validator ApplicationValidator) *Application
Validate sets a validation function to run when parsing.
func (*Application) Version ¶ added in v0.3.0
func (a *Application) Version(version string) *Application
Version adds a --version flag for displaying the application version.
func (*Application) Writer ¶
func (a *Application) Writer(w io.Writer) *Application
Specify the writer to use for usage and errors. Defaults to os.Stderr.
type ApplicationModel ¶
type ApplicationModel struct { Name string Help string Version string Author string *ArgGroupModel *CmdGroupModel *FlagGroupModel }
type ApplicationValidator ¶ added in v1.3.3
type ApplicationValidator func(*Application) error
type ArgClause ¶
type ArgClause struct {
// contains filtered or unexported fields
}
func (*ArgClause) Bool ¶
func (p *ArgClause) Bool() (target *bool)
Bool sets the parser to a boolean parser. Supports --no-<X> to disable the flag.
func (*ArgClause) BoolVar ¶
func (p *ArgClause) BoolVar(target *bool)
Bool sets the parser to a boolean parser. Supports --no-<X> to disable the flag.
func (*ArgClause) Bytes ¶ added in v1.2.0
func (p *ArgClause) Bytes() (target *units.Base2Bytes)
Bytes parses numeric byte units. eg. 1.5KB
func (*ArgClause) BytesVar ¶ added in v1.2.0
func (p *ArgClause) BytesVar(target *units.Base2Bytes)
BytesVar parses numeric byte units. eg. 1.5KB
func (*ArgClause) Default ¶
Default value for this argument. It *must* be parseable by the value of the argument.
func (*ArgClause) DurationList ¶
DurationList accumulates time.Duration values into a slice.
func (*ArgClause) DurationListVar ¶
func (*ArgClause) DurationVar ¶
Duration sets the parser to a time.Duration parser.
func (*ArgClause) ExistingDir ¶
func (p *ArgClause) ExistingDir() (target *string)
ExistingDir sets the parser to one that requires and returns an existing directory.
func (*ArgClause) ExistingDirVar ¶
func (p *ArgClause) ExistingDirVar(target *string)
ExistingDir sets the parser to one that requires and returns an existing directory.
func (*ArgClause) ExistingFile ¶
func (p *ArgClause) ExistingFile() (target *string)
ExistingFile sets the parser to one that requires and returns an existing file.
func (*ArgClause) ExistingFileVar ¶
func (p *ArgClause) ExistingFileVar(target *string)
ExistingFile sets the parser to one that requires and returns an existing file.
func (*ArgClause) Float ¶
func (p *ArgClause) Float() (target *float64)
Float sets the parser to a float64 parser.
func (*ArgClause) FloatVar ¶
func (p *ArgClause) FloatVar(target *float64)
Float sets the parser to a float64 parser.
func (*ArgClause) Int ¶
func (p *ArgClause) Int() (target *int)
Int sets the parser to an int parser.
func (*ArgClause) Int64List ¶
func (p *ArgClause) Int64List() (target *[]int64)
Int64List accumulates int64 values into a slice.
func (*ArgClause) Int64ListVar ¶
func (p *ArgClause) Int64ListVar(target *[]int64)
func (*ArgClause) IntVar ¶
func (p *ArgClause) IntVar(target *int)
Int sets the parser to an int parser.
func (*ArgClause) OpenFile ¶ added in v1.1.0
File attempts to open a File with os.OpenFile(flag, perm).
func (*ArgClause) OpenFileVar ¶ added in v1.1.0
OpenFileVar calls os.OpenFile(flag, perm)
func (*ArgClause) Required ¶
Required arguments must be input by the user. They can not have a Default() value provided.
func (*ArgClause) String ¶
func (p *ArgClause) String() (target *string)
String sets the parser to a string parser.
func (*ArgClause) StringMapVar ¶
StringMap provides key=value parsing into a map.
func (*ArgClause) StringVar ¶
func (p *ArgClause) StringVar(target *string)
String sets the parser to a string parser.
func (*ArgClause) Strings ¶
func (p *ArgClause) Strings() (target *[]string)
Strings accumulates string values into a slice.
func (*ArgClause) StringsVar ¶
func (p *ArgClause) StringsVar(target *[]string)
func (*ArgClause) TCPListVar ¶
func (*ArgClause) URLListVar ¶
URLListVar provides a parsed list of url.URL values.
func (*ArgClause) Uint64List ¶
func (p *ArgClause) Uint64List() (target *[]uint64)
Uint64List accumulates uint64 values into a slice.
func (*ArgClause) Uint64ListVar ¶
func (p *ArgClause) Uint64ListVar(target *[]uint64)
type ArgGroupModel ¶
type ArgGroupModel struct {
Args []*ArgModel
}
func (*ArgGroupModel) ArgSummary ¶
func (a *ArgGroupModel) ArgSummary() string
type CmdClause ¶
type CmdClause struct {
// contains filtered or unexported fields
}
A CmdClause is a single top-level command. It encapsulates a set of flags and either subcommands or positional arguments.
func (CmdClause) Flag ¶
func (f CmdClause) Flag(name, help string) *FlagClause
Flag defines a new flag with the given long name and help.
func (*CmdClause) FullCommand ¶ added in v1.3.1
func (*CmdClause) Validate ¶ added in v1.3.3
func (c *CmdClause) Validate(validator CmdClauseValidator) *CmdClause
Validate sets a validation function to run when parsing.
type CmdClauseValidator ¶ added in v1.3.3
type CmdGroupModel ¶
type CmdGroupModel struct {
Commands []*CmdModel
}
func (*CmdGroupModel) FlattenedCommands ¶
func (c *CmdGroupModel) FlattenedCommands() (out []*CmdModel)
type CmdModel ¶
type CmdModel struct { Name string Help string FullCommand string Depth int *FlagGroupModel *ArgGroupModel *CmdGroupModel }
type FlagClause ¶
type FlagClause struct {
// contains filtered or unexported fields
}
FlagClause is a fluid interface used to build flags.
func (*FlagClause) Action ¶
func (f *FlagClause) Action(action Action) *FlagClause
Dispatch to the given function after the flag is parsed and validated.
func (*FlagClause) Bool ¶
func (f *FlagClause) Bool() (target *bool)
Bool makes this flag a boolean flag.
func (*FlagClause) BoolVar ¶
func (p *FlagClause) BoolVar(target *bool)
Bool sets the parser to a boolean parser. Supports --no-<X> to disable the flag.
func (*FlagClause) Bytes ¶ added in v1.2.0
func (p *FlagClause) Bytes() (target *units.Base2Bytes)
Bytes parses numeric byte units. eg. 1.5KB
func (*FlagClause) BytesVar ¶ added in v1.2.0
func (p *FlagClause) BytesVar(target *units.Base2Bytes)
BytesVar parses numeric byte units. eg. 1.5KB
func (*FlagClause) Default ¶
func (f *FlagClause) Default(value string) *FlagClause
Default value for this flag. It *must* be parseable by the value of the flag.
func (*FlagClause) DurationList ¶
DurationList accumulates time.Duration values into a slice.
func (*FlagClause) DurationListVar ¶
func (*FlagClause) DurationVar ¶
Duration sets the parser to a time.Duration parser.
func (*FlagClause) ExistingDir ¶
func (p *FlagClause) ExistingDir() (target *string)
ExistingDir sets the parser to one that requires and returns an existing directory.
func (*FlagClause) ExistingDirVar ¶
func (p *FlagClause) ExistingDirVar(target *string)
ExistingDir sets the parser to one that requires and returns an existing directory.
func (*FlagClause) ExistingFile ¶
func (p *FlagClause) ExistingFile() (target *string)
ExistingFile sets the parser to one that requires and returns an existing file.
func (*FlagClause) ExistingFileVar ¶
func (p *FlagClause) ExistingFileVar(target *string)
ExistingFile sets the parser to one that requires and returns an existing file.
func (*FlagClause) Float ¶
func (p *FlagClause) Float() (target *float64)
Float sets the parser to a float64 parser.
func (*FlagClause) FloatVar ¶
func (p *FlagClause) FloatVar(target *float64)
Float sets the parser to a float64 parser.
func (*FlagClause) Hidden ¶ added in v1.2.0
func (f *FlagClause) Hidden() *FlagClause
Hidden hides a flag from usage but still allows it to be used.
func (*FlagClause) Int ¶
func (p *FlagClause) Int() (target *int)
Int sets the parser to an int parser.
func (*FlagClause) Int64List ¶
func (p *FlagClause) Int64List() (target *[]int64)
Int64List accumulates int64 values into a slice.
func (*FlagClause) Int64ListVar ¶
func (p *FlagClause) Int64ListVar(target *[]int64)
func (*FlagClause) IntVar ¶
func (p *FlagClause) IntVar(target *int)
Int sets the parser to an int parser.
func (*FlagClause) Model ¶
func (f *FlagClause) Model() *FlagModel
func (*FlagClause) OpenFile ¶ added in v1.1.0
File attempts to open a File with os.OpenFile(flag, perm).
func (*FlagClause) OpenFileVar ¶ added in v1.1.0
OpenFileVar calls os.OpenFile(flag, perm)
func (*FlagClause) OverrideDefaultFromEnvar ¶ added in v0.3.0
func (f *FlagClause) OverrideDefaultFromEnvar(envar string) *FlagClause
OverrideDefaultFromEnvar overrides the default value for a flag from an environment variable, if available.
func (*FlagClause) PlaceHolder ¶ added in v0.2.0
func (f *FlagClause) PlaceHolder(placeholder string) *FlagClause
PlaceHolder sets the place-holder string used for flag values in the help. The default behaviour is to use the value provided by Default() if provided, then fall back on the capitalized flag name.
func (*FlagClause) PreAction ¶
func (f *FlagClause) PreAction(action Action) *FlagClause
func (*FlagClause) Required ¶
func (f *FlagClause) Required() *FlagClause
Required makes the flag required. You can not provide a Default() value to a Required() flag.
func (*FlagClause) Short ¶
func (f *FlagClause) Short(name byte) *FlagClause
Short sets the short flag name.
func (*FlagClause) String ¶
func (p *FlagClause) String() (target *string)
String sets the parser to a string parser.
func (*FlagClause) StringMapVar ¶
StringMap provides key=value parsing into a map.
func (*FlagClause) StringVar ¶
func (p *FlagClause) StringVar(target *string)
String sets the parser to a string parser.
func (*FlagClause) Strings ¶
func (p *FlagClause) Strings() (target *[]string)
Strings accumulates string values into a slice.
func (*FlagClause) StringsVar ¶
func (p *FlagClause) StringsVar(target *[]string)
func (*FlagClause) TCPListVar ¶
func (*FlagClause) URLListVar ¶
URLListVar provides a parsed list of url.URL values.
func (*FlagClause) Uint64List ¶
func (p *FlagClause) Uint64List() (target *[]uint64)
Uint64List accumulates uint64 values into a slice.
func (*FlagClause) Uint64ListVar ¶
func (p *FlagClause) Uint64ListVar(target *[]uint64)
type FlagGroupModel ¶
type FlagGroupModel struct {
Flags []*FlagModel
}
func (*FlagGroupModel) FlagSummary ¶
func (f *FlagGroupModel) FlagSummary() string
type FlagModel ¶
type FlagModel struct { Name string Help string Short rune Default string Envar string PlaceHolder string Required bool Hidden bool Value Value }
func (*FlagModel) FormatPlaceHolder ¶
func (*FlagModel) IsBoolFlag ¶
type Getter ¶
type Getter interface { Value Get() interface{} }
Getter is an interface that allows the contents of a Value to be retrieved. It wraps the Value interface, rather than being part of it, because it appeared after Go 1 and its compatibility rules. All Value types provided by this package satisfy the Getter interface.
type ParseContext ¶ added in v1.3.0
type ParseContext struct { SelectedCommand *CmdClause // Flags, arguments and commands encountered and collected during parse. Elements []*ParseElement // contains filtered or unexported fields }
ParseContext holds the current context of the parser. When passed to Action() callbacks Elements will be fully populated with *FlagClause, *ArgClause and *CmdClause values and their corresponding arguments (if any).
func (*ParseContext) EOL ¶
func (p *ParseContext) EOL() bool
func (*ParseContext) HasTrailingArgs ¶
func (p *ParseContext) HasTrailingArgs() bool
HasTrailingArgs returns true if there are unparsed command-line arguments. This can occur if the parser can not match remaining arguments.
func (*ParseContext) Next ¶ added in v1.3.0
func (p *ParseContext) Next() *Token
Next token in the parse context.
func (*ParseContext) Peek ¶ added in v1.3.0
func (p *ParseContext) Peek() *Token
func (*ParseContext) String ¶ added in v1.3.2
func (p *ParseContext) String() string
type ParseElement ¶
type ParseElement struct { // Clause is either *CmdClause, *ArgClause or *FlagClause. Clause interface{} // Value is corresponding value for an ArgClause or FlagClause (if any). Value *string }
A union of possible elements in a parse stack.
type Value ¶
Value is the interface to the dynamic value stored in a flag. (The default value is represented as a string.)
If a Value has an IsBoolFlag() bool method returning true, the command-line parser makes --name equivalent to -name=true rather than using the next command-line argument, and adds a --no-name counterpart for negating the flag.
Example ¶
This example ilustrates how to define custom parsers. HTTPHeader cumulatively parses each encountered --header flag into a http.Header struct.
package main import ( "fmt" "net/http" "strings" ) type HTTPHeaderValue http.Header func (h *HTTPHeaderValue) Set(value string) error { parts := strings.SplitN(value, ":", 2) if len(parts) != 2 { return fmt.Errorf("expected HEADER:VALUE got '%s'", value) } (*http.Header)(h).Add(parts[0], parts[1]) return nil } func (h *HTTPHeaderValue) String() string { return "" } func HTTPHeader(s Settings) (target *http.Header) { target = new(http.Header) s.SetValue((*HTTPHeaderValue)(target)) return } // This example ilustrates how to define custom parsers. HTTPHeader // cumulatively parses each encountered --header flag into a http.Header struct. func main() { var ( curl = New("curl", "transfer a URL") headers = HTTPHeader(curl.Flag("headers", "Add HTTP headers to the request.").Short('H').PlaceHolder("HEADER:VALUE")) ) curl.Parse([]string{"-H Content-Type:application/octet-stream"}) for key, value := range *headers { fmt.Printf("%s = %s\n", key, value) } }
Output: