Skip to main content

Exposes your APIs to the command line.

Project description

This is a helper module for making intuitive command line programs with
zero effort. Let's say you have a function that looks like this:
example_function(string1, string2='something', string3='something else')

The simplest way to make it into a script is to do:
if __name__ == "__main__":
import sys
example_function(*sys.argv[1:])

But this fails when someone writes script.py --help on the command line.
("--help" will just be passed as the first argument to example_function)
It also causes pain if you try to do it with a function like this:
example_function2(string1, int1=5, int2=4, float1=0.3)

What commandline allows you to do is (without changing example_function):
if __name__ == "__main__":
import commandline
commandline.run_as_main(example_function)

and it will do option parsing, and also convert all arguments to the same type
as the default arguments (as in example_function2).

Limitations
============

Note that it currently can't print help information for arguments other than their default values,
but it will print your docstring for you if that's of any use.
Help for arguments will probably come with python3000's function annotations.
http://www.python.org/dev/peps/pep-3107/

The code is a bit ugly at the moment.

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

commandline-0.1dev.tar.gz (4.5 kB view hashes)

Uploaded Source

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