Documentation ¶
Overview ¶
Package flagx provide convenient for parsing command line arguments, and execute logics.
Usage:
1. Define one Option struct:
type Option struct { Address string `description:"the address"` Path string `description:"the path"` }
2. For plain command line:
option := &Option{} cmd, err := flagx.NewCommand("my_command", "some description", option, func() error { return myHandle(option) }) if err != nil { fmt.Print("parse arguments failed", err) return } cmd.ParseOsArgsAndExecute()
3. For composite command line:
cc := flagx.NewCompositeCommand("my_cc", "some description") option := &Option{} _ = cc.AddSubCommand("my_command", "some description", option, func() error { return myHandle(option) }) cc.ParseAndExecute(os.Args[1:])
- struct field tag: name: The arg name. if not set, use converted struct filed name default: Default arg value description: Arg usage and other messages "flag" If is a flag arg(true) or a non-flag arg(false), default is true. index: The position of non-flag args, start from 0. If a field with slice type, set args true, and leave index not set, will receive all non-consumed non-flag args. ignore: Ignore this field, do not parse and add arg flag "required" If this args is required(true|false). Default is false. If default value is set, the required filed will be ignored.
- supported struct field type: string bool int int8 int16 int32 int64 uint uint8 uint16 uint32 uint64 float32 float64 time.Duration
Pointer and are not supported. And, slice type with the element type above can be used, flag args can be set multi times, and stored in the slice, such as "-f 1 -f 2". However, positional non-flag args can not use slices type.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Command ¶
type Command struct { Name string // the name of this command Description string // usage message // contains filtered or unexported fields }
Command is a command line
func NewCommand ¶
func NewCommand(Name string, Description string, option interface{}, handle Handle) (*Command, error)
NewCommand create new command
func (*Command) ParseAndExecute ¶ added in v1.1.2
ParseAndExecute parse arguments, and run handlers. If error occurred, will exit with non-zero code.
func (*Command) ParseOsArgsAndExecute ¶ added in v1.1.2
func (c *Command) ParseOsArgsAndExecute()
ParseOsArgsAndExecute parse commandline passed arguments, and run handlers.If error occurred, will exit with non-zero code.
type CompositeCommand ¶
type CompositeCommand struct { Name string // the command name Description string // the description // contains filtered or unexported fields }
CompositeCommand for commands
func NewCompositeCommand ¶
func NewCompositeCommand(Name string, description string) *CompositeCommand
NewCompositeCommand create new CompositeCommand
func (*CompositeCommand) AddSubCommand ¶
func (c *CompositeCommand) AddSubCommand(name string, description string, option interface{}, handle Handle) error
AddSubCommand add one sub command
func (*CompositeCommand) ParseAndExecute ¶
func (c *CompositeCommand) ParseAndExecute(arguments []string)
ParseAndExecute parse arguments, and execute command
func (*CompositeCommand) ParseOsArgsAndExecute ¶ added in v1.1.1
func (c *CompositeCommand) ParseOsArgsAndExecute()
ParseOsArgsAndExecute parse commandline passed arguments, and execute command