1

Is it possible to have multiples options with more than one argument in docopt, without knowing the number of these arguments?

I would like to do something like this with a variable number of arguments:

Usage:
    myprog.py --option1 ARG1 ARG2... --option2 ARG3 ARG4 ARG5...

I tried to use <arg>... but it only works as positional argument.

Thanks for help.

Liad
  • 340
  • 4
  • 15

1 Answers1

2

You can give an option several times, but you have to specify the option each time. For example it could look like this:

"""Example of program with options repeated using docopt.
Usage:
  myprog.py [--options1=OPT]...
"""
from docopt import docopt

if __name__ == '__main__':
    arguments = docopt(__doc__)
    print(arguments)

And the output would look like this:

$ python myprog.py --options1=test1 --options1=test2
{'--options1': ['test1', 'test2']}
J. P. Petersen
  • 4,871
  • 4
  • 33
  • 33
  • Didn't know you could do that, thanks for information! But I asking user to give a list of files, repeating the option will take to much time.. I guess I will use the solution I just found on [another topic](https://stackoverflow.com/questions/21804165/docopt-options-after-repeating-elements-are-interpeted-as-repeating-elements?rq=1) by giving only one argument separated by ','. I'm not sure it's the best options, do you think there is other way? – Liad Jun 08 '17 at 08:14
  • 1
    The solution with using commas as a separator is very common. So I wouldn't worry too much about using it. One problem that might arise is if the options themselves contain commas - then you would need some way of escaping them. For example filenames on some file system can contain commas. – J. P. Petersen Jun 08 '17 at 11:52