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