README

Argv

GoDoc Build Status Coverage Status Go Report Card

Argv is a library for Go to split command line string into arguments array.

Documentation

Documentation can be found at Godoc

Example

func TestArgv(t *testing.T) {
	args, err := argv.Argv([]rune(" ls   `echo /`   |  wc  -l "), argv.ParseEnv(os.Environ()), argv.Run)
	if err != nil {
	    t.Fatal(err)
	}
	expects := [][]string{
	    []string{"ls", "/"},
	    []string{"wc", "-l"},
	}
	if !reflect.DeepEqual(args, expects) {
	    t.Fatal(args)
	}
}

LICENSE

MIT.

Expand ▾ Collapse ▴

Documentation

Overview

    Package argv parse command line string into arguments array using the bash syntax.

    Index

    Constants

    This section is empty.

    Variables

    View Source
    var (
    	// ErrInvalidSyntax was reported if there is a syntax error in command line string.
    	ErrInvalidSyntax = errors.New("invalid syntax")
    )

    Functions

    func Argv

    func Argv(cmdline string, backquoteExpander, stringExpander Expander) ([][]string, error)

      Argv split cmdline string as array of argument array by the '|' character.

      The parsing rules is same as bash. The environment variable will be replaced and string surround by '`' will be passed to reverse quote parser.

      func Cmds

      func Cmds(args ...[]string) ([]*exec.Cmd, error)

      func Pipe

      func Pipe(stdin io.Reader, stdout, stderr io.Writer, cmds ...*exec.Cmd) error

      Types

      type Expander

      type Expander func(string) (string, error)

      type Parser

      type Parser struct {
      	// contains filtered or unexported fields
      }

        Parser take tokens from Scanner, and do syntax checking, and generate the splitted arguments array.

        func NewParser

        func NewParser(s *Scanner, backQuoteExpander, stringExpander Expander) *Parser

          NewParser create a cmdline string parser.

          func (*Parser) Parse

          func (p *Parser) Parse() ([][]string, error)

            Parse split command line string into arguments array.

            EBNF:

            Cmdline = Section [ Pipe Cmdline ]
            Section = [Space] SpacedSection { SpacedSection }
            SpacedSection = MultipleUnit [Space]
            MultipleUnit = Unit {Unit}
            Unit = String | BackQuote | SingleQuote | DoubleQuote
            

            type Scanner

            type Scanner struct {
            	// contains filtered or unexported fields
            }

              Scanner is a cmdline string scanner.

              It split cmdline string to tokens: space, string, pipe, reverse quote string.

              func NewScanner

              func NewScanner(text string) *Scanner

                NewScanner create a scanner and init it's internal states.

                func (*Scanner) Next

                func (s *Scanner) Next() (Token, error)

                  Next return next token, if it reach the end, TOK_EOF will be returned.

                  Error is returned for invalid syntax such as unpaired quotes.

                  type Token

                  type Token struct {
                  	Type  TokenType
                  	Value []rune
                  }

                    Token is generated by the scanner with a type and value.

                    func Scan

                    func Scan(text string) ([]Token, error)

                      Scan is a utility function help split input text as tokens.

                      type TokenType

                      type TokenType uint32

                        TokenType is the type of tokens recognized by the scanner.

                        const (
                        	// TokString for string
                        	TokString TokenType = iota + 1
                        	TokStringSingleQuote
                        	TokStringDoubleQuote
                        	// TokPipe is the '|' character
                        	TokPipe
                        	// TokBackQuote is reverse quoted string
                        	TokBackQuote
                        	// TokSpace represent space character sequence
                        	TokSpace
                        	// TokEOF means the input end.
                        	TokEOF
                        )