0

I'm using docopt and python 2.7. I want to implement something similar to -- in vim. From man vim:

--      Denotes the end of the options. Arguments after this will be handled as a file 
        name. This can be used to edit a filename that starts with a '-'.

Thankfully, docopt already implements this, but it gives me an extra '--' in my argument list. For example, take this short python script:

"""Usage:
  doc.py FILE [ARGUMENTS ... ]
  doc.py FILE [options] [ARGUMENTS ... ]
Options:
  -h --help     Show this screen.
  -d            Debug mode.
  -f FILE       Open FILE
  -w FILE       Log to FILE
  --safe        Do not allow shell access
"""

from docopt import docopt

def main():
    print(args["ARGUMENTS"])

if __name__ == "__main__":
    args = docopt(__doc__, version="V alpha 0.1")
    main()

If I call this with

python doc.py file.txt -- 1 2 3 -w

I get this:

['--', '1', '2', '3', '-w']

I would expect it to give:

['1', '2', '3', '-w']

What am I doing wrong?

DJMcMayhem
  • 7,285
  • 4
  • 41
  • 61
  • Do you plan to put anything directly after `--`? – cwahls May 28 '16 at 01:00
  • @ClaytonWahlstrom No, I wasn't planning on it. Right now, if I do it tries to interpret the args as options. If that's the only way to get it to work, I'd be fine with it though. – DJMcMayhem May 28 '16 at 01:11

2 Answers2

0

I am no expert, but try making the -- optional for all usages.

doc.py FILE [options] [--] [ARGUMENTS ... ]

You can either enter options, or not and start with arguments. It will look for the first -- if its exists.

Let me know if this works.

cwahls
  • 743
  • 7
  • 22
0

I've tried Wahlstrom's solution, That's not work.
So far as I know, docopt will parse everything into argument list,
There is no syntax or special usage in docopt for your use.

The best solution for now is to wipe out that "--" element before your further program flow.
Or just simply add an if case to handle it.

mJace
  • 49
  • 1
  • 8