shlex

package
v0.2.0 Latest Latest
Warning

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

Go to latest
Published: Mar 29, 2021 License: MIT Imports: 2 Imported by: 1

Documentation

Overview

Package shlex is used for simple command line splitting.

Both POSIX and Windows dialects are provided.

Index

Constants

View Source
const (
	Space       = rune(' ')
	Word        = rune('A')
	DoubleQuote = rune('"')
	SingleQuote = rune('\'')
	EmptyRune   = rune(-2)
	NoEscape    = rune(-1)
)

Variables

View Source
var Posix = Dialect{
	IsSpace: func(r rune) bool {
		return ' ' == r || '\t' == r || '\n' == r
	},
	IsQuote: func(r rune) bool {
		return '"' == r || '\'' == r
	},
	Escape: func(s rune, r, r0 rune) rune {
		if '\\' != r {
			return NoEscape
		}
		switch s {
		case Space, Word:
			if '\n' == r0 || EmptyRune == r0 {
				return EmptyRune
			}
			return r0
		case DoubleQuote:
			if '\n' == r0 || EmptyRune == r0 {
				return EmptyRune
			}
			if '$' == r0 || '`' == r0 || '"' == r0 || '\\' == r0 {
				return r0
			}
			return NoEscape
		default:
			return NoEscape
		}
	},
}

Posix is the POSIX dialect of command line splitting. See https://tinyurl.com/26man79 for guidelines.

View Source
var Windows = Dialect{
	IsSpace: func(r rune) bool {
		return ' ' == r || '\t' == r || '\r' == r || '\n' == r
	},
	IsQuote: func(r rune) bool {
		return '"' == r
	},
	Escape: func(s rune, r, r0 rune) rune {
		switch s {
		case Space, Word:
			if '\\' == r && '"' == r0 {
				return r0
			}
			return NoEscape
		case DoubleQuote:
			if ('\\' == r || '"' == r) && '"' == r0 {
				return r0
			}
			return NoEscape
		default:
			return NoEscape
		}
	},
	LongEscape: func(s rune, r rune, line string) ([]rune, string, rune, int) {

		if '\\' != r {
			return nil, "", 0, 0
		}

		var w int
		n := 0
		for {
			r, w = utf8.DecodeRuneInString(line[n:])
			n++
			if 0 == w || '\\' != r {
				break
			}
		}

		if 2 > n {
			return nil, "", 0, 0
		}

		if '"' != r {
			return []rune(strings.Repeat("\\", n-1)), line[n-1:], r, w
		} else if 0 == n&1 {
			return []rune(strings.Repeat("\\", n/2-1)), line[n-1:], '"', 1
		} else {
			return []rune(strings.Repeat("\\", n/2-1)), line[n-2:], '\\', 1
		}
	},
}

Windows is the Windows dialect of command line splitting. See https://tinyurl.com/ycdj5ghh for guidelines.

Functions

This section is empty.

Types

type Dialect

type Dialect struct {
	IsSpace    func(r rune) bool
	IsQuote    func(r rune) bool
	Escape     func(s rune, r, r0 rune) rune
	LongEscape func(s rune, r rune, line string) ([]rune, string, rune, int)
}

Dialect represents a dialect of command line splitting.

func (*Dialect) Split

func (dialect *Dialect) Split(line string) (tokens []string)

Split splits a command line into tokens according to the chosen dialect.

Jump to

Keyboard shortcuts

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