Add a v1 => v2 migration guide

main
Tom Limoncelli 4 years ago
parent dd51d24ce6
commit 710df05119

@ -1424,6 +1424,166 @@ In this example the flag could be used like this :
Side note: quotes may be necessary around the date depending on your layout (if you have spaces for instance)
### Converting from v1 to v2
v2 has a number of breaking changes but converting is straightforward:
make the changes documented below then resolve any compiler errors.
Typically this is sufficient.
If you find any issues not covered by this document, please post a
comment on [Issue 921](https://github.com/urfave/cli/issues/921) or
consider sending a PR.
#### Flags before args
In v2 flags must come before args. This is more POSIX-compliant. You
may need to update scripts, user documentation, etc.
This will work:
```
cli hello --shout rick
```
This will not:
```
cli hello rick --shout
```
#### Import string changed
* OLD: `import "github.com/urfave/cli"`
* NEW: `import "github.com/urfave/cli/v2"`
Check each file for this and make the change.
Shell command to find them all: `fgrep -rl github.com/urfave/cli *`
#### Flag aliases are done differently.
Change `Name: "foo, f` to "Name: "foo", Aliases: []string{"f"}`
OLD:
```
cli.StringFlag{
Name: "config, cfg"
}
```
NEW:
```
cli.StringFlag{
Name: "config",
Aliases: []string{"cfg"},
}
```
Sadly v2 doesn't warn you if a comma is in the name.
#### Commands are now lists of pointers
Occurrences of `[]Command` have been changed to `[]*Command`.
What this means to you:
Look for `[]cli.Command{}` and change it to `[]*cli.Command{}`
Example:
* OLD: `var commands = []cli.Command{}`
* NEW: `var commands = []*cli.Command{}`
Compiler messages you might see:
`
commands/commands.go:56:30: cannot convert commands (type []cli.Command) to type cli.CommandsByName
commands/commands.go:57:15: cannot use commands (type []cli.Command) as type []*cli.Command in assignment
`
#### Lists of commands should be pointers
If you are building up a list of commands, the individual items should
now be pointers.
* OLD: `cli.Command{`
* NEW: `&cli.Command{`
Compiler messages you might see:
`
./commands.go:32:34: cannot use cli.Command literal (type cli.Command) as type *cli.Command in argument to
`
#### cli.Flag changed
`cli.Flag` is now a list of pointers.
What this means to you:
If you make a list of flags, add a `&` in front of each
item. cli.BoolFlag, cli.StringFlag, etc.
* OLD:
`
app.Flags = []cli.Flag{
cli.BoolFlag{
`
* NEW:
```
app.Flags = []cli.Flag{
&cli.BoolFlag{
```
Compiler messages you might see:
```
cli.StringFlag does not implement cli.Flag (Apply method has pointer receiver)
```
#### Appending Commands
Appending to a list of commands needs to be changed since the list is
now pointers.
* OLD: `commands = append(commands, *c)`
* NEW: `commands = append(commands, c)`
Compiler messages you might see:
`
commands/commands.go:28:19: cannot use c (type *cli.Command) as type cli.Command in append
`
#### Actions returns errors
A command's `Action:` now returns an `error`.
* OLD: `Action: func(c *cli.Context) {`
* NEW: `Action: func(c *cli.Context) error {`
Compiler messages you might see:
```
./commands.go:35:2: cannot use func literal (type func(*cli.Context)) as type cli.ActionFunc in field value
```
#### Everything else
Compile the code and worth through any errors it finds. Most should
relate to issues listed above.
Once it compiles, test the command. Make sure `-h` displays help
properly, then try various flags and subcommands.
If you find anything not covered by this document please let us know
by submitting a comment on
[Issue 921](https://github.com/urfave/cli/issues/921)
so that others can benefit.
### Full API Example
**Notice**: This is a contrived (functioning) example meant strictly for API

Loading…
Cancel
Save