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(optlist)
return docopt(downpost.__doc__, argv=argstr)
The code I am writing unit tests for has 2 tests that are separately given the following arguments:
-t <title> # <title> can be 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 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?