-
Notifications
You must be signed in to change notification settings - Fork 1.7k
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
Naked double dash separator (--) strange behavior #1114
Comments
Hi @krostar , I would like slove this issue. I did analysis the bug and found the bug in flag.go file |
Hi @atif-konasl, thank you for your reactivity, but I can't assign you to an issue. Feel free to submit a PR if you know how to solve this and reference it to this issue. Maintainers will probably be happy to see the bug already have a fix when they give a look to this issue, and your PR. |
Hi @krostar, there is a bug in parseOne() method in flag package of go. func (f *FlagSet) parseOne() (bool, error) {
if len(f.args) == 0 {
return false, nil
}
s := f.args[0]
if len(s) < 2 || s[0] != '-' {
return false, nil
}
numMinuses := 1
if s[1] == '-' {
numMinuses++
if len(s) == 2 { // "--" terminates the flags
f.args = f.args[1:]
return false, nil
}
}
name := s[numMinuses:]
if len(name) == 0 || name[0] == '-' || name[0] == '=' {
return false, f.failf("bad flag syntax: %s", s)
}
// it's a flag. does it have an argument?
f.args = f.args[1:]
hasValue := false
value := ""
for i := 1; i < len(name); i++ { // equals cannot be first
if name[i] == '=' {
value = name[i+1:]
hasValue = true
name = name[0:i]
break
}
}
m := f.formal
flag, alreadythere := m[name] // BUG
if !alreadythere {
if name == "help" || name == "h" { // special case for nice help message.
f.usage()
return false, ErrHelp
}
return false, f.failf("flag provided but not defined: -%s", name)
}
if fv, ok := flag.Value.(boolFlag); ok && fv.IsBoolFlag() { // special case: doesn't need an arg
if hasValue {
if err := fv.Set(value); err != nil {
return false, f.failf("invalid boolean value %q for -%s: %v", value, name, err)
}
} else {
if err := fv.Set("true"); err != nil {
return false, f.failf("invalid boolean flag %s: %v", name, err)
}
}
} else {
// It must have a value, which might be the next argument.
if !hasValue && len(f.args) > 0 {
// value is the next arg
hasValue = true
value, f.args = f.args[0], f.args[1:]
}
if !hasValue {
return false, f.failf("flag needs an argument: -%s", name)
}
if err := flag.Value.Set(value); err != nil {
return false, f.failf("invalid value %q for flag -%s: %v", value, name, err)
}
}
if f.actual == nil {
f.actual = make(map[string]*Flag)
}
f.actual[name] = flag
return true, nil
} ` numMinuses := 1
if s[1] == '-' {
numMinuses++
if len(s) == 2 { // "--" terminates the flags
f.args = f.args[1:]
return false, nil
}
} |
Hi @atif-konasl, @krostar isn't a maintainer for this library and therefore cannot assign you to anything 🙂 I'm assigning this issue to you now, feel free to stand up a PR whenever you have time. |
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. |
Reopening given stalebot's involvement; will be re-verifying 🔜 |
Verified this behaviour using a unit test. Yes the bug is in the golang flag library. Until we replace that with our own parsing dont think its an easy fix. |
@krostar so the "easiest" way to fix is to allow SkipFlagParsing at the level at which the args shouldnt be parsed and this will allow you to capture all the args |
my urfave/cli version is
v2.2.0
Checklist
Dependency Management
Describe the bug
--
is sometimes present in the output ofContext.Args.Slice()
and sometimes it is not present, which makes it impossible to properly differentiate arguments before and after the terminator.To reproduce
which produces
Observed behavior
When argument parsing terminator
--
is placed before any other arguments, it is missing from the arguments list. When there are positional arguments before it, it is present in the arguments list.Expected behavior
I would have expected to find the terminator in the second scenario as such:
Run
go version
and paste its output hereRun
go env
and paste its output hereThe text was updated successfully, but these errors were encountered: