Compile this program:
===============
void handleH(){assert(false);}
void handleG(){}
void main(string[] args)
{
import std.getopt;
getopt(args, config.passThrough, "g", &handleG);
getopt(args, config.passThrough, "h", &handleH);
}
===============
invoke with the -h option.
An assert error should happen.
There's a problem with the help system. If you replace "h" by "H" and call the program with -H, then the assertion fails as expected.
Comment #1 by b2.temp — 2017-01-05T09:45:05Z
@Alexandru, I think that the best fix is to
1/ add a static assert() that prevents to specify "-h" or "--help" (for example there's already "optionValidator " that does a few static checks)
2/ also update the documentation, saying that this switch is handled automatically
The real problem I had before reporting was 1 hour lost trying to get why a function pointer was never called. With a static check this will never happen anymore to anyone.
Comment #2 by b2.temp — 2017-01-05T09:49:42Z
(In reply to b2.temp from comment #1)
> @Alexandru, I think that the best fix is to
>
> 1/ add a static assert() that prevents to specify "-h" or "--help" (for
> example there's already "optionValidator" that does a few static checks)
>
> 2/ also update the documentation, saying that this switch is handled
> automatically
>
> The real problem I had before reporting was 1 hour lost trying to get why a
> function pointer was never called. With a static check this will never
> happen anymore to anyone.
Forgot to say that the only way to handle "h" should be the getopt results, e.g
void main(string[] args)
{
import std.getopt;
auto h = getopt(args, config.passThrough);
if (h.helpWanted) {}
}
That's why it can be rejected at compile time.
Comment #4 by dlang-bugzilla — 2017-07-21T09:16:57Z
This is a regression.
Simplified test case:
/////////////// test.d ///////////////
void main()
{
import std.getopt;
string[] args = ["program", "-h"];
getopt(args, config.passThrough);
assert(args == ["program", "-h"]);
}
//////////////////////////////////////
Introduced in https://github.com/dlang/phobos/pull/2072
(In reply to b2.temp from comment #1)
> @Alexandru, I think that the best fix is to
>
> 1/ add a static assert() that prevents to specify "-h" or "--help" (for
> example there's already "optionValidator" that does a few static checks)
What if I want to generate my own help screen? E.g. if I'm using a library around std.getopt that generates its own enhanced help screen (e.g. ae.utils.funopt).
Comment #5 by dlang-bugzilla — 2017-07-21T09:18:43Z
(In reply to b2.temp from comment #2)
> Forgot to say that the only way to handle "h" should be the getopt results,
> e.g
I guess "helpWanted" is a way to do that, but this was still a breaking change.
Also, I don't think the check can be done at compile-time, since the argument list to getopt consists of regular runtime arguments, so I don't think you can get the switch string literals at compile-time (from within getopt) here.
Comment #6 by robert.schadek — 2024-12-01T16:28:14Z