parser

package
v0.0.0-...-bd02f66 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Oct 19, 2019 License: Apache-2.0 Imports: 5 Imported by: 0

Documentation

Overview

Parser package contains basic utilities for command line args parser.

Index

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

func New(args []string, options ...Option) *Parser

New creates a new parser with provided command line arguments to parse and optional configuration.

func (*Parser) HasName

func (parser *Parser) HasName() bool

HasName returns true if last .Next call parsed flag name.

func (*Parser) HasValue

func (parser *Parser) HasValue() bool

HasValue returns true, if last .Next call parsed flag value or positional argument.

func (*Parser) Name

func (parser *Parser) Name() string

Name returns a flag name or an empty string.

func (*Parser) Next

func (parser *Parser) Next() bool

Next parses next argument, if no arguments left -- returns false, else true. Call it in cycle to parse all arguments.

func (*Parser) Value

func (parser *Parser) Value() string

Value returns flag or positional argument value, if it exists, empty string otherwise.

func (*Parser) ValueFloat32

func (parser *Parser) ValueFloat32() (float32, error)

ValueFloat32 attemts to parse value or positional argument as float32.

func (*Parser) ValueFloat64

func (parser *Parser) ValueFloat64() (float64, error)

ValueFloat64 attemts to parse value or positional argument as float64.

func (*Parser) ValueInt32

func (parser *Parser) ValueInt32() (int32, error)

ValueInt32 attemts to parse value or positional argument as int32.

func (*Parser) ValueInt64

func (parser *Parser) ValueInt64() (int64, error)

ValueInt64 attemts to parse value or positional argument as int64.

func (*Parser) ValueKind

func (parser *Parser) ValueKind() ValueKind

ValueKind returns value kind, if last .Next call parsed flag value or positional argument. Returns ValueKind if no value was parsed.

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
)

func (ValueKind) String

func (i ValueKind) String() string

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL