0

I'm trying a very simple python script with only positional arguments, handled by docopt.

#!/usr/bin/env python

opt_spec = """Test

Usage: docopt_test (import | export <output_file> <output_format>)
       docopt_test (-h | --help)
       docopt_test (-v | --version)

Options:
  -h --help     Show this screen.
  -v --version     Show version.

"""

from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(opt_spec, version='Test 1.0')
    print(arguments)

When run it will print:

./docopt_test.py export file.xml xml
{'--help': False,
 '--version': False,
 '<output_file>': 'file.xml',
 '<output_format>': 'xml',
 'export': True,
 'import': False}

The problem is that the output_file and output_format arguments retain the < and > delimiters in the name, making calls like args['output_file'] impossible. Removing the delimiters from the usage string changes the semantics, making the options into keywords.

Is there a way to solve this without having to resort to usage like args['<output_file>']?

André Fernandes
  • 2,335
  • 3
  • 25
  • 33

1 Answers1

0

I think that's considered part of dispatching, which may have all kinds of language-specific implications. There's an experimental project for docopt-dispatch, which seems to handle it nicely with Python annotations. I have a program called snippets where I remap the argument names within my own style of dispatch but I do so by calling args just the way you did.

ian.buchanan
  • 314
  • 1
  • 10