Debian Codegraph @

docs/migrate-v1-to-v2.md

raw symbols SCIP
1 # Migration Guide: v1 to v2
2
3 v2 has a number of breaking changes but converting is relatively
4 straightforward: make the changes documented below then resolve any
5 compiler errors. We hope this will be sufficient for most typical
6 users.
7
8 If you find any issues not covered by this document, please post a
9 comment on [Issue 921](https://github.com/urfave/cli/issues/921) or
10 consider sending a PR to help improve this guide.
11
12 ## Flags before args
13
14 In v2 flags must come before args. This is more POSIX-compliant. You
15 may need to update scripts, user documentation, etc.
16
17 This will work:
18
19 ```
20 cli hello --shout rick
21 ```
22
23 This will not:
24
25 ```
26 cli hello rick --shout
27 ```
28
29 ## Import string changed
30
31 === "v1"
32
33 `import "github.com/urfave/cli"`
34
35 === "v2"
36
37 `import "github.com/urfave/cli/v2"`
38
39 Check each file for this and make the change.
40
41 Shell command to find them all: `fgrep -rl github.com/urfave/cli *`
42
43 ## Flag aliases are done differently
44
45 Change `Name: "foo, f"` to `Name: "foo", Aliases: []string{"f"}`
46
47 === "v1"
48
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 ```
63
64 Sadly v2 doesn't warn you if a comma is in the name.
65 (https://github.com/urfave/cli/issues/1103)
66
67 ## EnvVar is now a list (EnvVars)
68
69 Change `EnvVar: "XXXXX"` to `EnvVars: []string{"XXXXX"}` (plural).
70
71 === "v1"
72
73 ```go
74 cli.StringFlag{
75 EnvVar: "APP_LANG"
76 }
77 ```
78
79 === "v2"
80
81 ```go
82 cli.StringFlag{
83 EnvVars: []string{"APP_LANG"}
84 }
85 ```
86
87 ## Actions returns errors
88
89 A command's `Action:` now returns an `error`.
90
91 === "v1"
92
93 `Action: func(c *cli.Context) {`
94
95 === "v2"
96
97 `Action: func(c *cli.Context) error {`
98
99 Compiler messages you might see:
100
101 ```
102 cannot use func literal (type func(*cli.Context)) as type cli.ActionFunc in field value
103 ```
104
105 ## cli.Flag changed
106
107 `cli.Flag` is now a list of pointers.
108
109 What this means to you:
110
111 If you make a list of flags, add a `&` in front of each
112 item. cli.BoolFlag, cli.StringFlag, etc.
113
114 === "v1"
115
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 ```
127
128 Compiler messages you might see:
129
130 ```
131 cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
132 ```
133
134 ## Commands are now lists of pointers
135
136 Occurrences of `[]Command` have been changed to `[]*Command`.
137
138 What this means to you:
139
140 Look for `[]cli.Command{}` and change it to `[]*cli.Command{}`
141
142 Example:
143
144 === "v1"
145
146 `var commands = []cli.Command{}`
147
148 === "v2"
149
150 `var commands = []*cli.Command{}`
151
152 Compiler messages you might see:
153
154 ```
155 cannot convert commands (type []cli.Command) to type cli.CommandsByName
156 cannot use commands (type []cli.Command) as type []*cli.Command in assignment
157 ```
158
159 ## Lists of commands should be pointers
160
161 If you are building up a list of commands, the individual items should
162 now be pointers.
163
164 === "v1"
165
166 `cli.Command{`
167
168 === "v2"
169
170 `&cli.Command{`
171
172 Compiler messages you might see:
173
174 ```
175 cannot use cli.Command literal (type cli.Command) as type *cli.Command in argument to
176 ```
177
178 ## Appending Commands
179
180 Appending to a list of commands needs to be changed since the list is
181 now pointers.
182
183 === "v1"
184
185 `commands = append(commands, *c)`
186
187 === "v2"
188
189 `commands = append(commands, c)`
190
191 Compiler messages you might see:
192
193 ```
194 cannot use c (type *cli.Command) as type cli.Command in append
195 ```
196
197 ## GlobalString, GlobalBool and its likes are deprecated
198
199 Use simply `String` instead of `GlobalString`, `Bool` instead of `GlobalBool`
200
201 ## BoolTFlag and BoolT are deprecated
202
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.
204
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("")
227
228 Example:
229
230 === "v1"
231
232 ```go
233 Value: &cli.StringSlice{""},
234 ```
235
236 === "v2"
237
238 ```go
239 Value: cli.NewStringSlice(""),
240 ```
241
242 ## Replace deprecated functions
243
244 `cli.NewExitError()` is deprecated. Use `cli.Exit()` instead. ([Staticcheck](https://staticcheck.io/) detects this automatically and recommends replacement code.)
245
246 ## Everything else
247
248 Compile the code and work through any errors. Most should
249 relate to issues listed above.
250
251 Once it compiles, test the command. Review the output of `-h` or any
252 help messages to verify they match the intended flags and subcommands.
253 Then test the program itself.
254
255 If you find any issues not covered by this document please let us know
256 by submitting a comment on
257 [Issue 921](https://github.com/urfave/cli/issues/921)
258 so that others can benefit.