Skip to content

Instantly share code, notes, and snippets.

@icy
Created July 29, 2018 09:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save icy/b8ed758b48134b369e854205aeb8f308 to your computer and use it in GitHub Desktop.
Save icy/b8ed758b48134b369e854205aeb8f308 to your computer and use it in GitHub Desktop.
std.getopt.problem.d
#!/usr/bin/env rdmd
/*
Author : Ky-Anh Huynh
License : MIT
Purpose : A bug in std.getopt
Date : 2018 07 29
Description:
$ dmd --version
DMD64 D Compiler v2.081.1-dirty
Copyright (C) 1999-2018 by The D Language Foundation, All Rights Reserved written by Walter Bright
$ 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"
*/
import std.stdio, std.format, std.getopt;
void main(string[] args) {
uint s_long = 0;
auto results = getopt(args,
std.getopt.config.noBundling,
std.getopt.config.passThrough,
"longer|long|l+", &s_long
);
if (results.helpWanted) {
defaultGetoptPrinter("Basic usage:", results.options);
}
stderr.writefln("How long it is: %d", s_long);
stderr.writefln("Remained: %(%s %)", args);
}
@icy
Copy link
Author

icy commented Jul 31, 2018

@icy
Copy link
Author

icy commented Jul 31, 2018

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment