Python argparse default value or specified value
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In argparse, a default value is used only when the option is omitted. If the user supplies the option, the supplied value wins. The real design question is often whether you need a simple default, or whether you also need to know whether the user explicitly provided the argument.
Basic Default Behavior
A normal default is straightforward.
Behavior:
- no
--portmeans the result is8080 - '
--port 5000means the result is5000'
This is the standard “default or specified value” behavior.
Use None When You Need to Detect Omission
Sometimes the program needs to know whether the user omitted the argument entirely. In that case, using a sentinel such as None is often enough.
This is useful when the real default comes from another source such as a config file or environment variable.
Combining CLI, Environment, and Hard Defaults
A common pattern is:
- command-line argument if provided
- environment variable if present
- hardcoded fallback if neither exists
This keeps argparse simple while still supporting layered configuration.
argparse.SUPPRESS for Advanced Cases
If you want omitted arguments to be absent from the namespace entirely, use argparse.SUPPRESS.
This is more explicit than None when None could itself be a meaningful value in your application logic.
Boolean Flags Are Different
Boolean flags often use actions such as store_true or store_false, which change how defaults work.
Here the default is False, and specifying --verbose sets it to True. That is still default-versus-specified behavior, but through an action rather than a typed value.
Common Pitfalls
Make Defaults Visible To Users
If a command-line option has an important fallback value, reflect that in the help text or documentation so callers are not surprised by hidden defaults at runtime.
Positional Arguments Follow Different Rules
Defaults for optional flags are common, but positional arguments behave differently because they are usually required unless you explicitly configure nargs or other parsing behavior. That distinction matters when you mix mandatory positional input with optional overrides in one CLI.
The most common mistake is setting a concrete default too early when the real value should come from a later configuration layer such as environment or file settings.
Another mistake is using None as a sentinel without checking whether None could also be a meaningful business value.
Developers also often forget that boolean flags use action-based behavior rather than ordinary typed defaults.
For complex CLIs, it is often clearer to keep parsing simple and apply higher-level fallback rules after parse_args() returns.
That keeps precedence rules visible in one place instead of hiding them inside parser defaults.
Summary
- '
argparseuses the default only when the option is omitted.' - A provided command-line value always overrides the default.
- Use
Noneorargparse.SUPPRESSwhen you need to detect whether the user specified the option. - Layer command-line, environment, and hardcoded defaults deliberately.
- Treat boolean flags as a slightly different case because they use actions.
Related reading
.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.