-
Notifications
You must be signed in to change notification settings - Fork 2.9k
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
If NoOptDefVal is set, arguments of the form "--myflag myvalue" does not work #866
Comments
This issue is being marked as stale due to a long period of inactivity |
Can we reopen this bug? Looking at the docs I think this feature should work like this... If we define a string flag called foo on a command (cmd) and set it's default value to the empty string and we set NoOptDefVal to "NOARGS". cmd.PersistentFlags().StringVarP(&fooVal, "foo", "f", "", "value for foo")
cmd.Flag("foo").NoOptDefVal = "NOARGS" I expect the code should handle the following cases like this:
Using Cobra version 1.0.0 case 3 does not work, foo is set to "NOARGS". Looking at the code if NoOptDefVal is defined it is used regardless of an option, bar, being specified. Maybe this feature is only meant to work when specifying flag options using the equals syntax? Looking at pflag.flag.go line 984 the if cases seem out of order. Current code: var value string
if len(split) == 2 {
// '--flag=arg'
value = split[1]
} else if flag.NoOptDefVal != "" {
// '--flag' (arg was optional)
value = flag.NoOptDefVal
} else if len(a) > 0 {
// '--flag arg'
value = a[0]
a = a[1:]
} else {
// '--flag' (arg was required)
err = f.failf("flag needs an argument: %s", s)
return
} If NoOptDefVal is set the code never looks to see if a option has been specified since it is the following case in the if/then/else. Rewriting this code to something like the code below fixes the problem but is probably inadequate and/or incomplete: var value string
if len(split) == 2 {
// '--flag=arg'
value = split[1]
} else if len(a) > 0 {
if flag.NoOptDefVal != "" && strings.HasPrefix(a[0], "-") {
// '--flag'
value = flag.NoOptDefVal
} else {
// '--flag arg'
value = a[0]
}
a = a[1:]
} else {
// '--flag' (arg was required)
err = f.failf("flag needs an argument: %s", s)
return
} Any chance on getting someone to look at this bug and maybe get a more robust solution? Thank you. |
Repro'd with current codebase. |
Reproduced with |
Any updates on this issue? |
Piling on - any ETA for a fix? EG |
I believe this is correct behavior. When setting the The So, when |
i agree there are constraints on when a generic flag processor could support this. Our specific scenario is to replicate the behavior of the old So if
|
I don’t believe it’s possible for cobra to know this at the moment. Supporting such a feature would probably mean having to define an optional behaviour that would be significantly different than how Cobra does things now. I am under the impression that this is not a significantly common case that would justify such complexity. |
Yes, you can specify that a command doesn't have any positional args. Line 42 in 0e3a0bf
|
I wish we could use that but: #1969 (comment) |
I support the current implementation of Looking at the code I see:
This could be changed to something like
|
If flag.NoOptDefVal is not set, the following two variations work
But if flag.NoOptDefVal is set, (1) does not work. Only (2) works.
From what I see, the relevant code in command.go seems to be here:
cobra/command.go
Lines 487 to 499 in 67fc483
As you can see, if NoOptDefVal is set, the code is stripping off the argument to the flag. I did not understand why it is doing that. Doesn't NoOptDefVal mean that a default value is to be used when (and only when) no value is specified with the flag? Can someone please clarify this?
The text was updated successfully, but these errors were encountered: