Add global env prefix example to README
* Also made newline separations around sections consistent * Also fixed usage of `p.Parse()` in env variable ignore example
This commit is contained in:
parent
9b5c76b1c4
commit
cb7e5c1905
50
README.md
50
README.md
|
@ -64,7 +64,7 @@ fmt.Println("Input:", args.Input)
|
||||||
fmt.Println("Output:", args.Output)
|
fmt.Println("Output:", args.Output)
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```shell
|
||||||
$ ./example src.txt x.out y.out z.out
|
$ ./example src.txt x.out y.out z.out
|
||||||
Input: src.txt
|
Input: src.txt
|
||||||
Output: [x.out y.out z.out]
|
Output: [x.out y.out z.out]
|
||||||
|
@ -80,12 +80,12 @@ arg.MustParse(&args)
|
||||||
fmt.Println("Workers:", args.Workers)
|
fmt.Println("Workers:", args.Workers)
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```shell
|
||||||
$ WORKERS=4 ./example
|
$ WORKERS=4 ./example
|
||||||
Workers: 4
|
Workers: 4
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```shell
|
||||||
$ WORKERS=4 ./example --workers=6
|
$ WORKERS=4 ./example --workers=6
|
||||||
Workers: 6
|
Workers: 6
|
||||||
```
|
```
|
||||||
|
@ -100,7 +100,7 @@ arg.MustParse(&args)
|
||||||
fmt.Println("Workers:", args.Workers)
|
fmt.Println("Workers:", args.Workers)
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```shell
|
||||||
$ NUM_WORKERS=4 ./example
|
$ NUM_WORKERS=4 ./example
|
||||||
Workers: 4
|
Workers: 4
|
||||||
```
|
```
|
||||||
|
@ -115,7 +115,7 @@ arg.MustParse(&args)
|
||||||
fmt.Println("Workers:", args.Workers)
|
fmt.Println("Workers:", args.Workers)
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```shell
|
||||||
$ WORKERS='1,99' ./example
|
$ WORKERS='1,99' ./example
|
||||||
Workers: [1 99]
|
Workers: [1 99]
|
||||||
```
|
```
|
||||||
|
@ -130,14 +130,35 @@ arg.MustParse(&args)
|
||||||
fmt.Println("Workers:", args.Workers)
|
fmt.Println("Workers:", args.Workers)
|
||||||
```
|
```
|
||||||
|
|
||||||
```
|
```shell
|
||||||
$ NUM_WORKERS=6 ./example
|
$ NUM_WORKERS=6 ./example
|
||||||
Workers: 6
|
Workers: 6
|
||||||
$ NUM_WORKERS=6 ./example --count 4
|
$ NUM_WORKERS=6 ./example --count 4
|
||||||
Workers: 4
|
Workers: 4
|
||||||
```
|
```
|
||||||
|
|
||||||
|
Configuring a global environment variable name prefix is also possible:
|
||||||
|
|
||||||
|
```go
|
||||||
|
var args struct {
|
||||||
|
Workers int `arg:"--count,env:NUM_WORKERS"`
|
||||||
|
}
|
||||||
|
|
||||||
|
p, err := arg.NewParser(arg.Config{
|
||||||
|
EnvPrefix: "MYAPP_",
|
||||||
|
}, &args)
|
||||||
|
|
||||||
|
p.MustParse(os.Args[1:])
|
||||||
|
fmt.Println("Workers:", args.Workers)
|
||||||
|
```
|
||||||
|
|
||||||
|
```shell
|
||||||
|
$ MYAPP_NUM_WORKERS=6 ./example
|
||||||
|
Workers: 6
|
||||||
|
```
|
||||||
|
|
||||||
### Usage strings
|
### Usage strings
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var args struct {
|
var args struct {
|
||||||
Input string `arg:"positional"`
|
Input string `arg:"positional"`
|
||||||
|
@ -185,6 +206,7 @@ arg.MustParse(&args)
|
||||||
```
|
```
|
||||||
|
|
||||||
#### Ignoring environment variables and/or default values
|
#### Ignoring environment variables and/or default values
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var args struct {
|
var args struct {
|
||||||
Test string `arg:"-t,env:TEST" default:"something"`
|
Test string `arg:"-t,env:TEST" default:"something"`
|
||||||
|
@ -195,10 +217,11 @@ p, err := arg.NewParser(arg.Config{
|
||||||
IgnoreDefault: true,
|
IgnoreDefault: true,
|
||||||
}, &args)
|
}, &args)
|
||||||
|
|
||||||
err = p.Parse(os.Args)
|
err = p.Parse(os.Args[1:])
|
||||||
```
|
```
|
||||||
|
|
||||||
### Arguments with multiple values
|
### Arguments with multiple values
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var args struct {
|
var args struct {
|
||||||
Database string
|
Database string
|
||||||
|
@ -214,6 +237,7 @@ Fetching the following IDs from foo: [1 2 3]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Arguments that can be specified multiple times, mixed with positionals
|
### Arguments that can be specified multiple times, mixed with positionals
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var args struct {
|
var args struct {
|
||||||
Commands []string `arg:"-c,separate"`
|
Commands []string `arg:"-c,separate"`
|
||||||
|
@ -231,6 +255,7 @@ Databases [db1 db2 db3]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Arguments with keys and values
|
### Arguments with keys and values
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var args struct {
|
var args struct {
|
||||||
UserIDs map[string]int
|
UserIDs map[string]int
|
||||||
|
@ -245,6 +270,7 @@ map[john:123 mary:456]
|
||||||
```
|
```
|
||||||
|
|
||||||
### Version strings
|
### Version strings
|
||||||
|
|
||||||
```go
|
```go
|
||||||
type args struct {
|
type args struct {
|
||||||
...
|
...
|
||||||
|
@ -269,6 +295,7 @@ someprogram 4.3.0
|
||||||
> If a `--version` flag is defined in `args` or any subcommand, it overrides the built-in versioning.
|
> If a `--version` flag is defined in `args` or any subcommand, it overrides the built-in versioning.
|
||||||
|
|
||||||
### Custom validation
|
### Custom validation
|
||||||
|
|
||||||
```go
|
```go
|
||||||
var args struct {
|
var args struct {
|
||||||
Foo string
|
Foo string
|
||||||
|
@ -310,13 +337,11 @@ Options:
|
||||||
--help, -h display this help and exit
|
--help, -h display this help and exit
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Embedded structs
|
### Embedded structs
|
||||||
|
|
||||||
The fields of embedded structs are treated just like regular fields:
|
The fields of embedded structs are treated just like regular fields:
|
||||||
|
|
||||||
```go
|
```go
|
||||||
|
|
||||||
type DatabaseOptions struct {
|
type DatabaseOptions struct {
|
||||||
Host string
|
Host string
|
||||||
Username string
|
Username string
|
||||||
|
@ -384,6 +409,7 @@ func main() {
|
||||||
fmt.Printf("%#v\n", args.Name)
|
fmt.Printf("%#v\n", args.Name)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ ./example --name=foo.bar
|
$ ./example --name=foo.bar
|
||||||
main.NameDotName{Head:"foo", Tail:"bar"}
|
main.NameDotName{Head:"foo", Tail:"bar"}
|
||||||
|
@ -420,6 +446,7 @@ func main() {
|
||||||
fmt.Printf("%#v\n", args.Name)
|
fmt.Printf("%#v\n", args.Name)
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ ./example --help
|
$ ./example --help
|
||||||
Usage: test [--name NAME]
|
Usage: test [--name NAME]
|
||||||
|
@ -445,6 +472,7 @@ var args struct {
|
||||||
}
|
}
|
||||||
arg.MustParse(&args)
|
arg.MustParse(&args)
|
||||||
```
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ ./example -h
|
$ ./example -h
|
||||||
Usage: example [--optimize LEVEL] [--maxjobs N] SRC [DST [DST ...]]
|
Usage: example [--optimize LEVEL] [--maxjobs N] SRC [DST [DST ...]]
|
||||||
|
@ -581,7 +609,6 @@ if p.Subcommand() == nil {
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|
||||||
### Custom handling of --help and --version
|
### Custom handling of --help and --version
|
||||||
|
|
||||||
The following reproduces the internal logic of `MustParse` for the simple case where
|
The following reproduces the internal logic of `MustParse` for the simple case where
|
||||||
|
@ -722,7 +749,8 @@ func main() {
|
||||||
p.WriteUsageForSubcommand(os.Stdout, p.SubcommandNames()...)
|
p.WriteUsageForSubcommand(os.Stdout, p.SubcommandNames()...)
|
||||||
os.Exit(1)
|
os.Exit(1)
|
||||||
}
|
}
|
||||||
}```
|
}
|
||||||
|
```
|
||||||
|
|
||||||
```shell
|
```shell
|
||||||
$ ./example --version
|
$ ./example --version
|
||||||
|
|
Loading…
Reference in New Issue