Documentation ¶
Overview ¶
Package mopt provides getopt style options parsing in 90 lines of Go.
Its API consist of five OptX methods, with 'X' being of 'B'ool, 'S'tring, 'F'loat, 'N'umber (int), and finally 'L'ist - that returns list (slice) of arguments after the last option (or after terminating --).
Declaration `var cl mopt.Usage = "usage/help"` is the only chore. Then you just call one of cl.OptX(flag, default) methods where needed. If flag was given you will get its value. If it was not - you have the default.
Mopt parses oldschool single letter options, and option "-h" is predefined to print var Usage content (ie. "usage/help" string). Spaces between flag letter and value are unimportant: ie. -a bc, and -abc are equivalent. Same for numbers: -n-3 and -n -3 both provide -3 number. For this elasticity a leading dash of string value must be given escaped with a backslash: eg. -s\-dashed or -s "\- started with a dash"; and flag grouping is not supported, too. Ie. -a -b -c are three boolean flags, but -abc would be an -a option introducing a string value of "bc".
Mopt is meant to be used in the PoC code and ad-hoc cli tools. It parses whole os.Args anew on each OptX call. There is no user feedback of "unknown option", nor developer is guarded against opt-letter reuse. Caveat Emptor!
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Exit func(int) = os.Exit
Exit(0) can be hijacked here
var HelpLead string = "purpose, usage & options:\n"
Mopt can also say "propósito, uso y opciones:\n"
Functions ¶
This section is empty.
Types ¶
type Usage ¶
type Usage string
Usage is the API holder type - to be filled with the message to be
printed as "help/usage" after the "prog - purpose, usage & options:\n" predefined lead, if user gave the '-h' option. After printing Usage content program terminates returning zero.
Expanding help: The -h flag and subsequent string can be retrieved early in a program without stepping into the default `print Usage then Exit` path. If first call to the Usage methods will be for the 'h', eg. `subhelp := cl.OptS("h","-")`, returned "subhelp" string (!= "-") will tell that -h option is present, amd that user possibly wants more help on topic. After servicing user needs, program should terminate.
func (Usage) OptB ¶
Method OptB returns true if flag was given, otherwise it returns false. It need not to take a default: flag either is present, or not.
func (Usage) OptF ¶
Method OptF returns float64 read as f32 from string following the flag. If flag was not given, or it could not be parsed to the float, OptF returns the def value. String is parsed to the float64 but of value that is convertible to the float32 without value changing.
func (Usage) OptL ¶
Method OptL returns a slice of commandline arguments after the last option, or arguments after the terminating -- dashes, if given; or all arguments, if no dash-letter was spotted.