Documentation ¶
Overview ¶
Package opt is an adapter to the getopt-package `github.com/pborman/getopt` providing `getopt` like processing of command line arguments.
Example ¶
package main import "fmt" func someOptions() *OptionSet { opts := NewOptionSet() opts.Add(NewOption(Bool, "foo", 'f', false, "a boolean option")) opts.Add(NewOption(String, "bar", 'b', "42", "a string option")) return opts } func main() { opts := someOptions() args := []string{"_", "-f", "-x", "--bar", "thirtyfive"} opts.Parse(args) var foo bool = opts.GetBool("foo") var bar string = opts.GetString("bar") fmt.Println(foo, bar) fmt.Println(args) }
Output: true thirtyfive [_ -f -x --bar thirtyfive]
Index ¶
- type Option
- type OptionSet
- func (s *OptionSet) Add(o Option)
- func (s *OptionSet) GetBool(name string) bool
- func (s *OptionSet) GetFloat64(name string) float64
- func (s *OptionSet) GetInt32(name string) int32
- func (s *OptionSet) GetList(name string) []string
- func (s *OptionSet) GetString(name string) string
- func (s *OptionSet) Parse(args []string)
- func (s *OptionSet) ParseUnforgiving(args []string)
- type OptionType
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type Option ¶
type Option struct { Type OptionType Name string Short rune DefaultValue interface{} Description string }
Option specifies a command line option (argument). Each option has a type, a name, a short representation of the name, a default value, and a description.
type OptionSet ¶
OptionSet is a container type holding multiple Options. It provides methods to parse cmd arguments based on the contained options, and methods to access the respective values afterwards.
func (*OptionSet) Add ¶
Add adds option `o` to OptionSet `s`. Panics if OptionType is not supported.
func (*OptionSet) GetFloat64 ¶
GetFloat64 returns the (float64) value of an option given its `name`
func (*OptionSet) ParseUnforgiving ¶
ParseUnforgiving parses a list of command line arguments according to an OptionSet. When an error occurs during option parsing, os.Exit(1) is called.
type OptionType ¶
type OptionType byte
OptionType allows to distinguish between different cmd option types
const ( // Bool is an option type for cmd arguments that can be either true or false Bool OptionType = iota // String is the option type for cmd arguments holding arbitrary strings String // Int32 is the option type for cmd arguments holding int32 values Int32 // List is the option type for cmd arguments that are lists of strings List // Float64 is the option type for cmd arguments holding float64 values Float64 )