3

I'm using docopt to parse command line input in python. I have my docstring:

"""
Usage:
  docoptTest.py [options]

Options:
  -h --help                show this help message and exit
  -n --name <name>         The name of the specified person
"""

Then I import docopt and parse the arguments and print them:

from docopt import docopt
args = docopt(__doc__)
print(args)

>>> python docoptTest.py -n asdf
{'--help': False,
 '--name': 'asdf'}

I tried putting ellipses to allow to input more than one name:

-n --name <name>...      The name of the specified person

But I got a usage error. Then I put the ellipses in the initial usage message:

"""
Usage:
  docoptTest.py [-n | --name <name>...] [options]

Options:
  -h --help                show this help message and exit
  -n --name                The name of the specified person
"""

But the output thinks that --name is a flag.

>>> python docoptTest.py -n asdf asdf
{'--help': False,
 '--name': True,
 '<name>': ['asdf', 'asdf']}

How do I fix this?

polarbits
  • 187
  • 1
  • 3
  • 10
  • Well, because it _is_ a flag (more precisely, an _option_). And according to POSIX, such option can have only one argument at most, and options can appear in any order in the command line. If you need more than one argument following an option, this is unstandard, and probably can be simulated with positional elements, although I'm not sure if docopt is well suited for that. – SasQ Mar 25 '21 at 12:11

1 Answers1

2

This notation:

>>> python docoptTest.py -n asdf asdf

will probably not work with docopt, as each option takes only one argument. If you want to to do it like this, then you could use some kind of separator, for example a comma, and then split it yourself. The problem arises if you add an argument, then the parser wouldn't be able to distinguish the last asdf as an part of the option or an argument. Some people also put an = between the option and its argument.

Maybe you could try this instead:

Usage:
  docoptTest.py [-n|--name <name>]... [options]

Options:
  -h --help                show this help message and exit
  -n --name <name>         The name of the specified person

This is a quite common way of doing something very similar. The docopt dictionary would look like this:

$python docoptTest.py -n asdf -n ads
{'--help': False,
 '--name': ['asdf', 'ads']}
$python docoptTest.py --name asdf --name ads
{'--help': False,
 '--name': ['asdf', 'ads']}
J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33
  • Thanks. Is this problem something fundamental (ambiguity whether it's an argument or value) or is there something else that I could do with the option descriptions? – polarbits Nov 02 '16 at 19:18
  • There is not so much you can do about it I think. You could split it yourself afterwards. You could make the `"asdf asf"` count as one argument by using `"` around it, and split it yourself. I think that docopt follows the Open Groups specification for utilites: http://pubs.opengroup.org/onlinepubs/007908799/xbd/utilconv.html. This does not describe how to handle double dash options, but those are probably handled according to best practises. – J. P. Petersen Nov 04 '16 at 12:52
  • Double dash "long" options are basically GNU extensions to the POSIX standard. – SasQ Mar 25 '21 at 13:22