3

Looking through the docopt documentation and examples I can't seem to find this functionality, but I feel as it should exist so I thought I'd ask to make sure.

I'm using docopt for Python and want to be able to allow arbitrary options. The use case is a command line templating utility - so arbitrary key values would be handy.

"""Templator

Usage:
    templator <template> [--arbitrary-option=<value>]...
"""

Hope that example demonstratives what I'm after. Maybe something like --*=<value> would be an alternative way of writing it.

EDIT:

Here's my current solution which takes key values pairs. However, these are seperated by spaces so could be hard to figure out what are keys and values for long statements.

templator <template> (<key> <value>)...

Then in the python script (for anyone interested in this solution)

arguments = docopt(__doc_)
arbitrary_kwargs = dict(zip(arguments['<key>'], arguments['<value>']))

Having key=value syntax would be ideal - if that's possible.

freebie
  • 2,161
  • 2
  • 19
  • 36

1 Answers1

5

Perhaps you could use

"""Templator

Usage:
    templator <template> [-c <name>=<value>]...
"""

which is what docopt/examples/git/git.py does.

Here is a test example:

bash-3.2$ cat test.py
""" Usage:
    Templator [-c <name>=<value>]...
"""
import sys, docopt
args = docopt.docopt(__doc__, version='0.0.1-alpha')
print str(args)

bash-3.2$ python test.py
{'-c': 0,
 '<name>=<value>': []}

bash-3.2$ python test.py -c foo=1
{'-c': 1,
 '<name>=<value>': ['foo=1']}

bash-3.2$ python test.py -c foo=1 -c baz=2
{'-c': 2,
 '<name>=<value>': ['foo=1', 'baz=2']}
jq170727
  • 13,159
  • 3
  • 46
  • 56