From 95761fa14ae625b1929f9b43403e4e1a4910e192 Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Sat, 23 Jan 2016 20:08:00 -0800 Subject: update readme with new additions --- README.md | 55 +++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 4 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index 3d1d12f..ae7aa6c 100644 --- a/README.md +++ b/README.md @@ -24,16 +24,16 @@ hello true ```go var args struct { - Foo string `arg:"required"` - Bar bool + ID int `arg:"required"` + Timeout time.Duration } arg.MustParse(&args) ``` ```shell $ ./example -usage: example --foo FOO [--bar] -error: --foo is required +usage: example --id ID [--timeout TIMEOUT] +error: --id is required ``` ### Positional arguments @@ -161,6 +161,53 @@ usage: samples [--foo FOO] [--bar BAR] error: you must provide one of --foo and --bar ``` +### Custom parsing + +You can implement your own argument parser by implementing `encoding.TextUnmarshaler`: + +```go +package main + +import ( + "fmt" + "strings" + + "github.com/alexflint/go-arg" +) + +// Accepts command line arguments of the form "head.tail" +type NameDotName struct { + Head, Tail string +} + +func (n *NameDotName) UnmarshalText(b []byte) error { + s := string(b) + pos := strings.Index(s, ".") + if pos == -1 { + return fmt.Errorf("missing period in %s", s) + } + n.Head = s[:pos] + n.Tail = s[pos+1:] + return nil +} + +func main() { + var args struct { + Name *NameDotName + } + arg.MustParse(&args) + fmt.Printf("%#v\n", args.Name) +} +``` +```shell +$ ./example --name=foo.bar +&main.NameDotName{Head:"foo", Tail:"bar"} + +$ ./example --name=oops +usage: example [--name NAME] +error: error processing --name: missing period in "oops" +``` + ### Installation ```shell -- cgit v1.2.3 From 8fee8f7bbe5933cfe3ba8c82479b91a8e777e5a0 Mon Sep 17 00:00:00 2001 From: Alex Flint Date: Sat, 23 Jan 2016 20:11:51 -0800 Subject: move installation instructions to top --- README.md | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) (limited to 'README.md') diff --git a/README.md b/README.md index ae7aa6c..28ff388 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,10 @@ ## Structured argument parsing for Go +```shell +go get github.com/alexflint/go-arg +``` + Declare the command line arguments your program accepts by defining a struct. ```go @@ -208,12 +212,6 @@ usage: example [--name NAME] error: error processing --name: missing period in "oops" ``` -### Installation - -```shell -go get github.com/alexflint/go-arg -``` - ### Documentation https://godoc.org/github.com/alexflint/go-arg -- cgit v1.2.3