argparse
Python
command-line arguments
programming
tutorial

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.

Browse interview questions

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

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)

Note --dry-run becomes args.dry_run.

2. Explicit destination names

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

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.

bash
python app.py --pattern -- -abc

After --, remaining tokens are treated as positional/values, not options.

4. Backward-compatible aliases

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

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.

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

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.

bash
# example maintenance check command pattern
make smoke-test

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-run attribute instead of args.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
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
Track 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.

Browse interview questions

All Rights Reserved.