[Python-ideas] Pyopt - an attempt at a pythonic optparse

RunThePun ubershmekel at gmail.com
Sun Sep 6 01:49:43 CEST 2009


I put some sweat into this one so I was hoping to see if you guys like
it or have any ideas for improvement.
http://code.google.com/p/pyopt/

Currently there are 2 modes of operation,
1. keyword command-line functions which create switches (-m, -r etc).
2. positional command-line functions which simply translate positional
arguments from the command-line to a function.
possibly in the future I'll implement a mixed keyword/positional
arguments behaviour.

At the moment annotations are mandatory for explicitness and here's an
example usage:

from pyopt import CmdPos
from pyopt import parse_cmds

@CmdPos
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__":
    parse_cmds()

Notice 4 things:
  * an import
  * a decorator
  * a parse_cmds()
  * type-annotations for casting
The following functionality is exposed:

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 13
2 arguments required, got only 1. Run with ? or -h for more help.



More information about the Python-ideas mailing list