Parse line as shell words

Overview

go-shellwords

codecov Build Status PkgGoDev ci

Parse line as shell words.

Usage

args, err := shellwords.Parse("./foo --bar=baz")
// args should be ["./foo", "--bar=baz"]
envs, args, err := shellwords.ParseWithEnvs("FOO=foo BAR=baz ./foo --bar=baz")
// envs should be ["FOO=foo", "BAR=baz"]
// args should be ["./foo", "--bar=baz"]
os.Setenv("FOO", "bar")
p := shellwords.NewParser()
p.ParseEnv = true
args, err := p.Parse("./foo $FOO")
// args should be ["./foo", "bar"]
p := shellwords.NewParser()
p.ParseBacktick = true
args, err := p.Parse("./foo `echo $SHELL`")
// args should be ["./foo", "/bin/bash"]
shellwords.ParseBacktick = true
p := shellwords.NewParser()
args, err := p.Parse("./foo `echo $SHELL`")
// args should be ["./foo", "/bin/bash"]

Thanks

This is based on cpan module Parse::CommandLine.

License

under the MIT License: http://mattn.mit-license.org/2017

Author

Yasuhiro Matsumoto (a.k.a mattn)

Comments
  • Return process Stderr in shellRun error

    Return process Stderr in shellRun error

    exec.Cmd.Output returns only the Stdout of the executed process, thus the actual error message was being lost. In cases where the subprocess return a exec.ExitError use exec.ExitError.Stderr as the ouput.

    It should've been possible to use exec.Cmd.CombinedOutput that returns both Stdout and Stderr in an unified way, but with this a successful command that also returns something in Stderr will interpolate Stderr in the returned string.

    opened by inkel 6
  • Fix confusion between bytes and runes in example code

    Fix confusion between bytes and runes in example code

    _example/pipe.go has an unnecessary cast from string to runes.

    Of course, as long as we never parse the multibyte string, there's no problem. However, if we change the value of line to /usr/bin/ls -la あ* | sort 2>&1 | tee files.log, we get strange results as follows:

    [/usr/bin/ls -la あ*]
    s
    [rt]
    2>&1
    []
    |
    [tee files.log]
    
    opened by hnw 5
  • Use custom environment instead of os.Getenv?

    Use custom environment instead of os.Getenv?

    Hi, i suggest the ability to replace the os.Getenv function with a generic function - to be more specific adding another field to parser (https://github.com/mattn/go-shellwords/blob/master/shellwords.go#L34):

    type Parser struct {
    	ParseEnv      bool
            Getenv        func(string) string
    	ParseBacktick bool
    	Position      int
    }
    

    if it is nil, os.Getenv is used (this is for backward compatibility). If it weren't for backward compatibility, I would suggest it replaces the ParseEnv variable entirely.

    Would be happy to send out a PR if this makes sense.

    opened by itayd 4
  • fix trying to parse subshell calls as env vars

    fix trying to parse subshell calls as env vars

    Parser didn't detect subshell calls, $(), and tried to parse them as environment variables. This picks up the $( pattern and continues on with parsing, leaving it as is.

    Added tests for it as well.

    opened by eikenb 3
  • Fix issue 48: single-quote+ParseEnv bug

    Fix issue 48: single-quote+ParseEnv bug

    Fixes issues with parsing shell lines with single quotes used to group things (eg. subshell calls). Fix was to set got state to argQuoted as the double quote path used (and which worked for it).

    Fixes #48

    opened by eikenb 3
  • Halp mi

    Halp mi

    When i do 'go get github.com/mattn/go-shellwords' on my vps which is Debian Jessie 8 32 bit i get the error eerr.Stderr undefined (type *exec.ExitError has no field or method Stderr)

    opened by JohnSmith268 3
  • Create a licence file

    Create a licence file

    Docker is using this as vendor package. In docker we can check for checking copyright for all the vendor packages and expects any files in .*LICENSE.*' '.*COPYRIGHT.*' '.*COPYING.*' pattern, currently while validating this package it is printing the warning message.

    Please create explicit licence file to avoid any warnings while validation, so create a LICENCE file in parent directory with the MIT licence referred in the README file.

    opened by mkumatag 3
  • parse `\t` as TAB, not escaped `t`

    parse `\t` as TAB, not escaped `t`

    \t or \n must be parsed as special characters TAB and NEWLINE, not considered escaped

    closes https://github.com/mattn/go-shellwords/issues/52 reported by https://github.com/docker/compose/issues/8605

    opened by ndeloof 2
  • Support arguments consisting only of escaped string

    Support arguments consisting only of escaped string

    Currently, arguments consisting only of escaped characters are merged with the following arguments.

    This pull request fixes the issue. Also adds some tests.

    Reproducible example

    Parse(`foo \& bar`)
    

    Expected result

    []string{"foo", "&", "bar"}
    

    Actual result

    []string{"foo", "&bar"}
    
    opened by hnw 2
  • Support empty arguments

    Support empty arguments

    Currently

    Parse(`foo "" bar ''`)
    

    ignores the empty parameters and gives ["foo","bar"]. This should not be the case since a shell will detect these empty arguments. Furthermore, according to #5 the current behaviour is not correct.

    With this PR the Parse result becomes ["foo", "", "bar", ""] as described in #5 and as a shell would have interpreted.

    opened by marckhouzam 2
  • Support smart quotes

    Support smart quotes

    I appreciate this project is old and may not be maintained anymore, but before forking it I thought I'd file my particular feature request in the event that it is still maintained.

    It would be great if the Parser had an option to ParseSmartQuotes, which would:

    • Treat and as ".
    • Treat and as '.

    That's it. Thanks!

    opened by kegsay 2
  • Parsing parens should not always throw `invalid command line string` error

    Parsing parens should not always throw `invalid command line string` error

    It's not uncommon, in certain scenarios, to use parens inside args.

    An example can be seen in the Traefik docs here (click CLI and look at the example), where it's expected that, to set a specific value, the arg --providers.docker.constraints=Label('a.label.name','foo') be used.

    With the current implementation of go-shellwords, parsing a string including an arg like that throws an error, even though this is a valid shell-word string.

    opened by laurazard 1
  • Avoid treating \ as escape char on Windows

    Avoid treating \ as escape char on Windows

    Fixes #38

    While this is probably not a perfect solution, I think it makes a good start as it makes all tests pass on Windows and fixes the most common issue when \ paths are used in the input.

    I originally wanted to implement proper escaping for Windows too, but then I realized it would be a little more complex and I thought this is already a progress. 🤷‍♂️

    opened by radeksimko 6
  • bug: Parse function removes needed backslashes

    bug: Parse function removes needed backslashes

    In Windows, backslashes are used at the OS path separator. When I run this code, I get an incorrect result for the value of cmd:

    func main() {
    	sh := `upx c:\github.com\jftuga\test\test.exe`
    	cmd, err := shellwords.Parse(sh)
    	if err != nil {
    		fmt.Println("err:", err)
    		return
    	}
    	fmt.Println(cmd)
    }
    

    This outputs:

    [upx c:github.comjftugatesttest.exe]
    

    when it should output:

    [upx c:\github.com\jftuga\test\test.exe]
    

    Can this be fixed, please?

    opened by jftuga 8
Owner
mattn
Long-time Golang user&contributor, Google Dev Expert for Go, and author of many Go tools, Vim plugin author. Windows hacker C#/Java/C/C++
mattn
Words - help with a word finder game, sketches a text-processing utility program

Shell-style text processing in Go I saw a word game where the puzzle gives you six letters. By means of a clever user interface, you construct words f

Bruce Ediger 0 Jan 1, 2022
Parse placeholder and wildcard text commands

allot allot is a small Golang library to match and parse commands with pre-defined strings. For example use allot to define a list of commands your CL

Sebastian Müller 55 Nov 24, 2022
A Go library to parse and format vCard

go-vcard A Go library to parse and format vCard. Usage f, err := os.Open("cards.vcf") if err != nil { log.Fatal(err) } defer f.Close() dec := vcard.

Simon Ser 85 Dec 26, 2022
Parse RSS, Atom and JSON feeds in Go

gofeed The gofeed library is a robust feed parser that supports parsing both RSS, Atom and JSON feeds. The library provides a universal gofeed.Parser

null 2k Jan 8, 2023
Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and struct tags for golang crawler

Pagser Pagser inspired by page parser。 Pagser is a simple, extensible, configurable parse and deserialize html page to struct based on goquery and str

foolin 78 Dec 13, 2022
Go library to parse and render Remarkable lines files

go-remarkable2pdf Go library to parse and render Remarkable lines files as PDF.

Jay Goel 35 Nov 7, 2022
parse and generate XML easily in go

etree The etree package is a lightweight, pure go package that expresses XML in the form of an element tree. Its design was inspired by the Python Ele

Brett Vickers 1k Dec 19, 2022
Parse data and test fixtures from markdown files, and patch them programmatically, too.

go-testmark Do you need test fixtures and example data for your project, in a language agnostic way? Do you want it to be easy to combine with documen

Eric Myhre 20 Oct 31, 2022
A shell parser, formatter, and interpreter with bash support; includes shfmt

sh A shell parser, formatter, and interpreter. Supports POSIX Shell, Bash, and mksh. Requires Go 1.14 or later. Quick start To parse shell scripts, in

Daniel Martí 5.3k Dec 29, 2022
Pryrite, interactively execute shell code blocks in a markdown file

Pryrite Pryrite is a command line tool that interactively runs executable blocks in a markdown file. One can think of pryrite as a console REPL/debugg

Rama Shenai 169 Dec 18, 2022
This command line converts thuderbird's exported RSS .eml file to .html file

thunderbird-rss-html This command line tool converts .html to .epub with images fetching. Install > go get github.com/gonejack/thunderbird-rss-html Us

会有猫的 0 Dec 15, 2021
Commonwords - Simple cli to find words in text that are not in the 1000 most common English words

Thousand common words Find words in a text that are not in the 1000 most common

Sakari Mursu 0 Feb 1, 2022
This project parses all mails from google-search within key-words and ban-words

mailParser This project parses all mails from google-search within key-words and ban-words For launch program create the input file first string conta

null 0 Feb 2, 2022
Go-fetch-words - Fetch 5 letter words from dictionary.com

Go-fetch-words This GO app fetches 5 letter words from dictionary.com and saves

Royson 2 Oct 16, 2022
parse-curl.js golang version. Parse curl commands, returning an object representing the request.

parse-curl.js golang version. Parse curl commands, returning an object representing the request.

StarBus 4 Nov 1, 2022
Parse a shell script and output all export declarations in an easy to read format

Find Exports Parse a shell script and output all export declarations in an easy to read format. Usage Example $ findexports ~/.bashrc PATH=$PATH:/usr/

Ilia Choly 1 Jan 13, 2022
painless task queue manager for shell commands with an intuitive cli interface (execute shell commands in distributed cloud-native queue manager).

EXEQ DOCS STILL IN PROGRESS. Execute shell commands in queues via cli or http interface. Features Simple intuitive tiny cli app. Modular queue backend

Mohammed Al Ashaal 13 Dec 14, 2022
ap 是一个 shell 工具,可以让其它 shell 命令的输出能够自动进入交互翻页模式

ap -- auto-pager ap 是一个 shell 工具,可以让其它 shell 命令的输出能够自动进入交互翻页模式。 ap 由两部分组成,一个 Go 语言编写的二进制程序,负责捕获命令的输出并支持翻页, 和一组 shell 脚本,负责为用户指定的命令清单创建与之同名的 wrapper。 经

flw 12 Apr 12, 2022
Assume-shell - A tool to create a shell with AWS environment credentials set

assume-shell This tool will request AWS credentials for a given profile/role and

Erik Jansson 3 Sep 29, 2022
Create strong passwords using words that are easy for you to remember

Grasp Create strong passwords using words that are easy for you to remember A way to circumvent password complexity rules and restrictions while only

Luca Sepe 22 Nov 3, 2022