単純な Python プログラムでdocoptを使用しています。
#!/usr/bin/env python
"""
Farmers market
Usage:
farmersmarket.py buy -i <item> -q <quantity> [<quantity>] [-p <price>] [-dvh]
farmersmarket.py -d | --debug
farmersmarket.py -v | --version
farmersmarket.py -h | --help
Options:
-i --item Item.
-q --quantity Quantity.
-p --price Price.
-d --debug Show debug messages.
-h --help Show this screen.
-v --version Show version.
"""
from docopt import docopt
print docopt(__doc__)
私が実行した場合:
farmersmarket.py buy --item eggs --quantity 100 115 --price 0.25
The expected behaviour is to buy a random quantity of eggs between the values 100 and 115 at the price 0.25. This works without problems at least when it comes to interpreting the arguments. In other words docopt gets everything as intended:
{'--debug': False,
'--help': False,
'--item': True,
'--price': True,
'--quantity': True,
'--version': False,
'<item>': 'eggs',
'<price>': '0.25',
'<quantity>': ['100', '115'],
'buy': True}
However sometimes I do not want to buy a random amount of eggs but a specific amount. In this case the --quantity
option takes only one argument:
farmersmarket.py buy --item eggs --quantity 471 --price 0.25
But this fails as docopt interprets --price 0.25
as a repeating element of --quantity
and loses the value of <price>
:
{'--debug': False,
'--help': False,
'--item': True,
'--price': True,
'--quantity': True,
'--version': False,
'<item>': 'eggs',
'<price>': None,
'<quantity>': ['471', '0.25'],
'buy': True}
How can I get other options to work after repeating elements?