Skip to content

zakuro9715/nzflag

Repository files navigation

nzflag

Test codecov GoDoc PkgGoDev Go Report Card License

Go library to Normalize argv

From

-ab=0 --value1=0 --value2 1

To

-a -b=0 --value=0 --value2=1

Install

go get github.com/zakuro9715/nzflag

Motivation

There are useful and powerful cli libraries for Go.

But I want preprocess argv before run cli library and raw cli args are complex format. Therefore I created nzflag to preprocess raw argv.

Concept

  • Simple
    • No validation. No typecheck
  • Respect raw argv. So don't swap order as possible as.
  • Result of NormalizeToStrings() is valid argv. So it comopatible with other cli Library (like urfave/cli.
    • You jest pass result of NormalizeToStrings() as Argv

Feature

  • Split short flags (-ab into -a -b)
  • Normalize flag with value (--value 0 into value=0)
  • Treat as arg after "--" (-f2 -- -f2 # -f2 is arg)
  • Merge flags specified multiple times (-a=0 -a=1 into -a=0,1)

Usage

app := New().FlagN("values1", 2).FlagN("values2", 2).FlagN("f", 2)


app.NormalizeToStrings([]string{
	"-ab", "-cd=c", "--cd=c", "-ef", "x", "x",
   	"--values1=v", "--values2", "v1", "v2", "arg",
});

// Result
[]string{
	"-a", "-b", "-c", "-d=c", "--cd=c", "-e", "-f=x,x",
   	"--values1=v", "--values2=v1,v2", "arg"
}

See also PkgGoDev](https://pkg.go.dev/github.com/zakuro9715/nzflag) and test

CLI Tool

Install

go get github.com/zakuro9715/nzflag/cmd/nzflag

Releases

No releases published

Packages

No packages published

Languages