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

CountVar doesn't support default value setting #384

Open
ZhouXing19 opened this issue Sep 18, 2023 · 1 comment
Open

CountVar doesn't support default value setting #384

ZhouXing19 opened this issue Sep 18, 2023 · 1 comment

Comments

@ZhouXing19
Copy link

ZhouXing19 commented Sep 18, 2023

When setting a count flag, the default value setting is not supported -- FlagSet.CountVarP() or FlagSet.CountVar() only takes name and usage as parameters. Weirdly, it is noted in the comment that default value is allowed.

pflag/count.go

Lines 47 to 58 in d5e0c06

// CountVar defines a count flag with specified name, default value, and usage string.
// The argument p points to an int variable in which to store the value of the flag.
// A count flag will add 1 to its value every time it is found on the command line
func (f *FlagSet) CountVar(p *int, name string, usage string) {
f.CountVarP(p, name, "", usage)
}
// CountVarP is like CountVar only take a shorthand for the flag name.
func (f *FlagSet) CountVarP(p *int, name, shorthand string, usage string) {
flag := f.VarPF(newCountValue(0, p), name, shorthand, usage)
flag.NoOptDefVal = "+1"
}

@fredbi
Copy link
Collaborator

fredbi commented Oct 3, 2023

IMHO, the only way to preserve a backard compatibility would be to extend these methods with some ...Option parameter. One such option could support adding a default value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants