Skip to main content

A documentation-generated argument parser.

Project description

Help Text - a documentation-generated argument parser

Stop wasting time on over-engineered nonsense.

PEP 257 – Docstring Conventions

The docstring of a script (a stand-alone program) should be usable as its “usage” message, [...] as well as a complete quick reference to all options and arguments for the sophisticated user.

Which means your script can already do this:

"""
Usage: hello [-ghv]

OPTIONS
  -h, --help           display this help text and exit
  -v, --version        display version information and exit
  -g, --global         print a familiar message
"""

__version__ = '1.2.3'

import sys
from helptext import parse

opts, _ = parse(sys.argv[1:], __doc__, __version__)
if opts['--global']['enabled']:
    print('Hello, world!')
$ hello -h
Usage: hello [-ghv]

OPTIONS
  -h, --help           display this help text and exit
  -v, --version        display version information and exit
  -g, --global         print a familiar message
$ hello --version
1.2.3
$ hello -g
Hello, world!

Installation

$ python3 -m pip install helptext

API

Help Text provides a single parse function.

parse

from helptext import parse
opts, operands = parse(args, doc, version=None, posixly_correct=False)

Arguments

  • args: a list of command-line argument strings.
  • doc: a docstring containing an option list.
  • version: a version string.
  • posixly_correct: a boolean to enable POSIX mode.

If version is not None, checks for --help and then --version. If defined in doc and invoked in args, prints doc or version accordingly, then raises SystemExit(0).

If posixly_correct is True, support for GNU-style long options is disabled and non-option operands terminate argument parsing.

Return Values

  • opts: a dictionary of option flags (e.g. -h, --help):
    • flags: a list of flag aliases for this option.
    • argument: a boolean for whether an argument is required or forbidden.
    • enabled: an integer count of command-line invocations.
    • value: an option-argument string, default is None.
  • operands: a list of non-option operand strings.

Docstring Format

"""
  -a --alpha           this option takes no arguments
  -b arg --beta        this option requires an argument
  -c, --gamma arg      this option behaves the same
                       this line will be ignored
  -d, --delta=arg      attached notation works as well
"""
  • Leading whitespace is ignored.
  • Lines not beginning with a dash (-) are ignored.
  • Flags and option-arguments are separated by whitespace or commas (,).
  • All flags on a line are aliases to the same option.
  • A single option-argument will also set the requirement for all aliases.
  • Long option flags may attach option-arguments with an equals sign (=).
  • Line parsing is terminated by two consecutive spaces ( ).

Best Practices

Designing user interfaces is hard. Even simple text-based command-line interfaces are difficult to execute cleanly. We do have some standards to guide us (e.g. POSIX, GNU), but we are contending with several decades of legacy code and bad advice (argparse).

The features below are unsupported in favor of encouraging CLI best practices.

Feature Comment
Required options A required option is an oxymoron. Set a reasonable default or use positional arguments to accept input, otherwise subcommands can be used to change modes.
Mutually-exclusive options Reduce command-line clutter by inverting your program logic. Use subcommands or an option-argument to select a value from a list.
Optional option-arguments Indicates an overloaded option. Can often be split into two options, one that requires an option-argument and another that does not.
Multiple option-arguments The standard approach is to use comma- or whitespace-separated values inside a single (quoted or escaped) option-argument. Such values can even accept arguments of their own by attaching them with an equals sign.

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

helptext-1.0.0.tar.gz (5.9 kB view details)

Uploaded Source

Built Distribution

helptext-1.0.0-py3-none-any.whl (5.5 kB view details)

Uploaded Python 3

File details

Details for the file helptext-1.0.0.tar.gz.

File metadata

  • Download URL: helptext-1.0.0.tar.gz
  • Upload date:
  • Size: 5.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for helptext-1.0.0.tar.gz
Algorithm Hash digest
SHA256 1311974f04af0dd9cc70b1bc27e07453a1454227d7cf3b59d939fcf664ece231
MD5 c21aa99db6eb1011c6a32f929eeeb8e8
BLAKE2b-256 7cb30e9d9f2b93b354516399b2ebc2f0578391a41871c09aacae6dbe5c3e900f

See more details on using hashes here.

File details

Details for the file helptext-1.0.0-py3-none-any.whl.

File metadata

  • Download URL: helptext-1.0.0-py3-none-any.whl
  • Upload date:
  • Size: 5.5 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.0.0 CPython/3.12.2

File hashes

Hashes for helptext-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 5122a302da20deefb1392d32d2cbaf474138899bfa5dfa2e8de08497a3dd2b1d
MD5 6e74c009eec6d2edcad1ca4a7be80d0a
BLAKE2b-256 630c2832df14aa225d009cbb513ee0314596e5562ef45b731a0cf33b818e2722

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