Skip to main content
This is a pre-production deployment of Warehouse. Changes made here affect the production instance of PyPI (pypi.python.org).
Help us improve Python packaging - Donate today!

Effortless argument parser

Project Description

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

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)

Note

On Python 2, where there are no keyword-only arguments, you should pass strict_kwonly=True to defopt.run. Otherwise, count is converted into an optional positional argument.

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 History

Release History

This version
History Node

4.0.1

History Node

4.0.0

History Node

3.2.0

History Node

3.1.1

History Node

3.0.0

History Node

2.0.1

History Node

2.0.0

History Node

1.3.0

History Node

1.2.0

History Node

1.1.0

History Node

1.0.1

History Node

1.0.0

History Node

0.3.1

History Node

0.3.0

History Node

0.2.0

History Node

0.1.0

Download Files

Download Files

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

File Name & Checksum SHA256 Checksum Help Version File Type Upload Date
defopt-4.0.1-py2.py3-none-any.whl (12.4 kB) Copy SHA256 Checksum SHA256 py2.py3 Wheel Nov 26, 2017
defopt-4.0.1.tar.gz (29.0 kB) Copy SHA256 Checksum SHA256 Source Nov 26, 2017

Supported By

WebFaction WebFaction Technical Writing Elastic Elastic Search Pingdom Pingdom Monitoring Dyn Dyn DNS Sentry Sentry Error Logging CloudAMQP CloudAMQP RabbitMQ Heroku Heroku PaaS Kabu Creative Kabu Creative UX & Design Fastly Fastly CDN DigiCert DigiCert EV Certificate Rackspace Rackspace Cloud Servers DreamHost DreamHost Log Hosting