On this page
argparse.HelpFormatter
Prev: argparse.FileType | Up: argparse | Next: argparse.MetavarTypeHelpFormatter
Rel: argparse.Action, argparse.ArgumentDefaultsHelpFormatter, argparse.ArgumentError, argparse.ArgumentParser
Ref: docs.python.org — argparse.HelpFormatter
Tags: #public #python3 #stdlib #argparse
Import
import argparse
from argparse import HelpFormatter
Signature & Overview
argparse.HelpFormatter(prog, indent_increment=2, max_help_position=24, width=None, color=True)
Formatter for generating usage messages and argument help strings.
Methods & Attributes
HelpFormatter.add_argument(self, action)—HelpFormatter.add_argumentmethod/property.HelpFormatter.add_arguments(self, actions)—HelpFormatter.add_argumentsmethod/property.HelpFormatter.add_text(self, text)—HelpFormatter.add_textmethod/property.HelpFormatter.add_usage(self, usage, actions, groups, prefix=None)—HelpFormatter.add_usagemethod/property.HelpFormatter.end_section(self)—HelpFormatter.end_sectionmethod/property.HelpFormatter.format_help(self)—HelpFormatter.format_helpmethod/property.HelpFormatter.start_section(self, heading)—HelpFormatter.start_sectionmethod/property.
Usage
import argparse
class WideHelp(argparse.HelpFormatter):
def __init__(self, prog: str) -> None:
super().__init__(prog, max_help_position=32, width=100)
p = argparse.ArgumentParser(prog="demo", formatter_class=WideHelp)
p.add_argument("--output-dir", help="Destination folder")
assert "--output-dir" in p.format_help()
Gotchas
- Pass the class, not an instance:
formatter_class=WideHelp(or a lambdalambda prog: HelpFormatter(prog, width=100)), becauseArgumentParserinstantiates it withprog.
REPL Inspect
from argparse import HelpFormatter
help(HelpFormatter)
Linked from
Related notes
- tuts/python3/stdlib_module/argparse/FileType Linked here; links to this page; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/MetavarTypeHelpFormatter 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/Action Linked here; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentDefaultsHelpFormatter Linked here; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentError Linked here; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentParser Linked here; shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/ArgumentTypeError shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/BooleanOptionalAction shared tags: #argparse, #stdlib
- tuts/python3/stdlib_module/argparse/Namespace shared tags: #argparse, #stdlib