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
Comment #2 by hsteoh — 2013-02-28T20:00:41Z
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
Commits pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/ba8ea757b984a860ef25fd23a63a698543311a3f Fixes Issue 9260 - std.getopt: Implement boolean parsing with optional argument. https://github.com/D-Programming-Language/phobos/commit/2ad62682c4a7a9b269b566689a2ed5dd444f865c Merge pull request #1050 from AndrejMitrovic/Fix9260 Issue 9260 - std.getopt: Implement boolean parsing with optional argument