# Human friendly input/output in Python. # # Author: Peter Odding # Last Change: June 11, 2021 # URL: https://humanfriendly.readthedocs.io """ Parsing and reformatting of usage messages. The :mod:`~humanfriendly.usage` module parses and reformats usage messages: - The :func:`format_usage()` function takes a usage message and inserts ANSI escape sequences that highlight items of special significance like command line options, meta variables, etc. The resulting usage message is (intended to be) easier to read on a terminal. - The :func:`render_usage()` function takes a usage message and rewrites it to reStructuredText_ suitable for inclusion in the documentation of a Python package. This provides a DRY solution to keeping a single authoritative definition of the usage message while making it easily available in documentation. As a cherry on the cake it's not just a pre-formatted dump of the usage message but a nicely formatted reStructuredText_ fragment. - The remaining functions in this module support the two functions above. Usage messages in general are free format of course, however the functions in this module assume a certain structure from usage messages in order to successfully parse and reformat them, refer to :func:`parse_usage()` for details. .. _DRY: https://en.wikipedia.org/wiki/Don%27t_repeat_yourself .. _reStructuredText: https://en.wikipedia.org/wiki/ReStructuredText """ # Standard library modules. import csv import functools import logging import re # Standard library module or external dependency (see setup.py). from importlib import import_module # Modules included in our package. from humanfriendly.compat import StringIO from humanfriendly.text import dedent, split_paragraphs, trim_empty_lines # Public identifiers that require documentation. __all__ = ( 'find_meta_variables', 'format_usage', 'import_module', # previously exported (backwards compatibility) 'inject_usage', 'parse_usage', 'render_usage', 'USAGE_MARKER', ) USAGE_MARKER = "Usage:" """The string that starts the first line of a usage message.""" START_OF_OPTIONS_MARKER = "Supported options:" """The string that marks the start of the documented command line options.""" # Compiled regular expression used to tokenize usage messages. USAGE_PATTERN = re.compile(r''' # Make sure whatever we're matching isn't preceded by a non-whitespace # character. (?