v3 has a number of breaking changes but converting is relatively straightforward: make the changes documented below then resolve any compiler errors. We hope this will be sufficient for most typical users.
If you find any issues not covered by this document, please post a comment on the discussion or consider sending a PR to help improve this guide.
=== "v2"
`import "github.com/urfave/cli/v2"`
=== "v3"
`import "github.com/urfave/cli/v3"`
Check each file for this and make the change.
Shell command to find them all: fgrep -rl github.com/urfave/cli/v2 *
=== "v2"
```go
cli.StringFlag{
FilePath: "/path/to/foo",
}
```
=== "v3"
```go
cli.StringFlag{
Sources: cli.Files("/path/to/foo"),
}
```
or
```go
cli.StringFlag{
Sources: cli.NewValueSourceChain(
cli.File("/path/to/foo"),
),
}
```
=== "v2"
```go
cli.StringFlag{
EnvVars: []string{"APP_LANG"},
}
```
=== "v3"
```go
cli.StringFlag{
Sources: cli.EnvVars("APP_LANG"),
}
```
or
```go
cli.StringFlag{
Sources: cli.NewValueSourceChain(
cli.EnvVar("APP_LANG"),
),
}
```
=== "v2"
`import "github.com/urfave/cli/v2/altsrc"`
=== "v3"
`import altsrc "github.com/urfave/cli-altsrc/v3"`
=== "v2"
```go
altsrc.NewStringFlag(
&cli.StringFlag{
Name: "key",
Value: "/tmp/foo",
},
),
```
=== "v3"
Requires to use at least `github.com/urfave/cli-altsrc/[email protected]`
```go
cli.StringFlag{
Sources: cli.NewValueSourceChain(altsrcjson.JSON("key", altsrc.StringSourcer("/path/to/foo.json"))),
}
```
Order of precedence of envvars, filepaths, altsrc now depends on the order in which they are defined
=== "v2"
```go
altsrc.NewStringFlag(
&cli.StringFlag{
Name: "key",
EnvVars: []string{"APP_LANG"},
FilePath: "/path/to/foo",
},
),
```
=== "v3"
Requires to use at least `github.com/urfave/cli-altsrc/[email protected]`
```go
import altsrcjson "github.com/urfave/cli-altsrc/v3/json"
// ...
&cli.StringFlag{
Name: "key",
Sources: cli.NewValueSourceChain(
cli.EnvVar("APP_LANG"),
cli.File("/path/to/foo"),
altsrcjson.JSON("key", altsrc.StringSourcer("/path/to/foo.json")),
),
},
```
In the above case the Envs are checked first and if not found then files are looked at and then finally the altsrc
All functions handled previously by cli.Context
have been incorporated into cli.Command
:
v2 | v3 |
---|---|
cli.Context.IsSet |
cli.Command.IsSet |
cli.Context.NumFlags |
cli.Command.NumFlags |
cli.Context.FlagNames |
cli.Command.FlagNames |
cli.Context.LocalFlagNames |
cli.Command.LocalFlagNames |
cli.Context.Lineage |
cli.Command.Lineage |
cli.Context.Count |
cli.Command.Count |
cli.Context.Value |
cli.Command.Value |
cli.Context.Args |
cli.Command.Args |
cli.Context.NArg |
cli.Command.NArg |
All handler functions now take at least 2 arguments a context.Context
and a pointer to Cli.Command
in addition to other specific args. This allows handler functions to utilize context.Context
for
blocking/time-specific operations and so on.
=== "v2"
`type BeforeFunc func(*Context) error`
=== "v3"
`type BeforeFunc func(context.Context, *cli.Command) (context.Context, error)`
=== "v2"
`type AfterFunc func(*Context) error`
=== "v3"
`type AfterFunc func(context.Context, *cli.Command) error`
=== "v2"
`type ActionFunc func(*Context) error`
=== "v3"
`type ActionFunc func(context.Context, *cli.Command) error`
=== "v2"
`type CommandNotFoundFunc func(*Context, string) error`
=== "v3"
`type CommandNotFoundFunc func(context.Context, *cli.Command, string) error`
=== "v2"
`type OnUsageErrorFunc func(*Context, err error, isSubcommand bool) error`
=== "v3"
`type OnUsageErrorFunc func(context.Context, *cli.Command, err error, isSubcommand bool) error`
=== "v2"
`type InvalidAccessFunc func(*Context, string) error`
=== "v3"
`type InvalidAccessFunc func(context.Context, *cli.Command, string) error`
=== "v2"
`type ExitErrHandlerFunc func(*Context, err error) error`
=== "v3"
`type ExitErrHandlerFunc func(context.Context, *cli.Command, err error) error`
Compiler messages you might see(for ActionFunc):
cannot use func literal (type func(*cli.Context) error) as type cli.ActionFunc in field value
Similar messages would be shown for other funcs.
=== "v2"
```go
&cli.TimestampFlag{
Name: "foo",
Layout: time.RFC3339,
}
```
=== "v3"
```go
&cli.TimestampFlag{
Name: "foo",
Config: cli.TimestampConfig{
Layouts: []string{time.RFC3339},
},
}
```
=== "v2"
```go
&cli.App{
Authors: []*cli.Author{
{Name: "Some Guy", Email: "[email protected]"},
},
}
```
=== "v3"
```go
// import "net/mail"
&cli.Command{
Authors: []any{
mail.Address{Name: "Some Guy", Address: "[email protected]"},
},
}
```
=== "v2"
```go
&cli.App{
EnableBashCompletion: true,
}
```
=== "v3"
```go
&cli.Command{
EnableShellCompletion: true,
}
```