You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
@@ -9,7 +9,7 @@ If you find any issues not covered by this document, please post a
9
9
comment on [Issue 921](https://github.com/urfave/cli/issues/921) or
10
10
consider sending a PR to help improve this guide.
11
11
12
-
# Flags before args
12
+
##Flags before args
13
13
14
14
In v2 flags must come before args. This is more POSIX-compliant. You
15
15
may need to update scripts, user documentation, etc.
@@ -26,69 +26,83 @@ This will not:
26
26
cli hello rick --shout
27
27
```
28
28
29
-
# Import string changed
29
+
##Import string changed
30
30
31
-
* OLD: `import "github.com/urfave/cli"`
32
-
* NEW: `import "github.com/urfave/cli/v2"`
31
+
=== "v1"
32
+
33
+
`import "github.com/urfave/cli"`
34
+
35
+
=== "v2"
36
+
37
+
`import "github.com/urfave/cli/v2"`
33
38
34
39
Check each file for this and make the change.
35
40
36
41
Shell command to find them all: `fgrep -rl github.com/urfave/cli *`
37
42
38
-
# Flag aliases are done differently
43
+
##Flag aliases are done differently
39
44
40
45
Change `Name: "foo, f"` to `Name: "foo", Aliases: []string{"f"}`
41
46
42
-
* OLD:
43
-
```go
44
-
cli.StringFlag{
45
-
Name: "config, cfg"
46
-
}
47
-
```
47
+
=== "v1"
48
48
49
-
* NEW:
50
-
```go
51
-
cli.StringFlag{
52
-
Name: "config",
53
-
Aliases: []string{"cfg"},
54
-
}
55
-
```
49
+
```go
50
+
cli.StringFlag{
51
+
Name: "config, cfg"
52
+
}
53
+
```
54
+
55
+
=== "v2"
56
+
57
+
```go
58
+
cli.StringFlag{
59
+
Name: "config",
60
+
Aliases: []string{"cfg"},
61
+
}
62
+
```
56
63
57
64
Sadly v2 doesn't warn you if a comma is in the name.
58
65
(https://github.com/urfave/cli/issues/1103)
59
66
60
-
# EnvVar is now a list (EnvVars)
67
+
##EnvVar is now a list (EnvVars)
61
68
62
69
Change `EnvVar: "XXXXX"` to `EnvVars: []string{"XXXXX"}` (plural).
63
70
64
-
* OLD:
65
-
```go
66
-
cli.StringFlag{
67
-
EnvVar: "APP_LANG"
68
-
}
69
-
```
71
+
=== "v1"
70
72
71
-
* NEW:
72
-
```go
73
-
cli.StringFlag{
74
-
EnvVars: []string{"APP_LANG"}
75
-
}
76
-
```
73
+
```go
74
+
cli.StringFlag{
75
+
EnvVar: "APP_LANG"
76
+
}
77
+
```
77
78
78
-
# Actions returns errors
79
+
=== "v2"
80
+
81
+
```go
82
+
cli.StringFlag{
83
+
EnvVars: []string{"APP_LANG"}
84
+
}
85
+
```
86
+
87
+
## Actions returns errors
79
88
80
89
A command's `Action:` now returns an `error`.
81
90
82
-
* OLD: `Action: func(c *cli.Context) {`
83
-
* NEW: `Action: func(c *cli.Context) error {`
91
+
=== "v1"
92
+
93
+
`Action: func(c *cli.Context) {`
94
+
95
+
=== "v2"
96
+
97
+
`Action: func(c *cli.Context) error {`
84
98
85
99
Compiler messages you might see:
86
100
87
101
```
88
102
cannot use func literal (type func(*cli.Context)) as type cli.ActionFunc in field value
89
103
```
90
104
91
-
# cli.Flag changed
105
+
##cli.Flag changed
92
106
93
107
`cli.Flag` is now a list of pointers.
94
108
@@ -97,25 +111,27 @@ What this means to you:
97
111
If you make a list of flags, add a `&` in front of each
98
112
item. cli.BoolFlag, cli.StringFlag, etc.
99
113
100
-
* OLD:
101
-
```go
102
-
app.Flags = []cli.Flag{
103
-
cli.BoolFlag{
104
-
```
114
+
=== "v1"
105
115
106
-
* NEW:
107
-
```go
108
-
app.Flags = []cli.Flag{
109
-
&cli.BoolFlag{
110
-
```
116
+
```go
117
+
app.Flags = []cli.Flag{
118
+
cli.BoolFlag{
119
+
```
120
+
121
+
=== "v2"
122
+
123
+
```go
124
+
app.Flags = []cli.Flag{
125
+
&cli.BoolFlag{
126
+
```
111
127
112
128
Compiler messages you might see:
113
129
114
130
```
115
131
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
116
132
```
117
133
118
-
# Commands are now lists of pointers
134
+
##Commands are now lists of pointers
119
135
120
136
Occurrences of `[]Command` have been changed to `[]*Command`.
121
137
@@ -125,8 +141,13 @@ Look for `[]cli.Command{}` and change it to `[]*cli.Command{}`
125
141
126
142
Example:
127
143
128
-
* OLD: `var commands = []cli.Command{}`
129
-
* NEW: `var commands = []*cli.Command{}`
144
+
=== "v1"
145
+
146
+
`var commands = []cli.Command{}`
147
+
148
+
=== "v2"
149
+
150
+
`var commands = []*cli.Command{}`
130
151
131
152
Compiler messages you might see:
132
153
@@ -135,81 +156,94 @@ cannot convert commands (type []cli.Command) to type cli.CommandsByName
135
156
cannot use commands (type []cli.Command) as type []*cli.Command in assignment
136
157
```
137
158
138
-
# Lists of commands should be pointers
159
+
##Lists of commands should be pointers
139
160
140
161
If you are building up a list of commands, the individual items should
141
162
now be pointers.
142
163
143
-
* OLD: `cli.Command{`
144
-
* NEW: `&cli.Command{`
164
+
=== "v1"
165
+
166
+
`cli.Command{`
167
+
168
+
=== "v2"
169
+
170
+
`&cli.Command{`
145
171
146
172
Compiler messages you might see:
147
173
148
174
```
149
175
cannot use cli.Command literal (type cli.Command) as type *cli.Command in argument to
150
176
```
151
177
152
-
# Appending Commands
178
+
##Appending Commands
153
179
154
180
Appending to a list of commands needs to be changed since the list is
155
181
now pointers.
156
182
157
-
* OLD: `commands = append(commands, *c)`
158
-
* NEW: `commands = append(commands, c)`
183
+
=== "v1"
184
+
185
+
`commands = append(commands, *c)`
186
+
187
+
=== "v2"
188
+
189
+
`commands = append(commands, c)`
159
190
160
191
Compiler messages you might see:
161
192
162
193
```
163
194
cannot use c (type *cli.Command) as type cli.Command in append
164
195
```
165
196
166
-
# GlobalString, GlobalBool and its likes are deprecated
197
+
##GlobalString, GlobalBool and its likes are deprecated
167
198
168
199
Use simply `String` instead of `GlobalString`, `Bool` instead of `GlobalBool`
169
200
170
-
# BoolTFlag and BoolT are deprecated
201
+
##BoolTFlag and BoolT are deprecated
171
202
172
203
BoolTFlag was a Bool Flag with its default value set to true and BoolT was used to find any BoolTFlag used locally, so both are deprecated.
173
204
174
-
* OLD:
175
-
176
-
```go
177
-
cli.BoolTFlag{
178
-
Name: FlagName,
179
-
Usage: FlagUsage,
180
-
EnvVar: "FLAG_ENV_VAR",
181
-
}
182
-
```
183
-
* NEW:
184
-
```go
185
-
cli.BoolFlag{
186
-
Name: FlagName,
187
-
Value: true,
188
-
Usage: FlagUsage,
189
-
EnvVar: "FLAG_ENV_VAR",
190
-
}
191
-
```
205
+
=== "v1"
206
+
207
+
```go
208
+
cli.BoolTFlag{
209
+
Name: FlagName,
210
+
Usage: FlagUsage,
211
+
EnvVar: "FLAG_ENV_VAR",
212
+
}
213
+
```
214
+
215
+
=== "v2"
216
+
217
+
```go
218
+
cli.BoolFlag{
219
+
Name: FlagName,
220
+
Value: true,
221
+
Usage: FlagUsage,
222
+
EnvVar: "FLAG_ENV_VAR",
223
+
}
224
+
```
225
+
226
+
## &cli.StringSlice{""} replaced with cli.NewStringSlice("")
192
227
228
+
Example:
193
229
194
-
# &cli.StringSlice{""} replaced with cli.NewStringSlice("")
230
+
=== "v1"
195
231
196
-
Example:
232
+
```go
233
+
Value: &cli.StringSlice{""},
234
+
```
197
235
198
-
* OLD:
236
+
=== "v2"
237
+
238
+
```go
239
+
Value: cli.NewStringSlice(""),
240
+
```
199
241
200
-
```go
201
-
Value: &cli.StringSlice{""},
202
-
```
203
-
* NEW:
204
-
```go
205
-
Value: cli.NewStringSlice(""),
206
-
}
207
-
```
208
-
# Replace deprecated functions
242
+
## Replace deprecated functions
209
243
210
244
`cli.NewExitError()` is deprecated. Use `cli.Exit()` instead. ([Staticcheck](https://staticcheck.io/) detects this automatically and recommends replacement code.)
211
245
212
-
# Everything else
246
+
##Everything else
213
247
214
248
Compile the code and work through any errors. Most should
0 commit comments