summaryrefslogtreecommitdiff
path: root/README.md
diff options
context:
space:
mode:
authorHugo Hromic <[email protected]>2024-07-01 23:16:51 +0100
committerHugo Hromic <[email protected]>2024-09-07 13:07:21 +0100
commitcb7e5c190570d24d0768224f08a11ce8bf607b41 (patch)
tree511c4fde9d073f3b90105a3affc6d83820e5295e /README.md
parent9b5c76b1c4eda926cbec2442dd24021653178b6b (diff)
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
Diffstat (limited to 'README.md')
-rw-r--r--README.md50
1 files changed, 39 insertions, 11 deletions
diff --git a/README.md b/README.md
index 761af56..e9075ba 100644
--- a/README.md
+++ b/README.md
@@ -64,7 +64,7 @@ fmt.Println("Input:", args.Input)
fmt.Println("Output:", args.Output)
```
-```
+```shell
$ ./example src.txt x.out y.out z.out
Input: src.txt
Output: [x.out y.out z.out]
@@ -80,12 +80,12 @@ arg.MustParse(&args)
fmt.Println("Workers:", args.Workers)
```
-```
+```shell
$ WORKERS=4 ./example
Workers: 4
```
-```
+```shell
$ WORKERS=4 ./example --workers=6
Workers: 6
```
@@ -100,7 +100,7 @@ arg.MustParse(&args)
fmt.Println("Workers:", args.Workers)
```
-```
+```shell
$ NUM_WORKERS=4 ./example
Workers: 4
```
@@ -115,7 +115,7 @@ arg.MustParse(&args)
fmt.Println("Workers:", args.Workers)
```
-```
+```shell
$ WORKERS='1,99' ./example
Workers: [1 99]
```
@@ -130,14 +130,35 @@ arg.MustParse(&args)
fmt.Println("Workers:", args.Workers)
```
-```
+```shell
$ NUM_WORKERS=6 ./example
Workers: 6
$ NUM_WORKERS=6 ./example --count 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
+
```go
var args struct {
Input string `arg:"positional"`
@@ -185,6 +206,7 @@ arg.MustParse(&args)
```
#### Ignoring environment variables and/or default values
+
```go
var args struct {
Test string `arg:"-t,env:TEST" default:"something"`
@@ -195,10 +217,11 @@ p, err := arg.NewParser(arg.Config{
IgnoreDefault: true,
}, &args)
-err = p.Parse(os.Args)
+err = p.Parse(os.Args[1:])
```
### Arguments with multiple values
+
```go
var args struct {
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
+
```go
var args struct {
Commands []string `arg:"-c,separate"`
@@ -231,6 +255,7 @@ Databases [db1 db2 db3]
```
### Arguments with keys and values
+
```go
var args struct {
UserIDs map[string]int
@@ -245,6 +270,7 @@ map[john:123 mary:456]
```
### Version strings
+
```go
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.
### Custom validation
+
```go
var args struct {
Foo string
@@ -310,13 +337,11 @@ Options:
--help, -h display this help and exit
```
-
### Embedded structs
The fields of embedded structs are treated just like regular fields:
```go
-
type DatabaseOptions struct {
Host string
Username string
@@ -384,6 +409,7 @@ func main() {
fmt.Printf("%#v\n", args.Name)
}
```
+
```shell
$ ./example --name=foo.bar
main.NameDotName{Head:"foo", Tail:"bar"}
@@ -420,6 +446,7 @@ func main() {
fmt.Printf("%#v\n", args.Name)
}
```
+
```shell
$ ./example --help
Usage: test [--name NAME]
@@ -445,6 +472,7 @@ var args struct {
}
arg.MustParse(&args)
```
+
```shell
$ ./example -h
Usage: example [--optimize LEVEL] [--maxjobs N] SRC [DST [DST ...]]
@@ -581,7 +609,6 @@ if p.Subcommand() == nil {
}
```
-
### Custom handling of --help and --version
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()...)
os.Exit(1)
}
-}```
+}
+```
```shell
$ ./example --version