pythoncommand-line-interfacedocopt

Multiple positional and optional arguments using docopt


I'm trying to implement a python3 CLI using the docopt package. I'm trying to get my program accept multiple positional input files and, optionally a list of output files. A MWE for my docstring is:

__doc__ = """
    Usage:
        test.py [INPUT...] [-o OUTPUT...] [-t TEST]

    Options:
        -o OUTPUT..., --output OUTPUT...  #One output file for each INPUT file [default: DEFAULT]
        -t TEST, --test TEST              #A test option
"""

For example a programm call as

test.py FILE_A FILE_B -o OUTFILE_A OUTFILE B -t true

Should return a dict:

{'--output': ['OUTFILE_A', 'OUTFILE_B'],
 '--test': 'true',
 'INPUT': ['FILE_A', 'FILE_B']}

but for some reason it is always appended to the INPUT arguments:

{'--output': ['OUTFILE_A'],
 '--test': 'true',
 'INPUT': ['FILE_A', 'FILE_B', 'OUTFILE_B']}

Solution

  • Options in Docopt unfortunately can only take one argument, so [-o OUTPUT...] will not work. The remaining elements will as you state be interpreted as additional arguments.

    One way around this is to move the ellipsis outside the square brackets:

    Usage:
        test.py [INPUT...] [-o OUTPUT]... [-t TEST]
    

    and use it like this:

    test.py FILE_A FILE_B -o OUTFILE_A -o OUTFILE_B
    

    It doesn't look as good, but it works.