www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - std.getopt: Unexpected behavior when using incremental options

reply Ky-Anh Huynh <saigon example.net> writes:
Hi,

I am using std.getopt and expect to parse an incremental option 
with different names: --long, --longer, -l. The sample code is 
here

https://gist.github.com/icy/b8ed758b48134b369e854205aeb8f308

excerpt:

[code]
   auto results = getopt(args,
     std.getopt.config.noBundling,
     std.getopt.config.passThrough,
     "longer|long|l+", &s_long
);
[/code]

I don't expect the program also accepts `-long` , unfortunately 
it does. The default help message also prints incorrect 
information:

[code]
$ rdmd tests.d  -l -long --long --longer -h
Basic usage:
-long --longer
    -h   --help This help information.
How long it is: 4
Remained: 
"/tmp/.rdmd-314/rdmd-tests.d-0C7A52ECD8084516F3064EED864D6E86/tests"
[/code]


Did I have something wrong with my code?

Thanks for your reading and support.
Jul 29 2018
parent reply Cym13 <cpicard openmailbox.org> writes:
On Sunday, 29 July 2018 at 09:28:41 UTC, Ky-Anh Huynh wrote:
 Hi,

 I am using std.getopt and expect to parse an incremental option 
 with different names: --long, --longer, -l. The sample code is 
 here

 [...]
I think -long is actually taken as -l -o -n -g here. Since you didn't define -o, -n and -g it prints a usage error.
Jul 29 2018
parent Ky-Anh Huynh <saigon example.net> writes:
On Sunday, 29 July 2018 at 10:57:06 UTC, Cym13 wrote:
 On Sunday, 29 July 2018 at 09:28:41 UTC, Ky-Anh Huynh wrote:
 Hi,

 I am using std.getopt and expect to parse an incremental 
 option with different names: --long, --longer, -l. The sample 
 code is here

 [...]
I think -long is actually taken as -l -o -n -g here. Since you didn't define -o, -n and -g it prints a usage error.
I think that I've got that. There isn't any actual definition of short/long option names. That means if I provide [code] "l|long|longer+", &s_long [/code] getopt will define 6 variants: -l, --l, -long, --long, -longer, --longer, and here `-long`, `-l` and `-longer` are both short:) `Short` isn't related to the length of the option name. It's short because there is only one dash (-). This way using "short" is quite confusing. Regarding the output of the default help message, I should put `l` at the first position, as in `l|long|longer`. This is slightly different from examples in the official documentation (https://dlang.org/phobos/std_getopt.html) where you are expected to see `long|longer|l+` instead.
Jul 29 2018