-
Notifications
You must be signed in to change notification settings - Fork 0
/
example_test.go
155 lines (136 loc) · 4.13 KB
/
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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
package args_test
import (
"fmt"
"os"
"time"
"github.com/jpvetterli/args"
)
func ExampleParser_PrintDoc() {
a := args.NewParser()
a.Doc(
"Usage: %s parameter...\n",
"The command does nothing but is has many parameters.",
"",
"Parameters:")
var files []string
var help bool
var short string
long := []string{"a1", "b2"}
sl := make([]int, 0)
var ar [4]float64
mp := map[string]uint8{"foo": 42}
a.Def("", &files).Aka("file").Doc("foo takes any number of file names")
a.Def("help", &help).Aka("-h").Doc("provide help").Opt()
a.Def("short", &short).Opt().Doc("short is a parameter with a short name")
a.Def("long-name", &long).Doc(
"long-name is a parameter with a name longer than 8",
"It also has a long explanation.")
a.Def("slice", &sl).Doc("slice is a parameter taking any number of values")
a.Def("array", &ar).Doc("array is a parameter taking 4 values").Split(`\s*:\s*`)
a.Def("map", &mp).Doc("map is a parameter taking key-value pairs")
a.PrintDoc(os.Stdout, "foo")
a.PrintConfig(os.Stdout)
// output:
// Usage: foo parameter...
// The command does nothing but is has many parameters.
//
// Parameters:
// (nameless), file
// foo takes any number of file names
// type: string, any number of values
// help, -h provide help
// type: bool, optional (default: false)
// short short is a parameter with a short name
// type: string, optional (default: )
// long-name
// long-name is a parameter with a name longer than 8
// It also has a long explanation.
// type: string, 0-2 values (default: [a1 b2])
// slice slice is a parameter taking any number of values
// type: int, any number of values
// array array is a parameter taking 4 values
// type: float64, split: \s*:\s*, exactly 4 values
// map map is a parameter taking key-value pairs
// type: map[string]uint8 (default: map[foo:42])
//
// Special characters:
// $ symbol prefix
// [ open quote
// ] close quote
// = separator
// \ escape
//
// Built-in operators:
// cond conditional parsing (if, then, else)
// dump print parameters and symbols on standard error (comment)
// import import environment variables as symbols
// include include a file or extract name-values (keys, extractor)
// macro expand symbols
// reset remove symbols
// -- do not parse the value (= comment out)
}
func ExampleParam_Scan() {
a := args.NewParser()
var target time.Time
scanner := func(value string, target interface{}) error {
if s, ok := target.(*time.Time); ok {
if t, err := time.Parse("2006-01-02 15:04:05", value); err == nil {
*s = t
} else {
return err
}
return nil
}
return fmt.Errorf(`time scanner error: "%s", *time.Time target required, not %T`, value, target)
}
a.Def("time", &target).Scan(scanner).Aka("datetime").Doc(`specify time in the format "yyyy-mm-dd hh:mm:ss"`)
a.Parse("time=[2015-06-30 15:32:00]")
fmt.Println(target.String())
// output:
// 2015-06-30 15:32:00 +0000 UTC
}
func ExampleParser_Parse() {
a := args.NewParser()
var s string
var f [3]float64
a.Def("foo", &s)
a.Def("bar", &f).Split(":")
err := a.Parse("foo=bar bar=1:2:3:4")
if err != nil {
fmt.Println(err.Error())
fmt.Println("Oops... let's try again")
err = a.Parse("foo=quux bar=1:2:3")
if err != nil {
fmt.Println(err.Error())
}
fmt.Println(s)
fmt.Println(f)
}
// output:
// Parse error on bar: too many values specified, expected 3
// Oops... let's try again
// quux
// [1 2 3]
}
func ExampleParam_Verbatim() {
a := args.NewParser()
cmd1 := ""
cmd2 := ""
a.Def("cmd1", &cmd1).Verbatim() // IMPORTANT: verbatim
a.Def("cmd2", &cmd2).Verbatim()
a.Parse("$MACRO = [arg1=$[ARG1] arg2=$[ARG2]] " +
"cmd1=[$ARG1=x $ARG2=y $[MACRO]] " +
"cmd2=[$ARG1=a $ARG2=b $[MACRO]]")
for _, s := range []string{cmd1, cmd2} {
a = args.NewParser() // IMPORTANT: get a new parser
arg1 := ""
arg2 := ""
a.Def("arg1", &arg1)
a.Def("arg2", &arg2)
a.Parse(s)
fmt.Println(arg1, arg2)
}
// output:
// x y
// a b
}