Bug 17327 – std.getopt: repeated options unrecognised

Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P1
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-04-15T03:37:02Z
Last change time
2018-01-05T13:28:19Z
Assigned to
No Owner
Creator
novalazy+dlang

Comments

Comment #0 by novalazy+dlang — 2017-04-15T03:37:02Z
getopt reports that a valid option is unrecognised if it is used multiple times. e.g. getopt(args, "dry-run|n", &dryrun); An exception is throw for both "prog -n -n" and "prog --dry-run -n".
Comment #1 by jrdemail2000-dlang — 2017-04-15T06:09:52Z
I tried to replicate this with the program below. It failed in the described way for the boolean option, but not for the others. If the original author found other cases it would be worth listing them. ==== getopt_repeated_args.d ==== void main(string[] args) { import std.getopt; import std.stdio; int counter; bool flag; int num; void dryrun0() { writeln("dryrun0"); } void dryrun1(string opt) { writefln("dryrun1(%s)", opt); } void dryrun2(string opt, string val) { writefln("dryrun2(%s, %s)", opt, val); } auto r = getopt( args, "c|counter+", "Counter", &counter, "f|flag", "flag", &flag, "n|num", "number", &num, "x|dryrun0", "zero args", &dryrun0, "y|dryrun1", "option is arg", &dryrun1, "z|dryrun2", "option and val are args", &dryrun2, ); if (r.helpWanted) defaultGetoptPrinter("Options:", r.options); else writefln("counter: %d; flag: %s; num: %d", counter, flag, num); return; } ================ ## All work with repeated invocation except the boolean case. $ ./getopt_repeated_args -c -n 5 -c -n 7 counter: 2; flag: false; num: 7 $ ./getopt_repeated_args --dryrun0 --dryrun0 dryrun0 dryrun0 counter: 0; flag: false; num: 0 $ ./getopt_repeated_args -x -x -y -y -z A -z B dryrun0 dryrun0 dryrun1(y|dryrun1) dryrun1(y|dryrun1) dryrun2(z|dryrun2, A) dryrun2(z|dryrun2, B) counter: 0; flag: false; num: 0 ## Single invocation of the boolean option works $ ./getopt_repeated_args -f counter: 0; flag: true; num: 0 $ ./getopt_repeated_args --flag=true counter: 0; flag: true; num: 0 ## Repeated invocation of the boolean option fails $ ./getopt_repeated_args -f -f std.getopt.GetOptException@/Library/D/dmd/src/phobos/std/getopt.d(790): Unrecognized option -f $ ./getopt_repeated_args --flag=true --flag=false std.getopt.GetOptException@/Library/D/dmd/src/phobos/std/getopt.d(790): Unrecognized option --flag=false
Comment #2 by novalazy+dlang — 2017-04-15T07:57:09Z
Oops, sorry for the incomplete bug report. Indeed I hit the problem with a boolean option and forgot to check other types.
Comment #3 by jrdemail2000-dlang — 2017-04-16T07:33:27Z
Comment #4 by github-bugzilla — 2017-04-16T17:14:34Z
Commits pushed to master at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/c1d49fc4948977239fa4ed8c15b3d318b3e60ca9 Fix issue 17327 - std.getopt: Repeated boolean command option fails. https://github.com/dlang/phobos/commit/684f41b64ee96a35bd9fba3aca95e5cdbf99d09c Fix issue 17327. Review comments: drop continue statement. https://github.com/dlang/phobos/commit/5aad85503a8bfb96dbf357af2758f01dc64cba60 Merge pull request #5334 from jondegenhardt/getopt-repeated-options-fix Fix issue 17327 - std.getopt: Repeated boolean command option fails. merged-on-behalf-of: Steven Schveighoffer <[email protected]>
Comment #5 by github-bugzilla — 2017-06-17T11:34:33Z
Commits pushed to stable at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/c1d49fc4948977239fa4ed8c15b3d318b3e60ca9 Fix issue 17327 - std.getopt: Repeated boolean command option fails. https://github.com/dlang/phobos/commit/684f41b64ee96a35bd9fba3aca95e5cdbf99d09c Fix issue 17327. Review comments: drop continue statement. https://github.com/dlang/phobos/commit/5aad85503a8bfb96dbf357af2758f01dc64cba60 Merge pull request #5334 from jondegenhardt/getopt-repeated-options-fix
Comment #6 by github-bugzilla — 2018-01-05T13:28:19Z
Commits pushed to dmd-cxx at https://github.com/dlang/phobos https://github.com/dlang/phobos/commit/c1d49fc4948977239fa4ed8c15b3d318b3e60ca9 Fix issue 17327 - std.getopt: Repeated boolean command option fails. https://github.com/dlang/phobos/commit/684f41b64ee96a35bd9fba3aca95e5cdbf99d09c Fix issue 17327. Review comments: drop continue statement. https://github.com/dlang/phobos/commit/5aad85503a8bfb96dbf357af2758f01dc64cba60 Merge pull request #5334 from jondegenhardt/getopt-repeated-options-fix