Skip to content

Commit

Permalink
add -h and -V (#23)
Browse files Browse the repository at this point in the history
  • Loading branch information
alessio authored Jul 18, 2023
2 parents 060c7c3 + 2857d75 commit 36e49af
Showing 1 changed file with 42 additions and 3 deletions.
45 changes: 42 additions & 3 deletions cmd/escargs/escargs.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"bytes"
"flag"
"fmt"
"log"
"os"

"github.com/alessio/shellescape"
Expand All @@ -17,22 +18,45 @@ var (
discardBlankLines bool
nullSeparator bool
argFile string
helpMode bool
versionMode bool
)

func main() {
var version = "UNRELEASED"

func init() {
flag.BoolVar(&discardBlankLines, "D", false, "ignore blank lines on the input stream.")
flag.BoolVar(&nullSeparator, "0", false, "input items are terminated by a null character instead of by new line.")
flag.StringVar(&argFile, "a", "", "read arguments from file, not standard input.")
flag.BoolVar(&helpMode, "h", false, "display this help and exit.")
flag.BoolVar(&versionMode, "V", false, "output version information and exit.")
flag.Usage = usage
flag.ErrHelp = nil
}

func main() {
log.SetFlags(0)
log.SetPrefix("escargs: ")
log.SetOutput(os.Stderr)
flag.Parse()

if helpMode {
usage()
return
}

if versionMode {
outputVersion()
return
}

firstScan := true
scanner := bufio.NewScanner(os.Stdin)

if argFile != "" {
f, err := os.Open(argFile)
if err != nil {
fmt.Fprintf(os.Stderr, "escargs: %v\n", err)
os.Exit(1)
log.Fatal(err)
}

scanner = bufio.NewScanner(f)
Expand Down Expand Up @@ -76,3 +100,18 @@ func splitNullTerminatedItems(data []byte, atEOF bool) (advance int, token []byt
// Request more data.
return 0, nil, nil
}

func usage() {
usageString := `Usage: escargs [-0ad]
Escape arbitrary strings for safe use as command line arguments.
Options:`
_, _ = fmt.Fprintln(os.Stderr, usageString)

flag.PrintDefaults()
}

func outputVersion() {
fmt.Fprintf(os.Stderr, "escargs version %s\n", version)
fmt.Fprintln(os.Stderr, "Copyright (C) 2020-2023 Alessio Treglia <alessio@debian.org>")
}

0 comments on commit 36e49af

Please sign in to comment.