On this page
argparse.ArgumentParser
Prev: argparse.ArgumentError | Up: argparse | Next: argparse.ArgumentTypeError
Rel: argparse.Action, argparse.ArgumentDefaultsHelpFormatter, argparse.ArgumentError, argparse.ArgumentTypeError
Ref: docs.python.org — argparse.ArgumentParser
Tags: #public #python3 #stdlib #argparse
Import
import argparse
from argparse import ArgumentParser
Signature & Overview
argparse.ArgumentParser(prog=None, usage=None, description=None, epilog=None, parents=[], formatter_class=<class 'argparse.HelpFormatter'>, prefix_chars='-', fromfile_prefix_chars=None, argument_default=None, conflict_handler='error', add_help=True, allow_abbrev=True, exit_on_error=True, *, suggest_on_error=False, color=True)
Object for parsing command line strings into Python objects.
Methods & Attributes
ArgumentParser.add_argument(self, *args, **kwargs)— add_argument(option_string, option_string, ..., name=value, ...)ArgumentParser.add_argument_group(self, *args, **kwargs)—ArgumentParser.add_argument_groupmethod/property.ArgumentParser.add_mutually_exclusive_group(self, **kwargs)—ArgumentParser.add_mutually_exclusive_groupmethod/property.ArgumentParser.add_subparsers(self, **kwargs)—ArgumentParser.add_subparsersmethod/property.ArgumentParser.convert_arg_line_to_args(self, arg_line)—ArgumentParser.convert_arg_line_to_argsmethod/property.ArgumentParser.error(self, message)— Prints a usage message incorporating the message to stderr and exits.ArgumentParser.exit(self, status=0, message=None)—ArgumentParser.exitmethod/property.ArgumentParser.format_help(self)—ArgumentParser.format_helpmethod/property.ArgumentParser.format_usage(self)—ArgumentParser.format_usagemethod/property.ArgumentParser.get_default(self, dest)—ArgumentParser.get_defaultmethod/property.ArgumentParser.parse_args(self, args=None, namespace=None)—ArgumentParser.parse_argsmethod/property.ArgumentParser.parse_intermixed_args(self, args=None, namespace=None)—ArgumentParser.parse_intermixed_argsmethod/property.ArgumentParser.parse_known_args(self, args=None, namespace=None)—ArgumentParser.parse_known_argsmethod/property.ArgumentParser.parse_known_intermixed_args(self, args=None, namespace=None)—ArgumentParser.parse_known_intermixed_argsmethod/property.ArgumentParser.print_help(self, file=None)—ArgumentParser.print_helpmethod/property.ArgumentParser.print_usage(self, file=None)—ArgumentParser.print_usagemethod/property.ArgumentParser.register(self, registry_name, value, object)—ArgumentParser.registermethod/property.ArgumentParser.set_defaults(self, **kwargs)—ArgumentParser.set_defaultsmethod/property.
Usage
import argparse
p = argparse.ArgumentParser(prog="tool", description="Resize images")
p.add_argument("input", help="Source path")
p.add_argument("-w", "--width", type=int, default=800)
p.add_argument("-v", "--verbose", action="store_true")
args = p.parse_args(["photo.png", "--width", "640", "-v"])
assert (args.input, args.width, args.verbose) == ("photo.png", 640, True)
Gotchas
- Pass an explicit list in tests (
parse_args([...])): Callingparse_args()with no arguments readssys.argv[1:], which breaks under pytest/unittest. - Hyphens become underscores:
--dry-runis stored onargs.dry_run.
REPL Inspect
from argparse import ArgumentParser
help(ArgumentParser)
Linked from
- tuts/python3/stdlib_module/argparse
- tuts/python3/stdlib_module/argparse/Action
- tuts/python3/stdlib_module/argparse/ArgumentDefaultsHelpFormatter
- tuts/python3/stdlib_module/argparse/ArgumentError
- tuts/python3/stdlib_module/argparse/ArgumentTypeError
- tuts/python3/stdlib_module/argparse/BooleanOptionalAction
- tuts/python3/stdlib_module/argparse/FileType
- tuts/python3/stdlib_module/argparse/HelpFormatter
- tuts/python3/stdlib_module/argparse/MetavarTypeHelpFormatter
- tuts/python3/stdlib_module/argparse/Namespace
- tuts/python3/stdlib_module/argparse/RawDescriptionHelpFormatter
- tuts/python3/stdlib_module/argparse/RawTextHelpFormatter
Related notes
- tuts/python3/stdlib_module/argparse/Action Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentDefaultsHelpFormatter Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentError Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentTypeError Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse Linked here; links to this page; shared tags: #stdlib
- tuts/python3/stdlib_module/argparse/BooleanOptionalAction links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/FileType links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/HelpFormatter links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/MetavarTypeHelpFormatter links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/Namespace links to this page; shared tags: #argparse, #stdlib