An open API service indexing awesome lists of open source software.

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.

Awesome Lists containing this project

README

        

# shelltoken

[![Go Reference](https://pkg.go.dev/badge/github.com/sni/shelltoken.svg)](https://pkg.go.dev/github.com/sni/shelltoken)
[![License](https://img.shields.io/github/license/sni/shelltoken)](https://github.com/sni/shelltoken/blob/main/LICENSE)
[![Go Report Card](https://goreportcard.com/badge/github.com/sni/shelltoken)](https://goreportcard.com/report/github.com/sni/shelltoken)
[![CICD Pipeline](https://github.com/sni/shelltoken/actions/workflows/citest.yml/badge.svg)](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 main

import (
"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"}
}
```