Documentation ¶
Overview ¶
Parses options of the forms:
--help - set empty string -h - set empty string -v - set empty string -v value - set value --output=value - set value --output value - set value --o value - set value --ovalue - set value -- - stop option parsing
Notes:
--help value - value is an argument, it is not assigned to "help" --output -v - is an error as value is missing for required argument --output=-v - is allowed, the value is "-v"
Index ¶
Examples ¶
Constants ¶
View Source
const ( NO_ARGUMENT = optionType(iota) REQUIRED_ARGUMENT = optionType(iota) OPTIONAL_ARGUMENT = optionType(iota) )
argument requirements
View Source
const ( Major = "0" Minor = "1" Patch = "1" Version = Major + "." + Minor + "." + Patch )
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option struct { Long string // long option e.g.: "verbose" HasArg optionType // one of: NO_ARGUMENT, REQUIRED_ARGUMENT or OPTIONAL_ARGUMENT Short rune // short option e.g.: 'v' }
option structure to represent a single option definition
type OptionsMap ¶
returned options, the Long name is used in this map repeated option values are returned as a string slice
func GetOS ¶
func GetOS(flags []Option) (program string, options OptionsMap, arguments []string, err error)
parse options from OS command-line
Return values:
program_name - string options - map["option"]=[]string{"value1","value2"} use len(option["verbose"]) to detect a sequence like: -v -v -v the actual value will be options["verbose"] = []string{"", "", ""} arguments - []string (all items not starting with "-" that do not belong to option and everything after --) err - nil if parsing was sucessful
func Getopt ¶
func Getopt(inputs []string, flags []Option) (options OptionsMap, arguments []string, err error)
parse options from an arbitrary array of strings
Note that the input string slices does not contain any program name
Example ¶
package main import ( "fmt" "github.com/bitmark-inc/getoptions" ) func main() { // define options flags := []getoptions.Option{ {Long: "help", HasArg: getoptions.NO_ARGUMENT, Short: 'h'}, {Long: "output", HasArg: getoptions.REQUIRED_ARGUMENT, Short: 'o'}, {Long: "verbose", HasArg: getoptions.NO_ARGUMENT, Short: 'v'}, } // simulated command-line arguments args := []string{"--help", "--output=data1", "zero", "-odata2", "-vvv", "one", "two"} // parse options options, arguments, err := getoptions.Getopt(args, flags) // display results if nil != err { fmt.Printf("parse error: %v\n", err) } else { for _, op := range []string{"help", "output", "verbose"} { fmt.Printf("option[%s]: %#v\n", op, options[op]) } fmt.Printf("arguments: %#v\n", arguments) } }
Output: option[help]: []string{""} option[output]: []string{"data1", "data2"} option[verbose]: []string{"", "", ""} arguments: []string{"zero", "one", "two"}
Click to show internal directories.
Click to hide internal directories.