nopt icon indicating copy to clipboard operation
nopt copied to clipboard

[BUG] Boolean arg swallows following arg if it is `false` or `true`

Open cspotcode opened this issue 9 years ago • 0 comments

Boolean options should not swallow the following argument if it is false or true. This can occur coincidentally and is probably not what the user wants.

Consider a CLI tool that outputs the size of files, where each file is passed as a positional argument. The -h or --human flag causes it to output human-friendly values. ("1KB" instead of "1024")

$ file-sizes -h *
file-1 1MB
file-2 2.3MB

Now imagine that the files in CWD are "false", "foo", and "bar"

$ file-sizes -h *
foo 1035
bar 345
# Errors: -h flag isn't set; "false" is omitted

For this reason, boolean options should never interpret the following argument as their value.

# following positional argument should *never* be interpreted as a value for the `--human` flag
file-sizes --human true

Users can still pass --human, --no-human, --human=true, or --human=false to explicitly set the flag to true or false.

cspotcode avatar Dec 05 '16 20:12 cspotcode