argparse
Python
command-line arguments
programming
tutorial

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

python
1import argparse
2
3parser = argparse.ArgumentParser()
4parser.add_argument('-v', '--verbose', action='store_true')
5parser.add_argument('--dry-run', action='store_true')
6args = parser.parse_args()
7
8print(args.verbose, args.dry_run)

--dry-run becomes args.dry_run.

Setting the attribute name with dest

python
parser.add_argument('--max-retries', dest='max_retries', type=int, default=3)

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:

bash
python app.py -- -abc          # positional argument "-abc"
python app.py --pattern=-abc   # option value "-abc"

-- 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

python
parser.add_argument('--config-file', '--config', dest='config_file')

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:

text
1validation checklist
2- baseline scenario with expected output shape and values
3- edge scenario with constrained or unusual input
4- failure scenario with expected fallback or error behavior
5- runtime/dependency/config assumptions for reproducibility

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:

bash
# example maintenance check command pattern
make smoke-test

Mistakes to avoid with dashed options

  • Reading args.dry-run instead of args.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.

Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Basis Lab
No 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 course
Data 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.

Practice algorithms