Having options in argparse with a dash
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Python argparse, option flags commonly use dashes, such as --dry-run or -v. Internally, argparse converts long option names to attribute names by replacing dashes with underscores. Understanding this mapping prevents confusion when reading parsed values.
This article shows best practices for dashed options, including boolean flags, aliases, and options that need literal dash-prefixed values.
Core Sections
1. Define short and long dashed options
Note --dry-run becomes args.dry_run.
2. Explicit destination names
Use dest when you need stable internal naming independent of CLI flag spelling.
3. Passing values that start with -
When an argument value itself begins with dash, use -- terminator.
After --, remaining tokens are treated as positional/values, not options.
4. Backward-compatible aliases
Aliases help migrate CLI interfaces without breaking scripts.
5. Build a repeatable validation checklist
After implementing argparse dashed-option design, create a small validation pack that runs the same way on developer machines, CI, and staging. The checklist should include a baseline case, an edge case, and a failure-path case with expected outcomes written in plain language. This avoids the common situation where a workflow appears correct in one environment but fails under a slightly different runtime, dependency version, or input distribution.
A useful checklist should also capture environment assumptions explicitly: runtime version, dependency versions, configuration flags, and external services required by the scenario. Teams often skip this because it feels obvious during initial implementation, but those hidden assumptions are exactly what cause regressions during upgrades and handoffs.
Treat this checklist as a versioned artifact. If code behavior changes, update expected results in the same pull request rather than relying on informal tribal memory. Coupling implementation and validation updates keeps argparse dashed-option design reliable as the codebase evolves.
6. Operational hardening and maintenance
Long-term reliability for argparse dashed-option design depends on observability and clear ownership. Add structured logs and metrics around the most failure-prone operations so incident responders can quickly identify whether failures come from input quality, configuration mismatch, external dependency drift, or code regressions. Without those signals, teams spend most of incident time reconstructing context instead of fixing root causes.
Also define who owns periodic compatibility checks. Libraries, runtimes, cloud APIs, and tooling change over time, and silent drift is common. Schedule lightweight smoke checks that run even when no feature work is active, and record results so there is an audit trail for when behavior started to diverge.
Finally, document rollback criteria ahead of time. If a deployment changes argparse dashed-option design behavior unexpectedly, the team should know when to roll back immediately versus when to hot-fix forward. This turns operational response from improvisation into a controlled process and prevents repeated incidents.
Common Pitfalls
- Expecting
args.dry-runattribute instead ofargs.dry_run. - Forgetting
--when passing dash-leading literal values. - Reusing short flags with conflicting meanings.
- Making required options positional by mistake due to typo in
add_argument. - Changing option names without compatibility aliases for existing automation.
Summary
Dashed options in argparse are straightforward once you remember the underscore mapping in parsed attributes. Use clear long flags, keep aliases for compatibility, and document value-passing rules for dash-leading strings. This yields a CLI that is both script-friendly and maintainable over time.
Related reading
- hdf5 not supported please install/reinstall h5py Scipy not supported when importing TFLearn?
- heapq with custom compare predicate
- Heavy usage of Python at Google
- Hidden features of Python
- Hidden features of Python
- Hidden import Tensorflow package not found when using Pyinstaller
- Hide all warnings in IPython
- Hiding axis text in matplotlib plots
.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.