Sub topic 3
Positional arguments
This chapter assumes you can write a Python function, run a script from a shell, and have read the previous chapter on creating a parser and calling parse_args.
What a positional argument is
A positional argument is a value that argparse identifies by its place on the command line, not by a flag name. In cp a.txt b.txt, the first value is the source and the second is the destination. You declare one by calling add_argument with a name that does not start with a dash.
import argparse
parser = argparse.ArgumentParser(prog="copy")
parser.add_argument("source", help="file to read")
parser.add_argument("dest", help="file to write")
parser.add_argument("copies", type=int, help="number of copies to make")
args = parser.parse_args(["a.txt", "b.txt", "3"])
print(args.source, args.dest, args.copies)This prints a.txt b.txt 3. The name you pass becomes the attribute on the result, so args.source holds the first value.
Order and requirement
Matching follows declaration order, not the order you think of the values. The first add_argument for a positional takes the first bare value, the second takes the second, and so on.
Every positional is required by default. If a value is missing, argparse prints a usage line and an error to standard error, then exits with status 2.
parser.parse_args(["a.txt"])usage: copy [-h] source dest copies
copy: error: the following arguments are required: dest, copiesBecause the parser calls sys.exit, this raises SystemExit instead of returning. Later chapters show how to test for it.
Type conversion
Command-line values arrive as strings. The type argument converts each one, and a failed conversion produces the same kind of clean usage error. Passing "many" for copies above gives invalid int value: 'many' and exit status 2. The converted value is what you read from args, so args.copies is an int.
Naming and display
The positional's name is also what appears in the usage line. Use metavar to change the displayed name without changing the attribute. This helps when you want a hyphenated label, because args.input-file is not valid Python.
parser = argparse.ArgumentParser(prog="count")
parser.add_argument("input_file", metavar="input-file", help="file to read")
args = parser.parse_args(["notes.txt"])
print(args.input_file)The usage line shows input-file, and your code reads args.input_file.
Common errors
- A value that starts with a dash. A value such as
-draft.txtis read as an option. Put--before it to end option parsing:copy -- -draft.txt out.txt 1. Negative numbers like-5work, as long as no option in the parser looks like a negative number. - Declaring a positional with a dash prefix.
add_argument("-name")creates an option, not a positional. Positionals never have leading dashes. - Wrong declaration order. Swapping two
add_argumentcalls swaps which value goes where. The usage line shows the order, so check it when values land in the wrong place. - Too many values. Extra bare values give
unrecognized arguments, again with exit status 2. - Using positionals for optional data. If a value has a sensible default, an option such as
--countis usually clearer. Positionals suit values the command cannot run without.
Exercise
A parser declares add_argument("src") and then add_argument("n", type=int). What happens when you run parse_args(["5", "file.txt"]), and why?
Show answer
The parser exits with status 2 and prints invalid int value: 'file.txt'. Values are matched in declaration order, so "5" goes to src and "file.txt" goes to n, where the int conversion fails.