Documentation
¶
Overview ¶
Package options provides methods for working with command-line options
Example (Parsing) ¶
// Key is option in format "short-name:long-name" or "long-name" // We highly recommend defining options names as constants optMap := Map{ "s:string": {}, // By default, argument has string type "S:string2": {Type: STRING, Value: "Default value"}, // You can predefine default values "int": {Type: INT}, // Integer without short name "I:int2": {Type: INT, Min: 1, Max: 10}, // Integer with limits "f:float": {Type: FLOAT, Value: 10.0}, // Float "b:boolean": {Type: BOOL}, // Boolean "r:required": {Type: INT, Required: true}, // Some options can be marked as required "m:merg": {Type: STRING, Mergeble: true}, // Mergeble options can be defined more than one time "h:help": {Type: BOOL, Alias: "u:usage about"}, // You can define argument aliases "e:example": {Conflicts: "s:string S:string2"}, // Option conflicts with string and string2 (options can't be set at same time) "E:example2": {Bound: "int I:int2"}, // Option bound with int and int2 (options must be set at same time) } // args contains unparsed values args, errs := Parse(optMap) if len(errs) != 0 { for _, err := range errs { fmt.Printf("Error: %v\n", err) os.Exit(1) } } if Has("s:string") { fmt.Println("\"--string/-s\" is set") } fmt.Printf("Arguments: %v\n", args) fmt.Printf("First argument: %s\n\n", args.Get(0)) fmt.Printf("string → %s\n", GetS("string")) fmt.Printf("int → %d\n", GetI("int")) fmt.Printf("float → %f\n", GetF("f:float")) fmt.Printf("boolean → %t\n", GetB("b:boolean"))
Output:
Index ¶
- Constants
- func Add(name string, opt *V) error
- func AddMap(optMap Map) []error
- func GetB(name string) bool
- func GetF(name string) float64
- func GetI(name string) int
- func GetS(name string) string
- func Has(name string) bool
- func ParseOptionName(name string) (string, string)
- func Q(opts ...string) string
- type Arguments
- type Map
- type OptionError
- type Options
- func (opts *Options) Add(name string, option *V) error
- func (opts *Options) AddMap(optMap Map) []error
- func (opts *Options) GetB(name string) bool
- func (opts *Options) GetF(name string) float64
- func (opts *Options) GetI(name string) int
- func (opts *Options) GetS(name string) string
- func (opts *Options) Has(name string) bool
- func (opts *Options) Parse(rawOpts []string, optMap ...Map) (Arguments, []error)
- type V
Examples ¶
Constants ¶
View Source
const ( STRING = iota // String option INT // Int/Uint option BOOL // Boolean option FLOAT // Floating number option MIXED // String or boolean option )
Options types
View Source
const ( ERROR_UNSUPPORTED = iota ERROR_NO_NAME ERROR_DUPLICATE_LONGNAME ERROR_DUPLICATE_SHORTNAME ERROR_OPTION_IS_NIL ERROR_EMPTY_VALUE ERROR_REQUIRED_NOT_SET ERROR_WRONG_FORMAT ERROR_CONFLICT ERROR_BOUND_NOT_SET ERROR_UNSUPPORTED_VALUE )
Error codes
Variables ¶
This section is empty.
Functions ¶
func ParseOptionName ¶
ParseOptionName parses combined name and returns long and short options
Types ¶
type Arguments ¶
type Arguments []string
type OptionError ¶
OptionError is argument parsing error
func (OptionError) Error ¶
func (e OptionError) Error() string
type Options ¶
type Options struct {
// contains filtered or unexported fields
}
Options is options struct
type V ¶
type V struct { Type int // option type Max float64 // maximum integer option value Min float64 // minimum integer option value Alias string // list of aliases Conflicts string // list of conflicts options Bound string // list of bound options Mergeble bool // option supports options value merging Required bool // option is required Value interface{} // default value // contains filtered or unexported fields }
V is basic option struct
Click to show internal directories.
Click to hide internal directories.