Skip to main content

Effortless argument parser

Project description

GitHub PyPI conda-forge
Read the Docs Build

defopt is a lightweight, no-effort argument parser.

defopt will:

  • Allow functions to be run from code and the command line without modification.

  • Reward you for documenting your functions.

  • Save you from writing, testing and maintaining argument parsing code.

defopt will not:

  • Modify your functions in any way.

  • Allow you to build highly complex or customized command line tools.

If you want total control over how your command line looks or behaves, try docopt, click or argh. If you just want to write Python code and leave the command line interface up to someone else, defopt is for you.

Usage

Once you have written and documented your function, simply pass it to defopt.run() and you’re done.

import defopt

# Use type hints:
def main(greeting: str, *, count: int = 1):
    """
    Display a friendly greeting.

    :param greeting: Greeting to display
    :param count: Number of times to display the greeting
    """
    for _ in range(count):
        print(greeting)

# ... or document parameter types in the docstring:
def main(greeting, *, count=1):
    """
    Display a friendly greeting.

    :param str greeting: Greeting to display
    :param int count: Number of times to display the greeting
    """
    for _ in range(count):
        print(greeting)

if __name__ == '__main__':
    defopt.run(main)

Descriptions of the parameters and the function itself are used to build an informative help message.

$ python test.py -h
usage: test.py [-h] [-c COUNT] greeting

Display a friendly greeting.

positional arguments:
  greeting              Greeting to display

optional arguments:
  -h, --help            show this help message and exit
  -c COUNT, --count COUNT
                        Number of times to display the greeting
                        (default: 1)

Your function can now be called identically from Python and the command line.

>>> from test import main
>>> main('hello!', count=2)
hello!
hello!
$ python test.py hello! --count 2
hello!
hello!

Philosopy

defopt was developed with the following guiding principles in mind:

  1. The interface can be fully understood in seconds. If it took any longer, your time would be better spent learning a more flexible tool.

  2. Anything you learn applies to the existing ecosystem. The exact same docstrings used by defopt are also used by Sphinx’s autodoc extension to generate documentation, and by your IDE to do type checking. Chances are you already know everything you need to know to use defopt.

  3. Everything is handled for you. If you’re using defopt, it’s because you don’t want to write any argument parsing code at all. You can trust it to build a logically consistent command line interface to your functions with no configuration required.

  4. Your Python functions are never modified. Type conversions are only ever applied to data originating from the command line. When used in code, duck-typing still works exactly as you expect with no surprises.

Development

For source code, examples, questions, feature requests and bug reports, visit the GitHub repository.

Documentation

Documentation is hosted on Read the Docs.

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

defopt-6.4.0.tar.gz (42.3 kB view details)

Uploaded Source

File details

Details for the file defopt-6.4.0.tar.gz.

File metadata

  • Download URL: defopt-6.4.0.tar.gz
  • Upload date:
  • Size: 42.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.6.0 importlib_metadata/4.11.2 pkginfo/1.8.3 requests/2.27.1 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.10.5

File hashes

Hashes for defopt-6.4.0.tar.gz
Algorithm Hash digest
SHA256 359a56137b4b7dcbc051d2157e6591a09c35c4297cfc00f1ef8dbcd192d19a34
MD5 30905cb7a99af7272e7ebfdd5cd15ad4
BLAKE2b-256 cc3f3baf4e6fed12dac76c0ccb4716fd4235ef0aa5b48f66aab6f970afc7b44e

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