Sphinx Argparse Extension
sphinx-argparse is a Sphinx extension that automatically documents `argparse` commands and options by parsing Python scripts or functions. It helps generate command-line interface documentation directly from your `argparse` definitions. The current version is 0.5.2, and it maintains a moderate release cadence, with several updates per year.
Warnings
- breaking Starting with version 0.5.0, `sphinx-argparse` officially requires Python 3.10 or newer. Older versions supported Python 3.7+.
- gotcha The project's GitHub repository moved from `ashb/sphinx-argparse` to `sphinx-doc/sphinx-argparse`. Older links or internal references might point to the outdated repository.
- gotcha When documenting a function that returns an `ArgumentParser` instance, ensure the `func` option in the `argparse` directive correctly points to the function (e.g., `my_module.my_function`). If documenting a script, ensure the script is importable and that `sys.path` is configured correctly in `conf.py`.
Install
-
pip install sphinx-argparse
Imports
- sphinx_argparse
# In conf.py extensions = [ 'sphinx_argparse' ]
Quickstart
# conf.py
import os
import sys
sys.path.insert(0, os.path.abspath('.'))
project = 'My Project'
copyright = '2023, Your Name'
extensions = [
'sphinx_argparse'
]
# my_script.py (example script to document)
import argparse
def main():
parser = argparse.ArgumentParser(description='A sample command line tool.')
parser.add_argument('--foo', help='The foo argument.', default='bar')
parser.add_argument('-v', '--version', action='version', version='%(prog)s 1.0')
subparsers = parser.add_subparsers(dest='command', help='sub-command help')
parser_a = subparsers.add_parser('subcommand-a', help='subcommand a help')
parser_a.add_argument('--baz', help='The baz argument for subcommand a.')
args = parser.parse_args()
print(f'Args: {args}')
if __name__ == '__main__':
main()
# my_documentation_page.rst
# .. argparse:: my_script.py
# :func: main.parser
# :prog: my-cli
#
# (Note: For this example, you would place `my_script.py` in the same directory as `conf.py` or adjust `sys.path` accordingly.)