-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Description
The behavior of --flag=false
setting the flag to the value true
has caused problems for me many times. The problem is that boolean arguments are treated differently than arguments of other types.
As an example, say you have a few arguments to your program being called from someplace like so:
my_app --name=${name} --age=${age} --alive=${alive}
The first two will work as expected, but the third will be a bug with no error or warning message. This special case for the boolean type is very annoying. I would advocate a more orthogonal design.
Here is my suggestion for how to parse boolean flags:
Argument | Flag value |
---|---|
empty | false |
--flag |
true |
--flag=true |
true |
--flag=false |
false |
--flag=other |
Helpful error message |
The problem with this comes when considering how to treat other potential ways to express boolean, e.g. 0/1
, no/yes
, False/True
. We could add these as well to the above table, or stick to the Rust convention (true/false
). Either way would work for me.
In summary: the principle of least surprise suggests that if --int=42
should work the same as --bool=false
.