Skip to content

Commit 20335c6

Browse files
authored
Merge pull request #2091 from ldez/feat/improve-migration-guides
docs: improve migration guides render
2 parents 765f5fe + 08007fa commit 20335c6

File tree

2 files changed

+301
-176
lines changed

2 files changed

+301
-176
lines changed

docs/migrate-v1-to-v2.md

Lines changed: 121 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ If you find any issues not covered by this document, please post a
99
comment on [Issue 921](https://github.com/urfave/cli/issues/921) or
1010
consider sending a PR to help improve this guide.
1111

12-
# Flags before args
12+
## Flags before args
1313

1414
In v2 flags must come before args. This is more POSIX-compliant. You
1515
may need to update scripts, user documentation, etc.
@@ -26,69 +26,83 @@ This will not:
2626
cli hello rick --shout
2727
```
2828

29-
# Import string changed
29+
## Import string changed
3030

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"`
3338

3439
Check each file for this and make the change.
3540

3641
Shell command to find them all: `fgrep -rl github.com/urfave/cli *`
3742

38-
# Flag aliases are done differently
43+
## Flag aliases are done differently
3944

4045
Change `Name: "foo, f"` to `Name: "foo", Aliases: []string{"f"}`
4146

42-
* OLD:
43-
```go
44-
cli.StringFlag{
45-
Name: "config, cfg"
46-
}
47-
```
47+
=== "v1"
4848

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+
```
5663

5764
Sadly v2 doesn't warn you if a comma is in the name.
5865
(https://github.com/urfave/cli/issues/1103)
5966

60-
# EnvVar is now a list (EnvVars)
67+
## EnvVar is now a list (EnvVars)
6168

6269
Change `EnvVar: "XXXXX"` to `EnvVars: []string{"XXXXX"}` (plural).
6370

64-
* OLD:
65-
```go
66-
cli.StringFlag{
67-
EnvVar: "APP_LANG"
68-
}
69-
```
71+
=== "v1"
7072

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+
```
7778

78-
# Actions returns errors
79+
=== "v2"
80+
81+
```go
82+
cli.StringFlag{
83+
EnvVars: []string{"APP_LANG"}
84+
}
85+
```
86+
87+
## Actions returns errors
7988

8089
A command's `Action:` now returns an `error`.
8190

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 {`
8498

8599
Compiler messages you might see:
86100

87101
```
88102
cannot use func literal (type func(*cli.Context)) as type cli.ActionFunc in field value
89103
```
90104

91-
# cli.Flag changed
105+
## cli.Flag changed
92106

93107
`cli.Flag` is now a list of pointers.
94108

@@ -97,25 +111,27 @@ What this means to you:
97111
If you make a list of flags, add a `&` in front of each
98112
item. cli.BoolFlag, cli.StringFlag, etc.
99113

100-
* OLD:
101-
```go
102-
app.Flags = []cli.Flag{
103-
cli.BoolFlag{
104-
```
114+
=== "v1"
105115

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+
```
111127

112128
Compiler messages you might see:
113129

114130
```
115131
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
116132
```
117133

118-
# Commands are now lists of pointers
134+
## Commands are now lists of pointers
119135

120136
Occurrences of `[]Command` have been changed to `[]*Command`.
121137

@@ -125,8 +141,13 @@ Look for `[]cli.Command{}` and change it to `[]*cli.Command{}`
125141

126142
Example:
127143

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{}`
130151

131152
Compiler messages you might see:
132153

@@ -135,81 +156,94 @@ cannot convert commands (type []cli.Command) to type cli.CommandsByName
135156
cannot use commands (type []cli.Command) as type []*cli.Command in assignment
136157
```
137158

138-
# Lists of commands should be pointers
159+
## Lists of commands should be pointers
139160

140161
If you are building up a list of commands, the individual items should
141162
now be pointers.
142163

143-
* OLD: `cli.Command{`
144-
* NEW: `&cli.Command{`
164+
=== "v1"
165+
166+
`cli.Command{`
167+
168+
=== "v2"
169+
170+
`&cli.Command{`
145171

146172
Compiler messages you might see:
147173

148174
```
149175
cannot use cli.Command literal (type cli.Command) as type *cli.Command in argument to
150176
```
151177

152-
# Appending Commands
178+
## Appending Commands
153179

154180
Appending to a list of commands needs to be changed since the list is
155181
now pointers.
156182

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)`
159190

160191
Compiler messages you might see:
161192

162193
```
163194
cannot use c (type *cli.Command) as type cli.Command in append
164195
```
165196

166-
# GlobalString, GlobalBool and its likes are deprecated
197+
## GlobalString, GlobalBool and its likes are deprecated
167198

168199
Use simply `String` instead of `GlobalString`, `Bool` instead of `GlobalBool`
169200

170-
# BoolTFlag and BoolT are deprecated
201+
## BoolTFlag and BoolT are deprecated
171202

172203
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.
173204

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("")
192227

228+
Example:
193229

194-
# &cli.StringSlice{""} replaced with cli.NewStringSlice("")
230+
=== "v1"
195231

196-
Example:
232+
```go
233+
Value: &cli.StringSlice{""},
234+
```
197235

198-
* OLD:
236+
=== "v2"
237+
238+
```go
239+
Value: cli.NewStringSlice(""),
240+
```
199241

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
209243

210244
`cli.NewExitError()` is deprecated. Use `cli.Exit()` instead. ([Staticcheck](https://staticcheck.io/) detects this automatically and recommends replacement code.)
211245

212-
# Everything else
246+
## Everything else
213247

214248
Compile the code and work through any errors. Most should
215249
relate to issues listed above.

0 commit comments

Comments
 (0)