Documentation ¶
Overview ¶
Package argp is a stupid simple flag (argument) parser which allows to parse flags without panicing when non-registered flags are passed.
Index ¶
- Variables
- type Parser
- func (p *Parser) Args() []string
- func (p *Parser) Bool(flag string, def bool, help ...string) (val bool, err error)
- func (p *Parser) Float(flag string, def float64, help ...string) (val float64, err error)
- func (p *Parser) Help() string
- func (p *Parser) Int(flag string, def int, help ...string) (val int, err error)
- func (p *Parser) Scan(flag string, val interface{}) (ok bool, err error)
- func (p *Parser) String(flag string, def string, help ...string) (val string, err error)
Constants ¶
This section is empty.
Variables ¶
var ( Scan = singleton.Scan String = singleton.String Bool = singleton.Bool Int = singleton.Int Float = singleton.Float Args = singleton.Args Help = singleton.Help )
Functions ¶
This section is empty.
Types ¶
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser takes an array of arguments and provides functionalities to parse flags and values contained.
func New ¶
New initializes a new instance of Parser.
It defaultly takes the value of os.Args[1:] as array of arguments. Optionally, you can pass a custom array of arguments you want to scan.
func (*Parser) Args ¶
Args returns all other un-scanned arguments of the passed arguments array.
Example:
p := New([]string{"whats", "-n", "up"}) val, err := p.Bool("-n") // val = true // err = nil // p.Args() = []string{"whats", "up"}
func (*Parser) Bool ¶
Bool is shorthand for Scan with a bool flag value. If the flag was passed (with or wirhout value specified), true is returned. If the parsing fails, the error is returned. When def is passed and no flag was found, def is returned as val.
func (*Parser) Float ¶
Float is shorthand for Scan with a float flag value. It returns the scanned value and an error if the parsing failed. If no flag or value was found and a def value was passed, def is returned as val.
func (*Parser) Int ¶
Int is shorthand for Scan with a integer flag value. It returns the scanned value and an error if the parsing failed. If no flag or value was found and a def value was passed, def is returned as val.
func (*Parser) Scan ¶
Scan looks for the passed flag (unprefixed) in the arguments array. If the flag was found, the value of the flag is scanned into the pointer of val. If the flag and value was found and valid, true is returned. Otherwise, false is returned and if an error occurs, the error is returned as well.
Example:
var config string p := argp.New([]string{"--config", "myconfig.yml"}) ok, err := p.Scan("--config", &config) // config = "myconfig.yml" // ok = true // err = nil