cli: Global flag not accessible with many levels of sub-commands
Hello community,
My aim is to have some global flags that can be inserted anywhere. The code below registers the admin command which has a sub-command service which itself has another sub-command called status. Debug flag is global. However, when I type the following command, debug flag is not activated
$ ./binary admin -d service status
status: => local (false), global (false)
The code:
package main
import (
"fmt"
"os"
"strconv"
"github.com/urfave/cli"
)
func main() {
globalFlags := []cli.Flag{
cli.BoolFlag{Name: "debug, d", Usage: "Run in debug mode"},
}
adminServiceStatusCmd := cli.Command{
Name: "status",
Flags: append([]cli.Flag{}, globalFlags...),
Action: func(c *cli.Context) {
global := strconv.FormatBool(c.GlobalBool("debug"))
local := strconv.FormatBool(c.Bool("debug"))
fmt.Printf("%s: => local (%s), global (%s)\n", c.Command.Name, local, global)
},
}
adminServiceCmd := cli.Command{
Name: "service",
Flags: append([]cli.Flag{}, globalFlags...),
Subcommands: []cli.Command{adminServiceStatusCmd},
}
adminCmd := cli.Command{
Name: "admin",
Flags: append([]cli.Flag{}, globalFlags...),
Subcommands: []cli.Command{adminServiceCmd},
}
app := cli.NewApp()
app.Name = "lookup"
app.Flags = append([]cli.Flag{}, globalFlags...)
app.Commands = []cli.Command{adminCmd}
app.Run(os.Args)
}
Is this a bug ? or am I just misunderstanding the global flag concept ?
Thanks,
About this issue
- Original URL
- State: closed
- Created 7 years ago
- Reactions: 10
- Comments: 15 (7 by maintainers)
Closing this as it has become stale.
The example above is probably still true! I would be very much in favor of someone creating a PR to make this better 🙏
This behaviour is not present in v2
Since there are workaround for v1 I am closing this issue
@AndreasBackx I know I’m necrobumping this and I hope you’ve found some workaround, but for other devs who are seeing this and thinking of XKCD 979, here’s something I’ve cobbled together which actually works for finding flag values from variously nested levels of
cli.Contexts.