argparse — Parser for command-line options, arguments and subcommands — Python 3.14.7 documentation

docs.python.org

Tin mới

Command-line interface libraries

Python Module Index

3.14.7 Documentation

The Python Standard Library

Choosing an argument parsing library

ArgumentParser objects¶

This page contains the API reference information. For a more gentle introduction to Python command-line parsing, have a look at the argparse tutorial.

Upgrading Optparse Code

If you’re looking for a guide about how to upgrade optparse code to argparse, see Upgrading Optparse Code.

filesystem encoding and error handler

Sometimes, when dealing with a particularly long argument list, it may make sense to keep the list of arguments in a file rather than typing it out at the command line. If the fromfile_prefix_chars= argument is given to

locale.getpreferredencoding(False)

Changed in version 3.12: ArgumentParser changed encoding and errors to read arguments files from default (e.g. locale.getpreferredencoding(False) and "strict") to the filesystem encoding and error handler. Arguments file

in your local environment

By default, the help message is printed in color using ANSI escape sequences. If you want plain text help messages, you can disable this in your local environment, or in the argument parser itself by setting color to Fal

Specifying ambiguous arguments

ArgumentParser objects usually associate a single command-line argument with a single action to be taken. The nargs keyword argument associates a different number of command-line arguments with a single action. See also

json.JSONDecodeError

By default, the parser reads command-line arguments in as simple strings. However, quite often the command-line string should instead be interpreted as another type, such as a float or int. The type keyword for add_argum