-
Notifications
You must be signed in to change notification settings - Fork 0
/
parser_example_test.go
72 lines (55 loc) · 1.46 KB
/
parser_example_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package yag_test
import (
"fmt"
"github.com/zoido/yag-config"
)
func ExampleParser_Usage() {
type config struct {
RequiredOption string
CustomEnvOption string
NoFlagOption string
NoEnvOption string
}
y := yag.New(yag.WithEnvPrefix("MY_APP_"))
cfg := &config{}
y.String(&cfg.RequiredOption, "required_option", "sets required option", yag.Required())
y.String(
&cfg.CustomEnvOption,
"custom_env_option",
"sets custom env option",
yag.FromEnv("MY_OPTION_TWO"),
)
y.String(&cfg.NoFlagOption, "no_flag_option", "sets np flag option", yag.NoFlag())
y.String(&cfg.NoFlagOption, "no_env_option", "sets no env option", yag.NoEnv())
fmt.Print(y.Usage())
// Output:
// -required_option ($MY_APP_REQUIRED_OPTION) [required]
// sets required option
// -custom_env_option ($MY_OPTION_TWO)
// sets custom env option
// $MY_APP_NO_FLAG_OPTION
// sets np flag option
// -no_env_option
// sets no env option
}
func ExampleErrHelp() {
var foo string
y := yag.New()
y.String(&foo, "foo", "sets Foo")
err := y.Parse([]string{"--help"})
if err == yag.ErrHelp {
fmt.Printf("--help flag passed")
}
// Output: --help flag passed
}
func ExampleParser_Args() {
var foo string
y := yag.New()
y.String(&foo, "foo", "sets Foo")
err := y.Parse([]string{"--foo", "x", "arg1", "arg2", "arg3"})
if err != nil {
fmt.Printf("error: %s", err)
}
fmt.Printf("foo: %q, args: %v", foo, y.Args())
// Output: foo: "x", args: [arg1 arg2 arg3]
}