forked from alecthomas/kong
-
Notifications
You must be signed in to change notification settings - Fork 0
/
tag_test.go
201 lines (180 loc) Β· 4.66 KB
/
tag_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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package kong_test
import (
"strings"
"testing"
"github.com/alecthomas/assert/v2"
"github.com/alecthomas/kong"
)
func TestDefaultValueForOptionalArg(t *testing.T) {
var cli struct {
Arg string `kong:"arg,optional,default='π'"`
}
p := mustNew(t, &cli)
_, err := p.Parse(nil)
assert.NoError(t, err)
assert.Equal(t, "π", cli.Arg)
}
func TestNoValueInTag(t *testing.T) {
var cli struct {
Empty1 string `kong:"default"`
Empty2 string `kong:"default="`
}
p := mustNew(t, &cli)
_, err := p.Parse(nil)
assert.NoError(t, err)
assert.Equal(t, "", cli.Empty1)
assert.Equal(t, "", cli.Empty2)
}
func TestCommaInQuotes(t *testing.T) {
var cli struct {
Numbers string `kong:"default='1,2'"`
}
p := mustNew(t, &cli)
_, err := p.Parse(nil)
assert.NoError(t, err)
assert.Equal(t, "1,2", cli.Numbers)
}
func TestBadString(t *testing.T) {
var cli struct {
Numbers string `kong:"default='yay'n"`
}
_, err := kong.New(&cli)
assert.Error(t, err)
}
func TestNoQuoteEnd(t *testing.T) {
var cli struct {
Numbers string `kong:"default='yay"`
}
_, err := kong.New(&cli)
assert.Error(t, err)
}
func TestEscapedQuote(t *testing.T) {
var cli struct {
DoYouKnow string `kong:"default='i don\\'t know'"`
}
p := mustNew(t, &cli)
_, err := p.Parse(nil)
assert.NoError(t, err)
assert.Equal(t, "i don't know", cli.DoYouKnow)
}
func TestBareTags(t *testing.T) {
var cli struct {
Cmd struct {
Arg string `arg`
Flag string `required default:"π"`
} `cmd`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"cmd", "arg", "--flag=hi"})
assert.NoError(t, err)
assert.Equal(t, "hi", cli.Cmd.Flag)
assert.Equal(t, "arg", cli.Cmd.Arg)
}
func TestBareTagsWithJsonTag(t *testing.T) {
var cli struct {
Cmd struct {
Arg string `json:"-" optional arg`
Flag string `json:"best_flag" default:"\"'π'\""`
} `cmd json:"CMD"`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"cmd"})
assert.NoError(t, err)
assert.Equal(t, "\"'π'\"", cli.Cmd.Flag)
assert.Equal(t, "", cli.Cmd.Arg)
}
func TestManySeps(t *testing.T) {
var cli struct {
Arg string `arg optional default:"hi"`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{})
assert.NoError(t, err)
assert.Equal(t, "hi", cli.Arg)
}
func TestTagSetOnEmbeddedStruct(t *testing.T) {
type Embedded struct {
Key string `help:"A key from ${where}."`
}
var cli struct {
Embedded `set:"where=somewhere"`
}
buf := &strings.Builder{}
p := mustNew(t, &cli, kong.Writers(buf, buf), kong.Exit(func(int) {}))
_, err := p.Parse([]string{"--help"})
assert.NoError(t, err)
assert.Contains(t, buf.String(), `A key from somewhere.`)
}
func TestTagSetOnCommand(t *testing.T) {
type Command struct {
Key string `help:"A key from ${where}."`
}
var cli struct {
Command Command `set:"where=somewhere" cmd:""`
}
buf := &strings.Builder{}
p := mustNew(t, &cli, kong.Writers(buf, buf), kong.Exit(func(int) {}))
_, err := p.Parse([]string{"command", "--help"})
assert.NoError(t, err)
assert.Contains(t, buf.String(), `A key from somewhere.`)
}
func TestTagSetOnFlag(t *testing.T) {
var cli struct {
Flag string `set:"where=somewhere" help:"A key from ${where}."`
}
buf := &strings.Builder{}
p := mustNew(t, &cli, kong.Writers(buf, buf), kong.Exit(func(int) {}))
_, err := p.Parse([]string{"--help"})
assert.NoError(t, err)
assert.Contains(t, buf.String(), `A key from somewhere.`)
}
func TestTagAliases(t *testing.T) {
type Command struct {
Arg string `arg help:"Some arg"`
}
var cli struct {
Cmd Command `cmd aliases:"alias1, alias2"`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"alias1", "arg"})
assert.NoError(t, err)
assert.Equal(t, "arg", cli.Cmd.Arg)
_, err = p.Parse([]string{"alias2", "arg"})
assert.NoError(t, err)
assert.Equal(t, "arg", cli.Cmd.Arg)
}
func TestTagAliasesConflict(t *testing.T) {
type Command struct {
Arg string `arg help:"Some arg"`
}
var cli struct {
Cmd Command `cmd hidden aliases:"other-cmd"`
OtherCmd Command `cmd`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"other-cmd", "arg"})
assert.NoError(t, err)
assert.Equal(t, "arg", cli.OtherCmd.Arg)
}
func TestTagAliasesSub(t *testing.T) {
type SubCommand struct {
Arg string `arg help:"Some arg"`
}
type Command struct {
SubCmd SubCommand `cmd aliases:"other-sub-cmd"`
}
var cli struct {
Cmd Command `cmd hidden`
}
p := mustNew(t, &cli)
_, err := p.Parse([]string{"cmd", "other-sub-cmd", "arg"})
assert.NoError(t, err)
assert.Equal(t, "arg", cli.Cmd.SubCmd.Arg)
}
func TestInvalidRuneErrors(t *testing.T) {
cli := struct {
Flag bool `short:"invalid"`
}{}
_, err := kong.New(&cli)
assert.EqualError(t, err, "<anonymous struct>.Flag: invalid short flag name \"invalid\": invalid rune")
}