From 6e13281e98c17733ce0ae4616abd1cf9712141a0 Mon Sep 17 00:00:00 2001 From: Hugo Hromic Date: Mon, 1 Jul 2024 23:16:51 +0100 Subject: [PATCH] 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 --- README.md | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 4d23034..5e837ef 100644 --- a/README.md +++ b/README.md @@ -137,7 +137,28 @@ $ 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) +``` + +``` +$ MYAPP_NUM_WORKERS=6 ./example +Workers: 6 +``` + ### Usage strings + ```go var args struct { Input string `arg:"positional"` @@ -212,10 +233,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 @@ -231,6 +253,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"` @@ -248,6 +271,7 @@ Databases [db1 db2 db3] ``` ### Arguments with keys and values + ```go var args struct { UserIDs map[string]int @@ -262,6 +286,7 @@ map[john:123 mary:456] ``` ### Custom validation + ```go var args struct { Foo string