Documentation
¶
Overview ¶
Parser package contains basic utilities for command line args parser.
Index ¶
- func PlusFlags(parser *Parser)
- func WindowsFlags(parser *Parser)
- type Option
- type Parser
- func (parser *Parser) HasName() bool
- func (parser *Parser) HasValue() bool
- func (parser *Parser) Name() string
- func (parser *Parser) Next() bool
- func (parser *Parser) Value() string
- func (parser *Parser) ValueFloat32() (float32, error)
- func (parser *Parser) ValueFloat64() (float64, error)
- func (parser *Parser) ValueInt32() (int32, error)
- func (parser *Parser) ValueInt64() (int64, error)
- func (parser *Parser) ValueKind() ValueKind
- type ValueKind
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func PlusFlags ¶
func PlusFlags(parser *Parser)
PlusFlags allows parser to treat plus-prefixed arguments as flags (+lool, +e, etc.)
func WindowsFlags ¶
func WindowsFlags(parser *Parser)
WindowsFlags allows the parser to parse backslash-style flags. (\help, \boop, \j, etc.)
Types ¶
type Option ¶
type Option func(parser *Parser)
Option describes a parser optional configuration unit.
type Parser ¶
type Parser struct {
// contains filtered or unexported fields
}
Parser reads command line arguments and emits flags and positional values as key-value pairs.
Example ¶
package main import ( "fmt" "github.com/ninedraft/cege/pkg/parser" ) func main() { // import "github.com/ninedraft/cege/pkg/parser" var args = []string{ "loop", "--bu=10", "-e", "birdy", "--enable-tool", "arg1", `"gopher://losa.rom"`, "/heck/do", } var pr = parser.New(args) for pr.Next() { if pr.HasName() { fmt.Printf("name: %s ", pr.Name()) } if pr.HasValue() { fmt.Printf("value: %q\n", pr.Value()) } } }
Output:
func New ¶
New creates a new parser with provided command line arguments to parse and optional configuration.
func (*Parser) HasValue ¶
HasValue returns true, if last .Next call parsed flag value or positional argument.
func (*Parser) Next ¶
Next parses next argument, if no arguments left -- returns false, else true. Call it in cycle to parse all arguments.
func (*Parser) Value ¶
Value returns flag or positional argument value, if it exists, empty string otherwise.
func (*Parser) ValueFloat32 ¶
ValueFloat32 attemts to parse value or positional argument as float32.
func (*Parser) ValueFloat64 ¶
ValueFloat64 attemts to parse value or positional argument as float64.
func (*Parser) ValueInt32 ¶
ValueInt32 attemts to parse value or positional argument as int32.
func (*Parser) ValueInt64 ¶
ValueInt64 attemts to parse value or positional argument as int64.
type ValueKind ¶
type ValueKind int
ValueKind describes kind of parsed value.
const ( // UndefinedValueKind corresponds to unitialised value. UndefinedValueKind ValueKind = iota // no tokens expected // Text corresponds to text value. Text // a quoted string expected // Int corresponds to integer value. Int // an integer number literal expected // Float corresponds to float number value. Float // a float number literal expected )