I want my script accepts command line args like "cp" command does:
'''
Usage:
cp.py <source>... <directory>
cp.py -t <directory> <source>...
cp.py -s <source>... -t <directory>
'''
Those command line
$ python cp.py src/path/1 src/path/2 target/path
$ python cp.py -t target/path src/path/1 src/path/2
$ python cp.py -s src/path/1 src/path/2 -t target/path
will get the same result:
{'<source>':['src/path/1', 'src/path/2'],'<directory>': 'target/path'}
Thx very much. And sorry for my English:)
You are not the only one dreaming of such feature, see docopt issue #190 Repeating positional arguments followed by a single positional argument
Options following repeating positional argument make parsing ambiguous. Imagine a file, having the same name as command option - how you would specify it and what would you expect as result?
I will assume, you prefer to place target directory to the end to make it intuitive to user.
Usage:
cp.py (-s <source>)... -t <directory>
This allows one target directory and multiple source.
Usage:
cp.py <directory> <source>...
this breaks the preference of target being as last one, but is quite easy.
docopt
does not currently support the style, cp
is using. One reason is that it is not easy, another that cp
is sometime too complex and even ambiguous.docopt
would allow multiple positional argument followed by fixed set of positional arguments, but this is currently not implemented.