https://github.com/sni/shelltoken
Go library to split a commandline into env, command and arguments.
https://github.com/sni/shelltoken
golang golang-library parser shell string
Last synced: 4 months ago
JSON representation
Go library to split a commandline into env, command and arguments.
- Host: GitHub
- URL: https://github.com/sni/shelltoken
- Owner: sni
- License: mit
- Created: 2024-02-19T18:24:24.000Z (over 1 year ago)
- Default Branch: main
- Last Pushed: 2024-03-14T12:44:40.000Z (over 1 year ago)
- Last Synced: 2024-05-21T12:23:46.320Z (about 1 year ago)
- Topics: golang, golang-library, parser, shell, string
- Language: Go
- Homepage:
- Size: 142 KB
- Stars: 3
- Watchers: 3
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# shelltoken
[](https://pkg.go.dev/github.com/sni/shelltoken)
[](https://github.com/sni/shelltoken/blob/main/LICENSE)
[](https://goreportcard.com/report/github.com/sni/shelltoken)
[](https://github.com/sni/shelltoken/actions/workflows/citest.yml)Go library to split a command line into env, command and arguments.
## Installation
%> go get github.com/sni/shelltoken
## Documentation
The documenation can be found on [pkg.go.dev](https://pkg.go.dev/github.com/sni/shelltoken).
## Example
```golang
package mainimport (
"fmt""github.com/sni/shelltoken"
)func ExampleSplitLinux() {
env, argv, err := shelltoken.SplitLinux("PATH=/bin ls -l")
if err != nil {
panic(err.Error())
}fmt.Printf("env: %#v\nargv: %#v\n", env, argv)
// Output:
// env: []string{"PATH=/bin"}
// argv: []string{"ls", "-l"}
}func ExampleSplitWindows() {
env, argv, err := shelltoken.SplitWindows(`'C:\Program Files\Vim\vim90\vim.exe' -n test.txt`)
if err != nil {
panic(err.Error())
}fmt.Printf("env: %#v\nargv: %#v\n", env, argv)
// Output:
// env: []string{}
// argv: []string{"C:\\Program Files\\Vim\\vim90\\vim.exe", "-n", "test.txt"}
}func ExampleSplitQuotes() {
token, err := shelltoken.SplitQuotes(`ls -la | grep xyz;echo ok`, `|;`, shelltoken.SplitIgnoreShellCharacters|shelltoken.SplitKeepSeparator)
if err != nil {
panic(err.Error())
}fmt.Printf("token: %#v\n", token)
// Output:
// token: []string{"ls -la ", "|", " grep xyz", ";", "echo ok"}
}
```