Skip to main content

render CLI arguments (sub-commands friendly) defined by argparse module

Project description

sphinx-argparse-cli

PyPI PyPI - Implementation PyPI - Python Version Downloads PyPI - License check

Render CLI arguments (sub-commands friendly) defined by the argparse module. For live demo checkout the documentation of tox, pypa-build and mdpo.

Installation

python -m pip install sphinx-argparse-cli

Enable in conf.py

# just add it to your list of extensions to load within conf.py
extensions = ["sphinx_argparse_cli"]

use

Within the reStructuredText files use the sphinx_argparse_cli directive that takes, at least, two arguments:

Name Description
module the module path to where the parser is defined
func the name of the function that once called with no arguments constructs the parser
prog (optional) when provided, overwrites the <prog> name.
hook (optional) hook argparse to retrieve the parser if func uses a parser instead of returning it.
title (optional) when provided, overwrites the <prog> - CLI interface title added by default and when empty, will not be included
description (optional) when provided, overwrites the description and when empty, will not be included
epilog (optional) when provided, overwrites the epilog and when empty, will not be included
usage_width (optional) how large should usage examples be - defaults to 100 character
usage_first (optional) show usage before description
group_title_prefix (optional) groups subsections title prefixes, accepts the string {prog} as a replacement for the program name - defaults to {prog}
group_sub_title_prefix (optional) subcommands groups subsections title prefixes, accepts replacement of {prog} and {subcommand} for program and subcommand name - defaults to {prog} {subcommand}
no_default_values (optional) suppresses generation of default entries
force_refs_lower (optional) Sphinx :ref: only supports lower-case references. With this, any capital letter in generated reference anchors are lowered and given an _ prefix (i.e. A becomes _a)

For example:

.. sphinx_argparse_cli::
  :module: a_project.cli
  :func: build_parser
  :prog: my-cli-program

If you have code that creates and uses a parser but does not return it, you can specify the :hook: flag:

.. sphinx_argparse_cli::
  :module: a_project.cli
  :func: main
  :hook:
  :prog: my-cli-program

Refer to generated content

The tool will register reference links to all anchors. This means that you can use the sphinx ref role to refer to both the (sub)command title/groups and every flag/argument. The tool offers a configuration flag sphinx_argparse_cli_prefix_document (change by setting this variable in conf.py - by default False). This option influences the reference ids generated. If it's false the reference will be the anchor id (the text appearing after the '# in the URI once you click on it). If it's true the anchor id will be prefixed by the document name (this is useful to avoid reference label clash when the same anchors are generated in multiple documents).

For example in case of a tox command, and sphinx_argparse_cli_prefix_document=False (default):

  • to refer to the optional arguments group use :ref:`tox-optional-arguments` ,
  • to refer to the run subcommand use :ref:`tox-run` ,
  • to refer to flag --magic of the run sub-command use :ref:`tox-run---magic` .

For example in case of a tox command, and sphinx_argparse_cli_prefix_document=True, and the current document name being cli:

  • to refer to the optional arguments group use :ref:`cli:tox-optional-arguments` ,
  • to refer to the run subcommand use :ref:`cli:tox-run` ,
  • to refer to flag --magic of the run sub-command use :ref:`cli:tox-run---magic` .

Due to Sphinx's :ref: only supporting lower-case values, if you need to distinguish mixed case program names or arguments, set the :force_refs_lower: argument. With this flag, captial-letters in references will be converted to their lower-case counterpart and prefixed with an _. For example:

  • A prog name SampleProgram will be referenced as :ref:`_sample_program...` .
  • To distinguish between mixed case flags -a and -A use :ref:`_sample_program--a` and :ref:`_sample_program--_a` respectively

Note that if you are not concerned about using internal Sphinx :ref: cross-references, you may choose to leave this off to maintain mixed-case anchors in your output HTML; but be aware that later enabling it will change your anchors in the output HTML.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

sphinx_argparse_cli-1.18.2.tar.gz (12.5 kB view details)

Uploaded Source

Built Distribution

sphinx_argparse_cli-1.18.2-py3-none-any.whl (9.8 kB view details)

Uploaded Python 3

File details

Details for the file sphinx_argparse_cli-1.18.2.tar.gz.

File metadata

  • Download URL: sphinx_argparse_cli-1.18.2.tar.gz
  • Upload date:
  • Size: 12.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/5.1.1 CPython/3.12.6

File hashes

Hashes for sphinx_argparse_cli-1.18.2.tar.gz
Algorithm Hash digest
SHA256 4c6a0b35aee405459e3648a7a8967c15562a6ac3421bf62b1fae75dd1605ac6e
MD5 b1d14bcc95f8b37e1a1a035a13694415
BLAKE2b-256 b36831d6e1ce62bbc901774635ff14679ae5319d30b573553cf25d49126a71ea

See more details on using hashes here.

File details

Details for the file sphinx_argparse_cli-1.18.2-py3-none-any.whl.

File metadata

File hashes

Hashes for sphinx_argparse_cli-1.18.2-py3-none-any.whl
Algorithm Hash digest
SHA256 edb5afcb296d0a512b312af2b9b9e45fc56c1f1dd2e7549abc57f3ea6e77c552
MD5 51482bd63fe322f7e6de610d1377fb81
BLAKE2b-256 ca594e66cbfad7059158cd21532189980a44b949c2b81791ccb321abbd7dd171

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page