Skip to content
/ shelltoken Public

Go library to split a commandline into env, command and arguments.

License

Notifications You must be signed in to change notification settings

sni/shelltoken

Folders and files

NameName
Last commit message
Last commit date
Feb 19, 2024
Mar 13, 2024
Feb 19, 2024
Mar 14, 2024
Feb 19, 2024
Mar 14, 2024
Mar 5, 2024
Mar 4, 2024
Mar 5, 2024
Nov 25, 2024
Nov 25, 2024
Mar 5, 2024
Mar 5, 2024

Repository files navigation

shelltoken

Go Reference License Go Report Card CICD Pipeline

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.

Example

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"}
}