Skip to content

Allow boolean literals as values for flags #1649

@emilk

Description

@emilk

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-parsingArea: Parser's logic and needs it changed somehow.C-enhancementCategory: Raise on the bar on expectationsS-waiting-on-decisionStatus: Waiting on a go/no-go before implementing

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions