[Issue 17327] std.getopt: repeated options unrecognised

via Digitalmars-d-bugs digitalmars-d-bugs at puremagic.com
Fri Apr 14 23:09:52 PDT 2017


https://issues.dlang.org/show_bug.cgi?id=17327

Jon Degenhardt <jrdemail2000-dlang at yahoo.com> changed:

           What    |Removed                     |Added
----------------------------------------------------------------------------
                 CC|                            |jrdemail2000-dlang at yahoo.co
                   |                            |m

--- Comment #1 from Jon Degenhardt <jrdemail2000-dlang at yahoo.com> ---
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

--


More information about the Digitalmars-d-bugs mailing list