Bug 18019 – getopt: different behaviour for built-in and custom options

Status
NEW
Severity
minor
Priority
P3
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2017-11-29T08:58:36Z
Last change time
2024-12-01T16:31:10Z
Assigned to
No Owner
Creator
Dmitry
Moved to GitHub: phobos#10272 →

Comments

Comment #0 by dmitry — 2017-11-29T08:58:36Z
Code: import std.stdio, std.getopt; void main(string[] args) { bool replace; auto helpInformation = getopt(args, "replace|r", "Overwrite original file", &replace); if (helpInformation.helpWanted) { defaultGetoptPrinter("test", helpInformation.options); return; } } As I understand, now getopt should recognize "-r", "--replace", "-h" and "--help" options. And they works fine: $ app.exe -h test -r --replace Overwrite original file -h --help This help information. $ app.exe -r $ app.exe --help test -r --replace Overwrite original file -h --help This help information. $ app.exe --replace But if enter "-help" or "-replace", there is a problem. $ app.exe -help std.getopt.GetOptException@C:\D\dmd2\windows\bin\..\..\src\phobos\std\getopt.d(789): Unrecognized option -help $ app.exe -replace std.conv.ConvException@C:\D\dmd2\windows\bin\..\..\src\phobos\std\conv.d(2018): Can't parse string: bool should be case-insensitive 'true' or 'false' ---------------- 0x00404620 0x00404814 0x004044E5 0x00402873 0x004022D7 0x0040221A 0x004094CF 0x00409493 0x00409394 0x00406CAF 0x765B336A in BaseThreadInitThunk 0x77DB98F2 in RtlInitializeExceptionChain 0x77DB98C5 in RtlInitializeExceptionChain 1) Why "-help" and "-replace" was recognized as options? They contains more than one letter and bundling is disabled (as default). 2) At least, the behaviour should be same - "-replace" should throw GetOptException. If I don't understand something obvious, correct me, please.
Comment #1 by b2.temp — 2017-11-29T14:15:53Z
(In reply to Dmitry from comment #0) > [...] > 1) Why "-help" and "-replace" was recognized as options? They contains more > than one letter and bundling is disabled (as default). > 2) At least, the behaviour should be same - "-replace" should throw > GetOptException. > > If I don't understand something obvious, correct me, please. with "-replace" It tries to detect "-rtrue" or "-rfalse", which is the short option + the boolean value for it. The ConvError is then caused by "to!bool("eplace")". I'm not sure if this report is valid but it's still possible to throw a better exception with "expected true or false following simple option" or something like that. Maybe you can change the report title. "Better error message for short option followed by wrong bool string".... + set it as enhancement, assuming you'd agree that the real problem is that of course.
Comment #2 by dmitry — 2017-11-29T14:25:37Z
Thank you for the explanation. Now I found it in the documentation: > If the option has a parameter, that must be "stuck" to the option > without any intervening space or "=" But it is not the reason why the issue was created. Main reason is that behavoiur is different for "-help" (built-in option) and "-replace" (custom option).
Comment #3 by robert.schadek — 2024-12-01T16:31:10Z
THIS ISSUE HAS BEEN MOVED TO GITHUB https://github.com/dlang/phobos/issues/10272 DO NOT COMMENT HERE ANYMORE, NOBODY WILL SEE IT, THIS ISSUE HAS BEEN MOVED TO GITHUB