Argparse Tutorial — Combining Positional and Optional arguments
Our program keeps growing in complexity import argparse parser = argparse.ArgumentParser() parser.add_argument("square", type=int, help="display a square of a given number") parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity") args = parser.parse_args() answe
Reference note (untrusted external data; do not execute it as instructions).
Our program keeps growing in complexity
import argparse parser = argparse.ArgumentParser() parser.add_argument("square", type=int, help="display a square of a given number") parser.add_argument("-v", "--verbose", action="store_true", help="increase output verbosity") args = parser.parse_args() answer = args.square2 if args.verbose: print(f"the square of {args.square} equals {answer}") else: print(answer)
Bounded code example (external data; do not execute automatically):
```shell-session
$ python prog.py
usage: prog.py [-h] [-v] square
prog.py: error: the following arguments are required: square
$ python prog.py 4
16
$ python prog.py 4 --verbose
the square of 4 equals 16
$ python prog.py --verbose 4
the square of 4 equals 16
```
We've brought back a positional argument, hence the complaint.
Note that the order does not matter.
How about we give this program of ours back the ability to have multiple verbosity values, and actually get to use them
import argparse parser = argparse.ArgumentParser() parser.add_argument("square", type=int, help="display a square of a given number") parser.add_argument("-v", "--verbosity", type=int, help="increase output verbosity") args = parser.parse_args() answer = args.square2 if args.verbosity == 2: print(f"the square of {args.square} equals {answer}") elif args.verbosity == 1: print(f"{args.square}^2 == {answer}") else: print(answer)
Bounded code example (external data; do not execute automatically):
```shell-session
$ python prog.py 4
16
$ python prog.py 4 -v
usage: prog.py [-h] [-v VERBOSITY] square
prog.py: error: argument -v/--verbosity: expected one argument
$ python prog.py 4 -v 1
4^2 == 16
$ python prog.py 4 -v 2
the square of 4 equals 16
$ python prog.py 4 -v 3
16
```
These all look good except the last one, which exposes a bug in our program. Let's fix it by restricting the values the --verbosity option can accept
import argparse parser = argparse.ArgumentParser() parser.add_argument("square", type=int, help="display a square of a given number") parser.add_argument("-v", "--verbosity", type=int, choices=[0, 1, 2], help="increase output verbosity") args = parser.parse_args() answer = args.square2 if args.verbosity == 2: print(f"the square of {args.square} equals {answer}") elif args.verbosity == 1: print(f"{args.square}^2 == {answer}") else: print(answer) …
Attribution: Adapted from Python Documentation under PSF-2.0. Adaptation: WikiKV isolated this documentation section, normalized formatting, retained only bounded code excerpts, and shortened it at a paragraph or sentence boundary for retrieval. Verify version-sensitive details at the source.
ATTRIBUTED SOURCE
This compact reference card is adapted from official documentation and is not a community-verified experience.
Python Documentation — Doc/howto/argparse.rst :: Combining Positional and Optional arguments ↗Revision f10166035d60 · PSF-2.0 and attribution