Search code examples
pythondocopt

Options with multiple arguments


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.


Solution

  • 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']}