parser

package
v0.0.0-...-5c5f118 Latest Latest
Warning

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

Go to latest
Published: Feb 26, 2024 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

This package provides methods to parse a shell-like command line string into a list of arguments.

Words are split on white spaces, respecting quotes (single and double) and the escape character (backslash)

Index

Constants

View Source
const (
	ESCAPE_CHAR  = '\\'
	OPTION_CHAR  = '-'
	QUOTE_CHARS  = "`'\""
	SYMBOL_CHARS = `|><#{([`
	NO_QUOTE     = unicode.ReplacementChar
	RAW_QUOTE    = '`'
)

Variables

View Source
var (
	BRACKETS = map[rune]rune{
		'{': '}',
		'[': ']',
		'(': ')',
	}
)

Functions

func GetArgs

func GetArgs(line string, options ...GetArgsOption) (args []string)

Parse the input line into an array of arguments

func GetArgsN

func GetArgsN(line string, n int, options ...GetArgsOption) []string

Parse the input line into an array of max n arguments. If n <= 1 this is equivalent to calling GetArgs.

func GetOptions

func GetOptions(line string, scanOptions ...GetArgsOption) (options []string, rest string)

func NewFlags

func NewFlags(name string) *flag.FlagSet

Create a new FlagSet to be used with ParseFlags

func ParseFlags

func ParseFlags(flags *flag.FlagSet, line string) error

Parse the input line through the (initialized) FlagSet

Types

type Args

type Args struct {
	Options   map[string]string
	Arguments []string
}

func ParseArgs

func ParseArgs(line string, options ...GetArgsOption) (parsed Args)

func (Args) GetBoolOption

func (a Args) GetBoolOption(name string, def bool) bool

func (Args) GetIntOption

func (a Args) GetIntOption(name string, def int) int

func (Args) GetOption

func (a Args) GetOption(name, def string) string

type GetArgsOption

type GetArgsOption func(s *Scanner)

GetArgsOption is the type for GetArgs options

func InfieldBrackets

func InfieldBrackets() GetArgsOption

InfieldBrackets enable processing of in-field brackets (i.e. name={"values in brackets"})

func UserTokens

func UserTokens(t string) GetArgsOption

UserTokens allows a client to define a list of tokens (runes) that can be used as additional separators

type Scanner

type Scanner struct {
	InfieldBrackets bool
	UserTokens      string
	// contains filtered or unexported fields
}

func NewScanner

func NewScanner(r io.Reader) *Scanner

Creates a new Scanner with io.Reader as input source

func NewScannerString

func NewScannerString(s string) *Scanner

Creates a new Scanner with a string as input source

func (*Scanner) GetOptionTokens

func (scanner *Scanner) GetOptionTokens() ([]string, string, error)

Return all "option" tokens (tokens that start with "-") and remainder of the line

func (*Scanner) GetTokens

func (scanner *Scanner) GetTokens() (tokens []string, err error)

Return all tokens as an array of strings

func (*Scanner) GetTokensN

func (scanner *Scanner) GetTokensN(n int) ([]string, string, error)

func (*Scanner) NextToken

func (scanner *Scanner) NextToken() (s string, delim int, err error)

Get the next token from the Scanner, return io.EOF when done

Jump to

Keyboard shortcuts

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