github
Advanced Search
  • Home
  • Pricing and Signup
  • Explore GitHub
  • Blog
  • Login

buchuki / opterator

  • Admin
  • Watch Unwatch
  • Fork
  • Your Fork
  • Pull Request
  • Download Source
    • 3
    • 1
  • Source
  • Commits
  • Network (1)
  • Issues (2)
  • Downloads (2)
  • Wiki (1)
  • Graphs
  • Branch: master

click here to add a description

click here to add a homepage

  • Branches (1)
    • master ✓
  • Tags (0)
Sending Request…
Enable Donations

Pledgie Donations

Once activated, we'll place the following badge in your repository's detail box:
Pledgie_example
This service is courtesy of Pledgie.

generate OptionParser options from the main method signature — Read more

  cancel

  cancel
  • Private
  • Read-Only
  • HTTP Read-Only

This URL has Read+Write access

The action is not necessary in the @param line; THIS BREAKS BACKWARDS 
COMPATIBILITY. It is possible to intuit the action from the type of the 
kwarg default value. Closes #3 
Dusty Phillips (author)
Sun Jun 21 09:48:31 -0700 2009
commit  55bd44baa0cec081b907b4905e8be2a0955978b4
tree    2b4ea4db2a69529e9d1965c4177c32f9dd5cd86e
parent  a21b8651fb2460db8d42b5a225f7a9e0ca30b0e9
opterator /
name age
history
message
file LICENSE Loading commit data...
file MANIFEST.in
file README
directory examples/
file ez_setup.py
file opterator.py
file opterator_test.py
file setup.py
README
Opterator is an option parsing script for Python that takes the boilerplate out
of option parsing. It currently wraps optparse.OptionParser, and thus behaves
similarly, but in the future, it may take care of the parsing on its own to
allow more freedom in specifying options.

Opterator is based on an idea that a main() function for a script can be
decorated to allow command-line arguments to be translated into method
parameters. This allows it to be self-documenting, and reduces errors in
creating and assigning options.

For example, an optparse program for renaming a file might look like this:


>>> from optparse import OptionParser
>>> def main():
...     '''main entrypoint for renaming files. Accept two options, backup
...     and interactive'''
...     parser = OptionParser(usage="A script for renaming files")
...     parser.add_option('-b', '--backup', action=store_true,
...         help='backup the file')
...     parser.add_option('-i', '--interactive', action=store_true,
...         help='interactively move files')
...     # Move the file
...     
>>> if __name__ == '__main__':
...     main()


The equivalent code using opterator looks like this:


>>> from opterator import opterate
>>> @opterate
... def main(source, dest, backup=False, interactive=False):
...     '''A script for renaming files
...     @param backup -b --backup backup the file
...     @param interactive -i --interactive interatively
...     move files...     '''
...     # Move the file
...  
>>> if __name__ == '__main__':
...     main()

Opterator automatically generates help messages from the docstring. If your
main function looks like this:


@opterate
def main(filename1, filename2, recursive=False, backup=False,
        suffix='~', *other_filenames):
    '''An example copy script with some example parameters that might
    be used in a copy command.
    
    @param recursive -r --recursive copy directories
        recursively
    @param backup -b --backup backup any files you copy over
    @param suffix -S --suffix override the usual backup
        suffix '''
    pass



Your help text will look like this:


dusty:opterator $ python copy.py -h
Usage: copy.py [options] filename1 filename2 [other_filenames]

An example copy script with some example parameters that might
    be used in a copy command.
    
Options:
  -h, --help            show this help message and exit
  -r, --recursive       copy directories recursively
  -b, --backup          backup any files you copy over
  -S SUFFIX, --suffix=SUFFIX
                        override the usual backup suffix

That's about it... let me know if you have any questions or suggestions.

<dusty@linux.ca>
Blog | Support | Training | Contact | API | Status | Twitter | Help | Security
© 2010 GitHub Inc. All rights reserved. | Terms of Service | Privacy Policy
Powered by the Dedicated Servers and
Cloud Computing of Rackspace Hosting®
Dedicated Server