Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

bool flags accept a value but do not mention taking arguments in --help output #1657

Closed
alimpfard opened this issue Apr 8, 2022 · 7 comments
Labels
kind/support Questions, supporting users, etc.

Comments

@alimpfard
Copy link

Given a bool flag like

command.PersistentFlags().BoolVarP(&opts.Frobnicate, "frobnicate", "", true, "Frobnicate the output")

--help lists:

--frobnicate                           Frobnicate the output (default true)

but the parser accepts --frobnicate false, expected behaviour is to either disallow --frobnicate false or mention the optional argument in --help like

--frobnicate [bool]                           Frobnicate the output (default true)
@jpmcb
Copy link
Collaborator

jpmcb commented Apr 8, 2022

If you're looking for a single "switch" flag, you'll want something like this:

var bark bool

...

// use false, the "empty" value for a bool, as the default
rootCmd.PersistentFlags().BoolVarP(&bark, "woof", "", false, "prints out bark")

...

// In your command logic, use that variable
if bark {
    println("woof!")
}

Running this program looks like:

$ ./testing-app --help

...

Flags:
  -h, --help            help for testing-app
      --woof            prints out bark
$ ./testing-app
$ ./testing-app --woof
woof!
$

@jpmcb jpmcb closed this as completed Apr 8, 2022
@jpmcb jpmcb added the kind/support Questions, supporting users, etc. label Apr 8, 2022
@alimpfard
Copy link
Author

I am not looking for a switch flag, rather that (in your example), ./testing-app --woof=false is considered valid, yet the --help output does not mention it.

@jpmcb
Copy link
Collaborator

jpmcb commented Apr 8, 2022

A bool flag is a switch flag.

--woof=false, --woof=true, and --woof are all valid inputs. This is intended behavior so that a user may provide the flag by itself or with a input boolean.

Are you expecting that the default template for bool flags should mention that it's expecting a bool? Or am I missing something here?

@alimpfard
Copy link
Author

alimpfard commented Apr 8, 2022

I expect the --help output to tell me that --woof accepts an optional boolean argument (as it does).
To speak in...say, getopt terms, that's the difference between option.has_arg == true and option.has_arg == false; for instance:

$ man --ascii=true
man: option '--ascii' doesn't allow an argument

ofc, I'm not asking for removal of --bool-flag=false, I'm just asking the --help output to list its optional argument.

@jpmcb
Copy link
Collaborator

jpmcb commented Apr 9, 2022

I'm just asking the --help output to list its optional argument.

I think that's fair! Re-opening and we can get some community feedback

@jpmcb jpmcb reopened this Apr 9, 2022
@github-actions
Copy link

github-actions bot commented Jun 9, 2022

The Cobra project currently lacks enough contributors to adequately respond to all issues. This bot triages issues and PRs according to the following rules:

  • After 60d of inactivity, lifecycle/stale is applied. - After 30d of inactivity since lifecycle/stale was applied, lifecycle/rotten is applied and the issue is closed.
    You can:
  • Make a comment to remove the stale label and show your support. The 60 days reset. - If an issue has lifecycle/rotten and is closed, comment and ask maintainers if they'd be interseted in reopening

@mid-kid
Copy link

mid-kid commented Jul 18, 2024

bump. This issue bit me today, as I expected it to function as --bool-flag false instead of --bool-flag=false, given that all other flag arguments are passed without the =, and I think not mentioning something along the lines of --bool-flag[=VALUE] in the help output isn't a very good user experience, especially since some programs prefer --[no-]bool-flag or other schemes for controlling flags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/support Questions, supporting users, etc.
Projects
None yet
Development

No branches or pull requests

3 participants