argv

package module
v0.2.1 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2021 License: MIT Imports: 6 Imported by: 0

README

Argv

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

Example

fmt.Printf("%#v\n", argv.MustParse(`ls -al | tee "list of files.txt"`))
// [][]string{[]string{"ls", "-al"}, []string{"tee", "list of files.txt"}}

cmds := argv.NewCommands(argv.MustParse("ls -al | `openssl` md5", argv.Unbackquote)...)
// [/bin/ls -al /usr/bin/openssl md5]

// run command using standard input, output and error:
cmds.Std().Run()

// using custom stdout:
var buf bytes.Buffer
cmds.Out(&buf).Run()
fmt.Println(strings.TrimSpace(buf.String()))

LICENSE

MIT.

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 MustParse

func MustParse(cmdline string, expanders ...Expander) [][]string

MustParse is like Parse but panics if parsing fails.

func Parse

func Parse(cmdline string, expanders ...Expander) ([][]string, error)

Parse splits 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.

Types

type Commands

type Commands []*exec.Cmd

func NewCommands

func NewCommands(args ...[]string) (cmds Commands)

func (Commands) Err

func (cmds Commands) Err(stderr io.Writer) Commands

func (Commands) In

func (cmds Commands) In(stdin io.Reader) Commands

func (Commands) Out

func (cmds Commands) Out(stdout io.Writer) Commands

func (Commands) Run

func (cmds Commands) Run() error

func (Commands) Std

func (cmds Commands) Std() Commands

type Expander

type Expander func(string) (string, error)
var (
	Unbackquote Expander = func(backquoted string) (string, error) {
		return backquoted, nil
	}
)

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
)

Jump to

Keyboard shortcuts

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