2

How do I crate a docopt in python where my argument needs to be a string with spaces. I tried putting single and double quotes around it but it doesn't work.

This is my docopt string so far.

"""
Parser 
Usage: parser.py <trey_file> <als_cmd>

"""

parser.py myfile.py "This is an Orange" #doesn't work

3
  • 1
    What exactly happens, that exact doc string and test gives me {'<als_cmd>': 'This is an Orange', '<trey_file>': 'myfile.py'}. Commented Oct 1, 2014 at 1:00
  • that is a surprise. I tried the same thing on my python script and as well as the docopt site it didn't work at all: try.docopt.org/… Commented Oct 1, 2014 at 1:11
  • Yes, you were right. I tried it on my python script and now it seems to work. Looks like there is a bug in docopt site. Commented Oct 1, 2014 at 15:29

1 Answer 1

2

Docopt does not parse an argument string into an argument vector. Try the following.

"""
Parser
Usage: parser.py <trey_file> <als_cmd>

"""
print docopt(__doc__)
print docopt(__doc__, 'myfile.py "This is an Orange"')
print docopt(__doc__, ['myfile.py', 'This is an Orange'])

Run the script from your shell using python parser.py myfile.py "This is an Orange". The first case should work as expected because the shell splits the arguments into sys.argv as you expect. The second case fails with your problem because docopt expects its input already tokenized. The third case will work as expected.

As an aside, it seems that try.docopt.org makes the same mistake of passing a string instead of a vector to docopt().

Sign up to request clarification or add additional context in comments.

1 Comment

You are right @kalhartt. try.docopt.org is fooling me around. It works fine in my python interpreter!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.