| 1 | # Migration Guide: v2 to v3 |
| 2 | |
| 3 | v3 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 [the discussion](https://github.com/urfave/cli/discussions/2084) or |
| 10 | consider sending a PR to help improve this guide. |
| 11 | |
| 12 | ## New Import |
| 13 | |
| 14 | === "v2" |
| 15 | |
| 16 | `import "github.com/urfave/cli/v2"` |
| 17 | |
| 18 | === "v3" |
| 19 | |
| 20 | `import "github.com/urfave/cli/v3"` |
| 21 | |
| 22 | Check each file for this and make the change. |
| 23 | |
| 24 | Shell command to find them all: `fgrep -rl github.com/urfave/cli/v2 *` |
| 25 | |
| 26 | ## New Names |
| 27 | |
| 28 | ### cli.App |
| 29 | |
| 30 | === "v2" |
| 31 | |
| 32 | ```go |
| 33 | cli.App{ |
| 34 | // ... |
| 35 | } |
| 36 | ``` |
| 37 | |
| 38 | === "v3" |
| 39 | |
| 40 | ```go |
| 41 | cli.Command{ |
| 42 | // ... |
| 43 | } |
| 44 | ``` |
| 45 | |
| 46 | ### cli.App.EnableBashCompletion |
| 47 | |
| 48 | === "v2" |
| 49 | |
| 50 | ```go |
| 51 | cli.App{ |
| 52 | EnableBashCompletion: true, |
| 53 | } |
| 54 | ``` |
| 55 | |
| 56 | === "v3" |
| 57 | |
| 58 | ```go |
| 59 | cli.Command{ |
| 60 | EnableShellCompletion: true, |
| 61 | } |
| 62 | ``` |
| 63 | |
| 64 | ### cli.App.CustomAppHelpTemplate |
| 65 | |
| 66 | === "v2" |
| 67 | |
| 68 | ```go |
| 69 | cli.App{ |
| 70 | CustomAppHelpTemplate: "...", |
| 71 | } |
| 72 | ``` |
| 73 | |
| 74 | === "v3" |
| 75 | |
| 76 | ```go |
| 77 | cli.Command{ |
| 78 | CustomRootCommandHelpTemplate: "...", |
| 79 | } |
| 80 | ``` |
| 81 | |
| 82 | ### cli.App.RunContext |
| 83 | |
| 84 | === "v2" |
| 85 | |
| 86 | ```go |
| 87 | (&cli.App{}).RunContext(context.Background(), os.Args) |
| 88 | ``` |
| 89 | |
| 90 | === "v3" |
| 91 | |
| 92 | ```go |
| 93 | (&cli.Command{}).Run(context.Background(), os.Args) |
| 94 | ``` |
| 95 | |
| 96 | ### cli.App.BashComplete |
| 97 | |
| 98 | === "v2" |
| 99 | |
| 100 | ```go |
| 101 | cli.App{ |
| 102 | BashComplete: func(ctx *cli.Context) {}, |
| 103 | } |
| 104 | ``` |
| 105 | |
| 106 | === "v3" |
| 107 | |
| 108 | ```go |
| 109 | cli.Command{ |
| 110 | ShellComplete: func(ctx context.Context, cmd *cli.Command) {}, |
| 111 | } |
| 112 | ``` |
| 113 | |
| 114 | ### cli.Command.Subcommands |
| 115 | |
| 116 | === "v2" |
| 117 | |
| 118 | ```go |
| 119 | cli.Command{ |
| 120 | Subcommands: []*cli.Command{}, |
| 121 | } |
| 122 | ``` |
| 123 | |
| 124 | === "v3" |
| 125 | |
| 126 | ```go |
| 127 | cli.Command{ |
| 128 | Commands: []*cli.Command{}, |
| 129 | } |
| 130 | ``` |
| 131 | |
| 132 | ## Sources |
| 133 | |
| 134 | ### FilePath |
| 135 | |
| 136 | === "v2" |
| 137 | |
| 138 | ```go |
| 139 | cli.StringFlag{ |
| 140 | FilePath: "/path/to/foo", |
| 141 | } |
| 142 | ``` |
| 143 | |
| 144 | === "v3" |
| 145 | |
| 146 | ```go |
| 147 | cli.StringFlag{ |
| 148 | Sources: cli.Files("/path/to/foo"), |
| 149 | } |
| 150 | ``` |
| 151 | |
| 152 | or |
| 153 | |
| 154 | ```go |
| 155 | cli.StringFlag{ |
| 156 | Sources: cli.NewValueSourceChain( |
| 157 | cli.File("/path/to/foo"), |
| 158 | ), |
| 159 | } |
| 160 | ``` |
| 161 | |
| 162 | ### EnvVars |
| 163 | |
| 164 | === "v2" |
| 165 | |
| 166 | ```go |
| 167 | cli.StringFlag{ |
| 168 | EnvVars: []string{"APP_LANG"}, |
| 169 | } |
| 170 | ``` |
| 171 | |
| 172 | === "v3" |
| 173 | |
| 174 | ```go |
| 175 | cli.StringFlag{ |
| 176 | Sources: cli.EnvVars("APP_LANG"), |
| 177 | } |
| 178 | ``` |
| 179 | |
| 180 | or |
| 181 | |
| 182 | ```go |
| 183 | cli.StringFlag{ |
| 184 | Sources: cli.NewValueSourceChain( |
| 185 | cli.EnvVar("APP_LANG"), |
| 186 | ), |
| 187 | } |
| 188 | ``` |
| 189 | |
| 190 | ### Altsrc |
| 191 | |
| 192 | #### Altsrc is now a dedicated module |
| 193 | |
| 194 | === "v2" |
| 195 | |
| 196 | `import "github.com/urfave/cli/v2/altsrc"` |
| 197 | |
| 198 | === "v3" |
| 199 | |
| 200 | `import altsrc "github.com/urfave/cli-altsrc/v3"` |
| 201 | |
| 202 | #### Altsrc is now a value source for CLI |
| 203 | |
| 204 | === "v2" |
| 205 | |
| 206 | ```go |
| 207 | altsrc.NewStringFlag( |
| 208 | &cli.StringFlag{ |
| 209 | Name: "key", |
| 210 | Value: "/tmp/foo", |
| 211 | }, |
| 212 | ), |
| 213 | ``` |
| 214 | |
| 215 | === "v3" |
| 216 | |
| 217 | Requires to use at least `github.com/urfave/cli-altsrc/[email protected]` |
| 218 | |
| 219 | ```go |
| 220 | cli.StringFlag{ |
| 221 | Sources: cli.NewValueSourceChain(altsrcjson.JSON("key", altsrc.StringSourcer("/path/to/foo.json"))), |
| 222 | } |
| 223 | ``` |
| 224 | |
| 225 | ## Build Tags |
| 226 | |
| 227 | v2 provided build tags to reduce binary size by stripping optional features: |
| 228 | |
| 229 | | Tag | Feature | |
| 230 | |-----|---------| |
| 231 | | `urfave_cli_no_docs` | Disabled markdown/man page generation | |
| 232 | | `urfave_cli_no_suggest` | Disabled "Did you mean?" suggestions | |
| 233 | |
| 234 | These tags are **not available in v3** because they are no longer necessary: |
| 235 | |
| 236 | - **Doc generation** (`ToMarkdown`, `ToMan`) has been removed from the core library and is available as a separate module, `github.com/urfave/cli-docs/v3`. |
| 237 | - **Suggestions** now use an inlined Jaro-Winkler algorithm with no external dependency (`xrash/smetrics`). |
| 238 | - **Shell completion** uses `embed.FS` for its scripts, with no external dependencies. |
| 239 | - **Altsrc** has been extracted to a separate module, `github.com/urfave/cli-altsrc/v3` (see below). |
| 240 | |
| 241 | Binary size in v3 is managed through architectural simplification rather than build tags. v3's minimal dependency tree (only `github.com/stretchr/testify` in tests) means the core library contributes very little to your final binary. |
| 242 | |
| 243 | ### Order of precedence of envvars, filepaths, altsrc now depends on the order in which they are defined |
| 244 | |
| 245 | === "v2" |
| 246 | |
| 247 | ```go |
| 248 | altsrc.NewStringFlag( |
| 249 | &cli.StringFlag{ |
| 250 | Name: "key", |
| 251 | EnvVars: []string{"APP_LANG"}, |
| 252 | FilePath: "/path/to/foo", |
| 253 | }, |
| 254 | ), |
| 255 | ``` |
| 256 | |
| 257 | === "v3" |
| 258 | |
| 259 | Requires to use at least `github.com/urfave/cli-altsrc/[email protected]` |
| 260 | |
| 261 | ```go |
| 262 | import altsrcjson "github.com/urfave/cli-altsrc/v3/json" |
| 263 | |
| 264 | // ... |
| 265 | |
| 266 | &cli.StringFlag{ |
| 267 | Name: "key", |
| 268 | Sources: cli.NewValueSourceChain( |
| 269 | cli.EnvVar("APP_LANG"), |
| 270 | cli.File("/path/to/foo"), |
| 271 | altsrcjson.JSON("key", altsrc.StringSourcer("/path/to/foo.json")), |
| 272 | ), |
| 273 | }, |
| 274 | ``` |
| 275 | |
| 276 | In the above case the Envs are checked first and if not found then files are looked at and then finally the `altsrc` |
| 277 | |
| 278 | ## cli.Context has been removed |
| 279 | |
| 280 | All functions handled previously by `cli.Context` have been incorporated into `cli.Command`: |
| 281 | |
| 282 | | v2 | v3 | |
| 283 | |------------------------------|------------------------------| |
| 284 | | `cli.Context.IsSet` | `cli.Command.IsSet` | |
| 285 | | `cli.Context.NumFlags` | `cli.Command.NumFlags` | |
| 286 | | `cli.Context.FlagNames` | `cli.Command.FlagNames` | |
| 287 | | `cli.Context.LocalFlagNames` | `cli.Command.LocalFlagNames` | |
| 288 | | `cli.Context.Lineage` | `cli.Command.Lineage` | |
| 289 | | `cli.Context.Count` | `cli.Command.Count` | |
| 290 | | `cli.Context.Value` | `cli.Command.Value` | |
| 291 | | `cli.Context.Args` | `cli.Command.Args` | |
| 292 | | `cli.Context.NArg` | `cli.Command.NArg` | |
| 293 | |
| 294 | ## Handler Function Signatures Changes |
| 295 | |
| 296 | All handler functions now take at least 2 arguments a `context.Context` and a pointer to `Cli.Command` |
| 297 | in addition to other specific args. This allows handler functions to utilize `context.Context` for |
| 298 | blocking/time-specific operations and so on. |
| 299 | |
| 300 | ### BeforeFunc |
| 301 | |
| 302 | === "v2" |
| 303 | |
| 304 | `type BeforeFunc func(*Context) error` |
| 305 | |
| 306 | === "v3" |
| 307 | |
| 308 | `type BeforeFunc func(context.Context, *cli.Command) (context.Context, error)` |
| 309 | |
| 310 | ### AfterFunc |
| 311 | |
| 312 | === "v2" |
| 313 | |
| 314 | `type AfterFunc func(*Context) error` |
| 315 | |
| 316 | === "v3" |
| 317 | |
| 318 | `type AfterFunc func(context.Context, *cli.Command) error` |
| 319 | |
| 320 | ### ActionFunc |
| 321 | |
| 322 | === "v2" |
| 323 | |
| 324 | `type ActionFunc func(*Context) error` |
| 325 | |
| 326 | === "v3" |
| 327 | |
| 328 | `type ActionFunc func(context.Context, *cli.Command) error` |
| 329 | |
| 330 | ### CommandNotFoundFunc |
| 331 | |
| 332 | === "v2" |
| 333 | |
| 334 | `type CommandNotFoundFunc func(*Context, string) error` |
| 335 | |
| 336 | === "v3" |
| 337 | |
| 338 | `type CommandNotFoundFunc func(context.Context, *cli.Command, string) error` |
| 339 | |
| 340 | ### OnUsageErrorFunc |
| 341 | |
| 342 | === "v2" |
| 343 | |
| 344 | `type OnUsageErrorFunc func(*Context, err error, isSubcommand bool) error` |
| 345 | |
| 346 | === "v3" |
| 347 | |
| 348 | `type OnUsageErrorFunc func(context.Context, *cli.Command, err error, isSubcommand bool) error` |
| 349 | |
| 350 | ### InvalidAccessFunc |
| 351 | |
| 352 | === "v2" |
| 353 | |
| 354 | `type InvalidAccessFunc func(*Context, string) error` |
| 355 | |
| 356 | === "v3" |
| 357 | |
| 358 | `type InvalidAccessFunc func(context.Context, *cli.Command, string) error` |
| 359 | |
| 360 | ### ExitErrHandlerFunc |
| 361 | |
| 362 | === "v2" |
| 363 | |
| 364 | `type ExitErrHandlerFunc func(*Context, err error) error` |
| 365 | |
| 366 | === "v3" |
| 367 | |
| 368 | `type ExitErrHandlerFunc func(context.Context, *cli.Command, err error) error` |
| 369 | |
| 370 | Compiler messages you might see(for ActionFunc): |
| 371 | |
| 372 | ``` |
| 373 | cannot use func literal (type func(*cli.Context) error) as type cli.ActionFunc in field value |
| 374 | ``` |
| 375 | |
| 376 | Similar messages would be shown for other funcs. |
| 377 | |
| 378 | ## TimestampFlag |
| 379 | |
| 380 | === "v2" |
| 381 | |
| 382 | ```go |
| 383 | &cli.TimestampFlag{ |
| 384 | Name: "foo", |
| 385 | Layout: time.RFC3339, |
| 386 | } |
| 387 | ``` |
| 388 | |
| 389 | === "v3" |
| 390 | |
| 391 | ```go |
| 392 | &cli.TimestampFlag{ |
| 393 | Name: "foo", |
| 394 | Config: cli.TimestampConfig{ |
| 395 | Layouts: []string{time.RFC3339}, |
| 396 | }, |
| 397 | } |
| 398 | ``` |
| 399 | |
| 400 | ## PathFlag |
| 401 | |
| 402 | === "v2" |
| 403 | |
| 404 | ```go |
| 405 | &cli.PathFlag{ |
| 406 | Name: "foo", |
| 407 | } |
| 408 | ``` |
| 409 | |
| 410 | === "v3" |
| 411 | |
| 412 | ```go |
| 413 | &cli.StringFlag{ |
| 414 | Name: "foo", |
| 415 | TakesFile: true, |
| 416 | } |
| 417 | ``` |
| 418 | |
| 419 | ## Authors |
| 420 | |
| 421 | === "v2" |
| 422 | |
| 423 | ```go |
| 424 | &cli.App{ |
| 425 | Authors: []*cli.Author{ |
| 426 | {Name: "Some Guy", Email: "[email protected]"}, |
| 427 | }, |
| 428 | } |
| 429 | ``` |
| 430 | |
| 431 | === "v3" |
| 432 | |
| 433 | ```go |
| 434 | // import "net/mail" |
| 435 | &cli.Command{ |
| 436 | Authors: []any{ |
| 437 | mail.Address{Name: "Some Guy", Address: "[email protected]"}, |
| 438 | }, |
| 439 | } |
| 440 | ``` |