Help us shape the future of Go! Take the 2026 Go Developer Survey

shellwords

package module
v1.0.14 Latest Latest
Warning

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

Go to latest
Published: Jul 10, 2026 License: MIT Imports: 7 Imported by: 1,316

README

go-shellwords

codecov Build Status PkgGoDev ci

Parse line as shell words.

Usage

args, err := shellwords.Parse("./foo --bar=baz")
// args should be ["./foo", "--bar=baz"]
envs, args, err := shellwords.ParseWithEnvs("FOO=foo BAR=baz ./foo --bar=baz")
// envs should be ["FOO=foo", "BAR=baz"]
// args should be ["./foo", "--bar=baz"]
os.Setenv("FOO", "bar")
p := shellwords.NewParser()
p.ParseEnv = true
args, err := p.Parse("./foo $FOO")
// args should be ["./foo", "bar"]
p := shellwords.NewParser()
p.ParseBacktick = true
args, err := p.Parse("./foo `echo $SHELL`")
// args should be ["./foo", "/bin/bash"]
shellwords.ParseBacktick = true
p := shellwords.NewParser()
args, err := p.Parse("./foo `echo $SHELL`")
// args should be ["./foo", "/bin/bash"]
p := shellwords.NewParser()
p.ParseComment = true
args, err := p.Parse("./foo # comment")
// args should be ["./foo"]
p := shellwords.NewParser()
p.SetExcludeSeparators('\t', ';')

Thanks

This is based on cpan module Parse::CommandLine.

License

under the MIT License: http://mattn.mit-license.org/2017

Author

Yasuhiro Matsumoto (a.k.a mattn)

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	ParseEnv      bool = false
	ParseBacktick bool = false
	ParseComment  bool = false
)

Functions

func Parse

func Parse(line string) ([]string, error)

func ParseWithEnvs added in v1.0.11

func ParseWithEnvs(line string) (envs []string, args []string, err error)

Types

type Parser

type Parser struct {
	ParseEnv      bool
	ParseBacktick bool
	ParseComment  bool
	Position      int
	Dir           string

	// If ParseEnv is true, use this for getenv.
	// If nil, use os.Getenv.
	Getenv func(string) string
	// contains filtered or unexported fields
}

func NewParser

func NewParser() *Parser

func (*Parser) ExcludedSeparators added in v1.0.14

func (p *Parser) ExcludedSeparators() []rune

ExcludedSeparators returns excluded separators

func (*Parser) Parse

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

func (*Parser) ParseWithEnvs added in v1.0.11

func (p *Parser) ParseWithEnvs(line string) (envs []string, args []string, err error)

func (*Parser) SetExcludeSeparators added in v1.0.14

func (p *Parser) SetExcludeSeparators(r ...rune)

SetExcludeSeparators indicates the parser to ignore provided separators when parsing example: parser.SetExcludeSeparators(';','\t')

Jump to

Keyboard shortcuts

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