update readme with new additions
This commit is contained in:
parent
b5933a0ea8
commit
95761fa14a
55
README.md
55
README.md
|
@ -24,16 +24,16 @@ hello true
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo string `arg:"required"`
|
ID int `arg:"required"`
|
||||||
Bar bool
|
Timeout time.Duration
|
||||||
}
|
}
|
||||||
arg.MustParse(&args)
|
arg.MustParse(&args)
|
||||||
```
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ ./example
|
$ ./example
|
||||||
usage: example --foo FOO [--bar]
|
usage: example --id ID [--timeout TIMEOUT]
|
||||||
error: --foo is required
|
error: --id is required
|
||||||
```
|
```
|
||||||
|
|
||||||
### Positional arguments
|
### Positional arguments
|
||||||
|
@ -161,6 +161,53 @@ usage: samples [--foo FOO] [--bar BAR]
|
||||||
error: you must provide one of --foo and --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
|
### Installation
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
|
|
Loading…
Reference in New Issue