Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Parsing arguments that contain spaces with docopt #129

Closed
ghost opened this issue Jul 24, 2013 · 4 comments
Closed

Parsing arguments that contain spaces with docopt #129

ghost opened this issue Jul 24, 2013 · 4 comments

Comments

@ghost
Copy link

ghost commented Jul 24, 2013

(I had originally posted this on StackOverflow, but since I was not getting any responses, I thought I'd try here.)

I am having issues getting docopt to parse arguments that contain spaces into a proper dictionary object for use with my unit tests. Here is the code I'm currently using to construct the argument list for docopt to parse:

testargs = []

def clear_args():
    testargs[:] = []
    return

def add_testfiles(file1='defaultfile1.txt', file2='defaultfile2.txt'):
    clear_args()
    testargs.append('--debug')
    testargs.append(file1)
    testargs.append(file2)
    return

def parse_args(optlist):
    argstr = ' '.join(testargs)
    return docopt(myPythonScript.__doc__, argv=argstr)

The code I am writing unit tests for has 2 tests that are separately given the following arguments:

-t <title>  # <title> is any string (with spaces) inside quotation marks
"A Filename with Spaces.txt"  # any filename as long as it's in quotation marks

To add, for example, the -t <title> argument, I would do:

def test_exampleunittest(self):
    add_testfiles()
    testargs.append('-t "This is the title I want"')
    self.args = parse_args(testargs)
    self.post = myPythonScript.process(self.args)
    self.assertEqual(self.post['Subject'], 'This is the title I want')

If I run the script I'm testing by itself with the said arguments, they are accepted without any problems and the output is as expected.

However, if I run the unit tests which use arguments containing spaces, I get the following:

DocoptExit: Usage: myPythonScript [options] <file_1> <file_2>

Other unit tests that require the same dict object (containing the same arguments) work fine.

What should I change in my code to make docopt parse the arguments as it normally does?

@keleshev
Copy link
Member

docopt takes argv parameter as either a string or a list.

  • If it is a list, it will interpret each item in the list as a separate argument.
  • If it is a string, it will split the string into a list using .split(). This way you loose all whitespace.

So in order for your test to work, you should pass a list instead of joining it into a string argstr = ' '.join(testargs).

This confusion is probably due to the fact that passing string to argv is undocumented. In fact it is not part of API, just an implementation detail. You should not rely on the fact that docopt argv takes a string—this might go away. However docopt will always accept a list for argv.

@keleshev
Copy link
Member

If this answers you question I will copy this answer to StackOverflow. You are always welcome create new issues in order to ask a question, or link StackOverflow questions.

@ghost
Copy link
Author

ghost commented Jul 24, 2013

@halst Passing a list worked! Thanks a lot for answering.

I thought it would be troublesome for anyone if I simply linked my SO question here and provided a short description, so I copied it whole. Feel free to copy the answer so I can mark it accepted.

@ghost ghost closed this as completed Jul 24, 2013
@ghost
Copy link
Author

ghost commented Jul 24, 2013

@halst: I've made a note on my StackOverflow post re: your answer here. If you have time, please copy your response there so I can mark it accepted. Thanks! 👍

This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant