Python Glossary Русский

all Python 3argparseterm

argparse

module

Command-line argument parsing: options, positional arguments and an automatic --help.

Syntax

import argparse

The standard choice for any script that takes arguments: ArgumentParser checks types, prints help and reports errors itself. Hand-parsing sys.argv only suits one or two arguments, and getopt and optparse are older predecessors. On a parse error, parse_args exits via SystemExit instead of raising an ordinary exception.

Examples

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('name')
parser.add_argument('--count', type=int, default=1)
args = parser.parse_args(['Ann', '--count', '3'])
print(args.name, args.count)  # → Ann 3

See also

Common errors

Open in the full glossarydocs.python.orgReport an inaccuracy