Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sergeymakinen/go-quote
Command-line arguments and variables quoting and unquoting
https://github.com/sergeymakinen/go-quote
cmdline commandline go golang quote shell unix windows
Last synced: 14 days ago
JSON representation
Command-line arguments and variables quoting and unquoting
- Host: GitHub
- URL: https://github.com/sergeymakinen/go-quote
- Owner: sergeymakinen
- License: bsd-3-clause
- Created: 2021-02-17T16:12:59.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2023-12-17T14:59:22.000Z (11 months ago)
- Last Synced: 2024-10-16T22:53:42.266Z (29 days ago)
- Topics: cmdline, commandline, go, golang, quote, shell, unix, windows
- Language: Go
- Homepage:
- Size: 37.1 KB
- Stars: 1
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# quote
[![tests](https://github.com/sergeymakinen/go-quote/workflows/tests/badge.svg)](https://github.com/sergeymakinen/go-quote/actions?query=workflow%3Atests)
[![Go Reference](https://pkg.go.dev/badge/github.com/sergeymakinen/go-quote.svg)](https://pkg.go.dev/github.com/sergeymakinen/go-quote)
[![Go Report Card](https://goreportcard.com/badge/github.com/sergeymakinen/go-quote)](https://goreportcard.com/report/github.com/sergeymakinen/go-quote)
[![codecov](https://codecov.io/gh/sergeymakinen/go-quote/branch/main/graph/badge.svg)](https://codecov.io/gh/sergeymakinen/go-quote)Package quote defines interfaces shared by other packages
that quote command-line arguments and variables.See the documentation for the [unix](https://pkg.go.dev/github.com/sergeymakinen/go-quote/unix) and [windows](https://pkg.go.dev/github.com/sergeymakinen/go-quote/windows) packages for more information.
## Installation
Use go get:
```bash
go get github.com/sergeymakinen/go-quote
```Then import the package into your own code:
```go
import "github.com/sergeymakinen/go-quote"
```## Example
### Unix
```go
filename := `Long File With 'Single' & "Double" Quotes.txt`
fmt.Println(unix.SingleQuote.MustQuote(filename))
// Echoing inside 'sh -c' requires a quoting to make it safe with an arbitrary string
quoted := unix.SingleQuote.Quote(filename)
fmt.Println([]string{
"sh",
"-c",
fmt.Sprintf("echo %s | callme", quoted),
})
unquoted, _ := unix.SingleQuote.Unquote(quoted)
fmt.Println(unquoted)
// Output:
// true
// [sh -c echo 'Long File With '"'"'Single'"'"' & "Double" Quotes.txt' | callme]
// Long File With 'Single' & "Double" Quotes.txt
```### Windows
```go
// If you have to deal with a different from Argv command-line quoting
// when starting processes on Windows, don't forget to manually create a command-line
// via the CmdLine SysProcAttr attribute:
//
// cmd := exec.Command(name)
// cmd.SysProcAttr = &windows.SysProcAttr{
// CmdLine: strings.Join(args, " "),
// }
filename := `Long File With 'Single' & "Double" Quotes.txt`
fmt.Println(windows.Argv.MustQuote(filename))
// Using both Argv and Cmd quoting as callme.exe requires the Argv quoting
// and its safe usage in cmd.exe requires the Cmd quoting
quoted := windows.Argv.Quote(filename)
fmt.Println([]string{
"cmd.exe",
"/C",
fmt.Sprintf("callme.exe %s", windows.Cmd.Quote(quoted)),
})
unquoted, _ := windows.Cmd.Unquote(quoted)
unquoted, _ = windows.Argv.Unquote(unquoted)
fmt.Println(unquoted)
// Output:
// true
// [cmd.exe /C callme.exe ^"Long^ File^ With^ ^'Single^'^ ^&^ \^"Double\^"^ Quotes.txt^"]
// Long File With 'Single' & "Double" Quotes.txt
```## License
BSD 3-Clause