shellwords

package module
v0.0.0-...-59467a9 Latest Latest
Warning

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

Go to latest
Published: Mar 15, 2018 License: MIT Imports: 5 Imported by: 73

README

Shellwords

A golang library for splitting command-line strings into words like a Posix or Windows shell would.

Installation

go get -u github.com/buildkite/shellwords

Usage

package main

import (
  "github.com/buildkite/shellwords"
  "fmt"
)

func main() {
  words := shellwords.Split(`/usr/bin/bash -e -c "llamas are the \"best\" && echo 'alpacas'"`)
  for _, word := range words {
    fmt.Println(word)
  }
}

// Outputs:
// /usr/bin/bash
// -e
// -c
// llamas are the "best" && echo 'alpacas'

Alternatives

Previously we were using https://github.com/mattn/go-shellwords, but it lacked support for Windows quoting and escaping conventions, specifically backslashed paths. It also supports things like env expansion and command execution, which made me very nervous, so I decided to write a simpler lexer that just addressed the splitting problem.

Other alternatives are https://github.com/flynn-archive/go-shlex and https://github.com/kballard/go-shellquote, of which the latter is probably closest in spirit to this library.

License

Licensed under MIT license, in LICENSE.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func Quote

func Quote(word string) string

Quote chooses between QuotePosix and QuoteBatch based on your operating system

func QuoteBatch

func QuoteBatch(s string) string

QuoteBatch returns the string such that a CMD.EXE shell would parse it as a single word

func QuotePosix

func QuotePosix(s string) string

QuotePosix returns the string such that a posix shell would parse it as a single word

func Split

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

Split chooses between SplitPosix and SplitBatch based on your operating system

func SplitBatch

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

SplitBatch splits a command string into words like Windows CMD.EXE would See https://ss64.com/nt/syntax-esc.html

func SplitPosix

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

SplitPosix splits a command string into words like a posix shell would

Types

This section is empty.

Jump to

Keyboard shortcuts

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