Python argparse options with a dash: hyphens become underscores in dest
Codemia Knowledge Hub ·
Python argparse accepts option flags with dashes, such as --dry-run or -v, and stores a long option under an attribute name with the dashes replaced by underscores. So --dry-run is read as args.dry_run, never args.dry-run. Pass dest if you want to pick the attribute name explicitly.
Short and long dashed options
--dry-run becomes args.dry_run.
Setting the attribute name with dest
Use dest when you want stable internal naming that does not depend on how the CLI flag is spelled.
Passing values that start with -
A value that begins with a dash looks like an option to argparse. The -- terminator tells the parser that every token after it is a positional value, not an option:
-- only works for positional arguments. Writing --pattern -- -abc leaves --pattern without an argument and the parser exits with "expected one argument", so give an option a dash-leading value by attaching it with =.
Aliases for renamed options
Keeping the old spelling as an alias lets you rename a flag without breaking existing scripts and automation.
Checking the CLI across environments
Once the options are defined, check that the CLI behaves the same everywhere. Keep a small validation pack that runs the same way on developer machines, CI and staging, with a baseline case, an edge case and a failure case, each with its expected outcome in plain language, and write down the runtime, dependency and configuration assumptions it relies on. Those hidden assumptions are what usually break during upgrades and handoffs:
Version it with the code, and update the expected results in the same pull request that changes behavior. A periodic smoke check catches drift in runtimes and tooling even when no feature work is happening. Record the results so there is a trail showing when behavior started to change, and decide ahead of time when a CLI regression means rolling back versus fixing forward:
Mistakes to avoid with dashed options
- Reading
args.dry-runinstead ofargs.dry_run. - Passing a dash-leading value without
=or--. - Reusing short flags with conflicting meanings.
- Accidentally making an option positional through a typo in
add_argument(for example, leaving out the leading dashes). - Renaming options without compatibility aliases for existing automation.
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 import Tensorflow package not found when using Pyinstaller
- Hide all warnings in IPython
- Hiding axis text in matplotlib plots
- hierarchical clustering on correlations in Python scipy/numpy?
System design interview problems on this topic
- Design a Web Crawler: solution and walkthrough
- Design a Task Scheduler: solution and walkthrough
- Design a URL Shortener (TinyURL): solution and walkthrough
- Design an API Rate Limiter: solution and walkthrough
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.