shlex
Go implementation of Python's shlex for shell-like lexical analysis.
Install
Add the module to your Go project:
go get github.com/chhongzh/shlex@latest
Then import it:
import "github.com/chhongzh/shlex"
Usage
Split a command line into arguments
Split tokenizes a string using shell-like syntax (POSIX mode, whitespace-splitting, comments disabled by default):
package main
import (
"fmt"
"github.com/chhongzh/shlex"
)
func main() {
input := `echo "hello world" 'and more'`
tokens, err := shlex.Split(input)
if err != nil {
panic(err)
}
fmt.Printf("%#v\n", tokens)
// []string{"echo", "hello world", "and more"}
}
If the input has unmatched quotes or an unfinished escape sequence, Split returns an error.
Quote and join arguments
Use Quote to safely shell-escape a single string, and Join to build a command line from a slice of arguments:
args := []string{"echo", "hello world", "it's fine"}
line := shlex.Join(args)
fmt.Println(line)
// echo hello\ world 'it'"'"'s\ fine
Quote leaves "safe" shell characters unmodified and wraps other strings in single quotes, following Python shlex.quote behavior.
Advanced usage with Shlex
For more control, you can work with the Shlex type directly:
package main
import (
"fmt"
"io"
"github.com/chhongzh/shlex"
)
func main() {
lex := shlex.NewString("a && b || c")
lex.SetPosix(true)
lex.SetPunctuationChars("true") // split on shell punctuation like &&, ||, etc.
for {
tok, err := lex.GetToken()
if err == io.EOF {
break
}
if err != nil {
panic(err)
}
fmt.Println(tok)
}
// Output:
// a
// &&
// b
// ||
// c
}
Key configuration points on Shlex:
SetPosix(true) enables POSIX-compatible behavior.
SetPunctuationChars("true") or a custom string controls how punctuation is tokenized.
- Fields like
whitespace, commenters, and whitespaceSplit let you fine-tune parsing.
License
MIT License. See LICENSE for details.