3

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:)

TylerTemp
  • 970
  • 1
  • 9
  • 9
  • argparse ... or optparse ... or do it manually ... – Joran Beasley Jun 27 '14 at 17:03
  • I'm not clear on what the problem is. You're looking for a way to do it? you've tried and failed? argparse not producing the results you want? – Korem Jun 27 '14 at 17:24
  • 2
    @JoranBeasley Show us something, it sounds so easy. Realize, that handling an argument with expected multiplicity followed by other arguments is not really trivial. – Jan Vlcinsky Jun 27 '14 at 19:41

2 Answers2

2

Currently not supported

You are not the only one dreaming of such feature, see docopt issue #190 Repeating positional arguments followed by a single positional argument

Ambiguity of repeating argument followed by an option

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?

Proposed alternatives (changing command line design)

I will assume, you prefer to place target directory to the end to make it intuitive to user.

Repeated options with values

Usage:
    cp.py  (-s <source>)... -t <directory>

This allows one target directory and multiple source.

Put repeated argument as last one

Usage:
    cp.py <directory> <source>...

this breaks the preference of target being as last one, but is quite easy.

Conclusions

  • Current 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.
  • using repeated argument followed by options is always tricky, try to avoid that.
  • Options are to be optional, so using options, which are required is contradicting this rule for easy to use command line programs.
  • Currently, my preference would be using target argument as first positional one followed by repeated source positional arguments.
  • It would be nice, if docopt would allow multiple positional argument followed by fixed set of positional arguments, but this is currently not implemented.
Jan Vlcinsky
  • 42,725
  • 12
  • 101
  • 98
0

Arriving at a similar need and reviewing the question many years later, I'm seeing that the issue referred by the accepted response is still open.

However, I've found out that now there's an alternative library docpie which implements the same principle of parsing the docstring for the cli, yet supports cp syntax. Due to its extreme similarity (and subsequently relative compatibility) to docopt, here's an explicit list of differences.

mapto
  • 605
  • 9
  • 23