Results.

SCEPTRA.TECH

Python argparse Boolean Flags: Why --verbose False Can Still Mean True

Original diagram contrasting bool conversion of a nonempty string with a presence flag

A command such as --verbose False looks as if it should disable verbose output. In a Python CLI using argparse with type=bool, it can do the opposite. The string is not empty, so Python converts it to True.

This is a parser-design problem, not a reason to ask users to experiment with capitalization. I would decide whether an option is a switch or a value, then make the command syntax match that choice.

Why type=bool does not parse the word false

The type argument supplies a conversion function for an argument's text. Passing bool asks for Python truth-value conversion, not recognition of human words for yes and no.

Python's argparse reference explicitly discourages this converter. Empty strings become false; nonempty strings become true. That includes False and 0. Adding shell quotes around those words does not make their contents empty.

I checked a small parser with type=bool and passed ['--verbose', 'False']. Its parsed value was True. That is a limited local demonstration of the documented behavior, not a test of every command-line wrapper.

Use a presence flag for a simple switch

For verbose logging that is off unless requested, define a flag without a separate value:

import argparse

parser = argparse.ArgumentParser()
parser.add_argument('--verbose', action='store_true')
args = parser.parse_args()
print(args.verbose)

Save this as cli.py, then run it using the command that launches Python 3 on your machine:

python cli.py
python cli.py --verbose

The first prints False; the second prints True. I ran both parser cases locally and confirmed those results. With this design, the syntax is --verbose, not --verbose False. A separate False token is not the flag's value and may be rejected or interpreted as another argument according to the rest of the parser.

Conversely, action='store_false' provides a flag that stores false when present and defaults to true when absent, unless you override its default. Name such an option clearly, for example a negative switch that disables a behavior. Avoid names whose meaning reverses twice.

Use explicit choices when a value is the point

Some options deserve a word after them. Color output, for example, often needs an automatic mode as well as forced-on and forced-off modes:

parser.add_argument(
    '--color',
    choices=['auto', 'always', 'never'],
    default='auto',
)

This defines a string choice, not a Boolean. An invocation such as --color never preserves that explicit mode. Your application must still implement what each mode does. The parser does not turn terminal colors on or off by itself.

If an existing external interface requires true and false values, use a deliberately defined vocabulary and reject anything outside it. Do not silently treat every unrecognized word as true.

Check the default and the invalid case

Review the generated --help text and test at least the absent option, the intended invocation and an invalid invocation. If you change a released CLI from value syntax to a presence flag, update scripts and documentation that call it; parser correctness does not make a breaking change harmless.

Most importantly, a parsed flag is only input to your program. This example prints a value. It does not enforce a safety mode, prevent writes or guarantee that an operation respects the option. Keep those behaviors explicit and test them separately.

Sources

Comments