cli
cli copied to clipboard
Display global flags in subcommand
Is it possible to display higher-level flags in a subcommand?
e.g., given:
app := &cli.App{
Flags: []cli.Flag{
cli.BoolFlag{
Name: "verbose, v",
Usage: "be LOUD",
},
},
Commands: []cli.Command{
cli.Command{
Name: "status",
},
},
}
Is it possible for verbose
to be displayed in status
's help command without redefining the flag?
GlobalXXX
?
I run into the same issue. I was expecting to see the option --verbose
when the help of the subcommand status
is printed. Something like:
$ go run main.go status -h
NAME:
main status -
USAGE:
main [global options] status [command options] [arguments...]
OPTIONS:
--help, -h show help (default: false)
GLOBAL OPTIONS:
--verbose be LOUD (default: false)
Just to be clear, it is not a question of defining a global flag as suggested in the previous comment. It is about the printed help.
Mostly for bookkeeping, I want to mention that this is conceptually related to my desire to expand global flags support for v3 https://github.com/urfave/cli/issues/833
This feature is now in review
, @urfave/cli please add a 👍 or 👎 to the top post if you're in favor or against this feature being added!
This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.
I'm not sure if this is still relevant as of v2?
This issue or PR has been bumped and is no longer marked as stale! Feel free to bump it again in the future, if it's still relevant.
This issue is still present in v2.
Thanks for the confirmation! 👍
I did a simple hack to always show the app global flags by overriding the cli.HelpPrinterCustom
function:
globalOptionsTemplate := `{{if .VisibleFlags}}GLOBAL OPTIONS:
{{range $index, $option := .VisibleFlags}}{{if $index}}
{{end}}{{$option}}{{end}}
{{end}}
`
app := &cli.App {...}
origHelpPrinterCustom := cli.HelpPrinterCustom
defer func() {
cli.HelpPrinterCustom = origHelpPrinterCustom
}()
cli.HelpPrinterCustom = func(out io.Writer, templ string, data interface{}, customFuncs map[string]interface{}) {
origHelpPrinterCustom(out, templ, data, customFuncs)
if data != app {
origHelpPrinterCustom(app.Writer, globalOptionsTemplate, app, nil)
}
}
if err := app.Run(os.Args); err != nil {
//...
}
This issue or PR has been automatically marked as stale because it has not had recent activity. Please add a comment bumping this if you're still interested in it's resolution! Thanks for your help, please let us know if you need anything else.
Closing this as it has become stale.
I had a similar requirement. Is there any progress on this? I can get a PR if this is still open.
It would be better if the global flags worked after a subcommand, optionally. In our tool it makes much more sense to do
tool subcommand --flag
than
tool --flag subcommand