Skip to main content
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.

Release files for defopt 7.0.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for defopt 7.0.0
File Size Uploaded
defopt-7.0.0.tar.gz 44.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for defopt 7.0.0
File Interpreter ABI Platform
defopt-7.0.0-py3-none-any.whl Python 3 none any Details

Total release size: 62.3 kB

Release files / defopt-7.0.0.tar.gz

Download URL defopt-7.0.0.tar.gz
Size 44.5 kB
Tags Source
SHA-256 checksum
How to use checksums
d7ac98810005880717e1df62527fd35dfe083f551b6d4fb5da0b25f608e8ef4c
BLAKE2b-256 checksum
How to use checksums
3a5389e5eaa8e775cef5b66254365f51c389ca167b0fa96713b722824708b720
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release files / defopt-7.0.0-py3-none-any.whl

Download URL defopt-7.0.0-py3-none-any.whl
Size 17.8 kB
Tags Python 3
SHA-256 checksum
How to use checksums
88b6747561d803e3fc020b9080fd0aa6a300927f64167c76d95544495f1a8307
BLAKE2b-256 checksum
How to use checksums
ecbe03e94203daf8653bb402936820ac762064b4ef6079daab47581506c187c7
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.1.0 CPython/3.13.0

Release history Release notifications | RSS feed

This release

7.0.0 This release

2 release files

6.4.0

1 release file

6.3.0

1 release file

6.2.0

1 release file

6.1.0

1 release file

6.0.2

1 release file

6.0.1

1 release file

6.0

1 release file

5.1.0

2 release files

5.0.0

2 release files

4.0.1

2 release files

4.0.0

2 release files

3.2.0

2 release files

3.1.1

2 release files

3.0.0

2 release files

2.0.1

2 release files

2.0.0

2 release files

1.3.0

2 release files

1.2.0

2 release files

1.1.0

2 release files

1.0.1

2 release files

1.0.0

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page