Skip to content

Commit

Permalink
allow automatic genertaion of option names.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dusty Phillips committed Dec 2, 2012
1 parent 8d757ca commit 29138d2
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 7 deletions.
24 changes: 17 additions & 7 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ For example, an optparse program for renaming a file might look like this:
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,
parser.add_option('-p', '--interactive', action=store_true,
help='interactively move files')
# Move the file
Expand All @@ -38,15 +38,23 @@ The equivalent code using opterator looks like this:
@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
:param backup: backup the file
:param interactive: -p --interactive interatively
move files... '''
# Move the file
if __name__ == '__main__':
main()
Opterator automatically generates help messages from the docstring. If your
Opterator automatically generates help messages from the docstring. The main part
of the docstring becomes the main part of the help string. The individual
parameter docstrings become the helptext for the arguments. By default, the
long and short form of a given parameter come from the parameter name and the
first character of the parameter name. You can replace either or both of these
by adding options that begin with a ``-`` character between the parameter and
the helptext.

If your
main function looks like this:

.. code-block:: python
Expand All @@ -57,10 +65,10 @@ main function looks like this:
'''An example copy script with some example parameters that might
be used in a copy command.
@param recursive -r --recursive copy directories
:param recursive: copy directories
recursively
@param backup -b --backup backup any files you copy over
@param suffix -S --suffix override the usual backup
:param backup: -b --backup backup any files you copy over
:param suffix: -S --suffix override the usual backup
suffix '''
pass
Expand All @@ -78,3 +86,5 @@ Your help text will look like this::
-b, --backup backup any files you copy over
-S SUFFIX, --suffix=SUFFIX
override the usual backup suffix

The short and long form of the options are
3 changes: 3 additions & 0 deletions opterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ def opterate(func):
param_args = param.split()
variable_name = param_args.pop(0)[:-1]
option_names.append(variable_name)
if not param_args[0].startswith('-'):
option_strings.append('--' + variable_name)
option_strings.append('-' + variable_name[0])
while param_args[0].startswith('-'):
option_strings.append(param_args.pop(0))
help_text = ' '.join(param_args)
Expand Down
33 changes: 33 additions & 0 deletions test_opterator.py
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,39 @@ def main(myoption='novalue'):
assert result.myoption == 'novalue'


def test_keyword_option_no_identifier():
result = Checker()

@opterate
def main(myoption="novalue"):
'''A script with one optional option.
:param myoption: the myoption helptext'''
result.myoption = myoption
main(['--myoption', 'avalue'])
assert result.myoption == 'avalue'


def test_keyword_option_no_identifier_helptext():
capture = py.io.StdCapture()

@opterate
def main(myoption='novalue'):
'''A script with one optional option.
:param myoption: the myoption helptext'''
print("never called")
py.test.raises(SystemExit, main, ['-h'])
out, error = capture.reset()
assert error == ''
assert out.strip() == """Usage: py.test [options]
A script with one optional option.
Options:
-h, --help show this help message and exit
-m MYOPTION, --myoption=MYOPTION
the myoption helptext"""


def test_keyword_list_option():
result = Checker()

Expand Down

0 comments on commit 29138d2

Please sign in to comment.