argparse module How to add option without any argument?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In argparse, an option without an argument is called a flag. Instead of consuming a value such as --port 8080, the flag changes program behavior simply by being present. The normal way to define one is with an action such as store_true or store_false.
Use store_true for a Simple Flag
If you want --verbose to mean "turn verbose mode on," use action="store_true".
Now:
prints False, while:
prints True.
The option does not need any extra argument because the presence of the flag itself is the signal.
Use store_false for the Reverse Meaning
Sometimes the default should be True and the flag should turn something off.
With no flag, use_cache is True. If --no-cache is present, it becomes False.
This pattern is clearer than forcing users to type --cache false.
Use BooleanOptionalAction for Paired Flags
Modern Python also supports a paired boolean style through BooleanOptionalAction.
This automatically gives you both:
- '
--feature' - '
--no-feature'
That is useful when you want symmetrical on and off flags without defining two separate arguments manually.
Count Repeated Flags When Needed
Some CLIs use repeated flags such as -v or -vv to increase verbosity. In that case, use count.
Examples:
This is still an option without a separate argument, but it gives you more than a simple boolean.
Do Not Use type=bool for Flags
A common beginner mistake is writing something like this:
That means the option expects a value, which is a different interface.
If your goal is a presence-based switch, use action, not type=bool.
A Practical Example
This produces a clean, typical CLI design where flags describe behavior and positional arguments describe required input.
Short Flags and Long Flags Together
Many CLIs expose both a short form and a long form for the same flag. argparse supports that naturally.
This lets users choose whichever form fits the situation:
- '
-qfor quick terminal use' - '
--quietfor readability in scripts and documentation'
That small design choice makes a CLI easier to use without changing the parsing model at all.
Common Pitfalls
The most common mistake is defining a flag with type=bool and then wondering why argparse expects an additional argument.
Another issue is choosing confusing names such as --disable-no-cache, which makes the resulting boolean hard to reason about.
Developers also sometimes forget the default value implied by store_true and store_false. Read the resulting attribute name carefully so the boolean is easy to understand later in the code.
Finally, if you want both --feature and --no-feature, do not hand-roll awkward parsing logic when BooleanOptionalAction already matches that use case.
Summary
- Use
action="store_true"for a flag that turns something on. - Use
action="store_false"for a flag that turns something off. - Use
BooleanOptionalActionwhen you want paired on and off flags. - Use
action="count"for repeated flags such as-vv. - Do not use
type=boolwhen the option should not take an argument.
Related reading
- Argparse optional positional arguments?
- Argparse optional positional arguments?
- Argparse Required arguments listed under optional arguments?
- Argparse Way to include default values in '--help'?
- ARIMA Forecast Cannot cast ufunc subtract output from dtype'float64' to dtype'int64' with casting rule 'same_kind
- as_list is not defined on an unknown TensorShape
- Asking for examples of async generators not directly transformable into manually implemented async iteration
- assertEquals vs. assertEqual in python
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.