args: Index | Examples | Files

package args

import ""

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)



Package Files



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


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

func GetArgs Uses

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

Parse the input line into an array of arguments


s := `one two three "double quotes" 'single quotes' arg\ with\ spaces "\"quotes\" in 'quotes'" '"quotes" in \'quotes'"`

for i, arg := range GetArgs(s) {
    fmt.Println(i, arg)


0 one
1 two
2 three
3 double quotes
4 single quotes
5 arg with spaces
6 "quotes" in 'quotes'
7 "quotes" in 'quotes

func GetArgsN Uses

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

Parse the input line into an array of max n arguments

func GetOptions Uses

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

func NewFlags Uses

func NewFlags(name string) *flag.FlagSet

Create a new FlagSet to be used with ParseFlags

func ParseFlags Uses

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

Parse the input line through the (initialized) FlagSet


arguments := "-l --number=42 -where=here -- -not-an-option- one two three"

flags := NewFlags("args")

list := flags.Bool("l", false, "list something")
num := flags.Int("number", 0, "a number option")
where := flags.String("where", "", "a string option")

if err := ParseFlags(flags, arguments); err != nil {
} else {
    fmt.Println("list:", *list)
    fmt.Println("num:", *num)
    fmt.Println("where:", *where)
    fmt.Println("args:", flags.Args())


list: true
num: 42
where: here
args: [-not-an-option- one two three]

type Args Uses

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

func ParseArgs Uses

func ParseArgs(line string) (parsed Args)


arguments := "-l --number=42 -where=here -- -not-an-option- one two three |pipers piping"

parsed := ParseArgs(arguments)

fmt.Println("options:", parsed.Options)
fmt.Println("arguments:", parsed.Arguments)


options: map[l: number:42 where:here]
arguments: [-not-an-option- one two three |pipers piping]

func (Args) GetBoolOption Uses

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

func (Args) GetIntOption Uses

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

func (Args) GetOption Uses

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

type GetArgsOption Uses

type GetArgsOption func(s *Scanner)

GetArgsOption is the type for GetArgs options

func InfieldBrackets Uses

func InfieldBrackets() GetArgsOption

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

func UserTokens Uses

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 Uses

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

func NewScanner Uses

func NewScanner(r io.Reader) *Scanner

Creates a new Scanner with io.Reader as input source

func NewScannerString Uses

func NewScannerString(s string) *Scanner

Creates a new Scanner with a string as input source

func (*Scanner) GetOptionTokens Uses

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

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

func (*Scanner) GetTokens Uses

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

Return all tokens as an array of strings

func (*Scanner) GetTokensN Uses

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

func (*Scanner) NextToken Uses

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

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

Package args imports 9 packages (graph) and is imported by 17 packages. Updated 2018-04-11. Refresh now. Tools for package owners.