On this page
argparse.Action
Up: argparse | Next: argparse.ArgumentDefaultsHelpFormatter
Rel: argparse.ArgumentDefaultsHelpFormatter, argparse.ArgumentError, argparse.ArgumentParser, argparse.ArgumentTypeError
Ref: docs.python.org — argparse.Action
Tags: #public #python3 #stdlib #argparse
Import
import argparse
from argparse import Action
Signature & Overview
argparse.Action(option_strings, dest, nargs=None, const=None, default=None, type=None, choices=None, required=False, help=None, metavar=None, deprecated=False)
Information about how to convert command line strings to Python objects.
Methods & Attributes
Action.format_usage(self)—Action.format_usagemethod/property.
Usage
import argparse
class UpperAction(argparse.Action):
def __call__(self, parser, namespace, values, option_string=None):
setattr(namespace, self.dest, str(values).upper())
p = argparse.ArgumentParser()
p.add_argument("--env", action=UpperAction)
assert p.parse_args(["--env", "prod"]).env == "PROD"
Gotchas
- Always use
setattr(namespace, self.dest, ...): Never hardcode the attribute name, so customdest=values keep working. - For simple value parsing, prefer
type=: SubclassActiononly when you need access toparser,namespace, oroption_string.
REPL Inspect
from argparse import Action
help(Action)
Linked from
- tuts/python3/stdlib_module/argparse
- tuts/python3/stdlib_module/argparse/ArgumentDefaultsHelpFormatter
- tuts/python3/stdlib_module/argparse/ArgumentError
- tuts/python3/stdlib_module/argparse/ArgumentParser
- 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/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/ArgumentParser 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