cli icon indicating copy to clipboard operation
cli copied to clipboard

Display global flags in subcommand

Open xeals opened this issue 6 years ago • 14 comments

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?

xeals avatar Apr 11 '18 05:04 xeals

GlobalXXX?

nasu avatar May 03 '18 11:05 nasu

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.

genieplus avatar Jun 10 '18 02:06 genieplus

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

coilysiren avatar Aug 17 '19 09:08 coilysiren

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!

coilysiren avatar Sep 10 '19 06:09 coilysiren

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.

stale[bot] avatar Feb 25 '20 08:02 stale[bot]

I'm not sure if this is still relevant as of v2?

coilysiren avatar Feb 26 '20 05:02 coilysiren

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.

stale[bot] avatar Feb 26 '20 05:02 stale[bot]

This issue is still present in v2.

bandesz avatar Mar 10 '20 13:03 bandesz

Thanks for the confirmation! 👍

coilysiren avatar Mar 10 '20 18:03 coilysiren

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 {
  //...
}

bandesz avatar Mar 10 '20 22:03 bandesz

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.

stale[bot] avatar Jun 08 '20 22:06 stale[bot]

Closing this as it has become stale.

stale[bot] avatar Jul 08 '20 22:07 stale[bot]

I had a similar requirement. Is there any progress on this? I can get a PR if this is still open.

Madhur1997 avatar Dec 13 '20 16:12 Madhur1997

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

factoidforrest avatar Dec 16 '21 20:12 factoidforrest