Bug 9260 – getopt should allow setting booleans to false
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
phobos
Product
D
Version
D2
Platform
All
OS
All
Creation time
2013-01-02T19:12:00Z
Last change time
2013-03-08T10:40:32Z
Keywords
pull
Assigned to
andrej.mitrovich
Creator
andrej.mitrovich
Comments
Comment #0 by andrej.mitrovich — 2013-01-02T19:12:04Z
import std.getopt;
void main(string[] args)
{
bool b;
getopt(args, "b", &b);
}
If you pass:
$ rdmd test.d --b=true
The '=true' is skipped, as getopt only cares whether '--b' is present. The problem is that this leads to a user thinking that the opposite works:
$ rdmd test.d --b=false
However 'b' is still true in this case, the '=false' part is discarded.
The documentation *does* mention that booleans can only be set to on, however it only uses the syntax '--b' and never mentions '--b=true' (it might be an oversight allowing it). To avoid confusion and avoid code breakage, we should either:
1) Throw when syntax '--b=false' is used, because it has no effect
2) Implement --b=false
I think #2 would be the best choice here.
The current alternative is to use enums, ala:
enum B { no, yes }
import std.getopt;
void main(string[] args)
{
B b;
getopt(args, "b", &b);
}
$ rdmd test.d --b=no
But there should be no problem implementing #2.
Comment #1 by andrej.mitrovich — 2013-01-03T10:21:42Z
Would it be too much to support --no-<flagname> as a synonym for --flag=false? For example, --verbose means --verbose=true, --no-verbose means --verbose=false.
Comment #3 by github-bugzilla — 2013-03-08T10:39:02Z