[stdlib-sig] Pyopt - command-line options that are alot easier than optparse

Yuvgoog Greenle ubershmekel at gmail.com
Thu Sep 10 02:38:01 CEST 2009


Hi stdlib-sig,
I wanted to ease exposing functions to the command-line using python 3
features and the result is http://code.google.com/p/pyopt/

<http://code.google.com/p/pyopt/>I think this could be good stuff for the
standard library and I'm not sure how to go about it, should I work on
integrating with optparse/optik (is that project still active?) or should
this be a separate module?

This is an example usage of pyopt.0.71:
---
import pyopt
expose = pyopt.Exposer()

@expose.args
def possy(archer:str, boulder:float, magic:int=42):
    """Shows an example positional command-line function.
        archer - is a str
        boulder - should be a float
        magic - a number that is magical"""
    print(repr(archer), repr(boulder), repr(magic))

if __name__ == "__main__":
    expose.run()

---
And this is what you get:

C:\>example.py -h
Usage: example.py archer boulder [magic]
        Shows an example positional command-line function.
        archer - is a str
        boulder - should be a float
        magic - a number that is magical

C:\>example.py 1 2 3
'1' 2.0 3

C:\>example.py 1 2
'1' 2.0 42

C:\>example.py 5
2 arguments required, got only 1. Run with ? or -h for more help.
---

For more examples and functionality (like multiple functions) look at:
http://code.google.com/p/pyopt/wiki/Examples

Notes:
1. I know the names aren't perfect (module pyopt, class Exposer and the
decorator methods args/kwargs/mixed). Please reply if you have better names
in mind.
2. I realize this isn't as flexible as optparse and passing options around
multiple different sub-functions is harder. This module is strictly a quick
and clean solution to a very day-to-day use case: bridging python functions
to command-line.
3. Feedback would be greatly appreciated. I would like to know if I'm the
only one who's tired of sys.argv/optparse boilerplate.

-- 
Yuv
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/stdlib-sig/attachments/20090910/89de5f51/attachment.htm>


More information about the stdlib-sig mailing list