python - argparse fails when called from unittest test -
in file (say parser.py
) have:
import argparse def parse_cmdline(cmdline=none): parser = argparse.argumentparser() parser.add_argument('--first-param',help="does foo.") parser.add_argument('--second-param',help="does bar.") if cmdline not none: args = parser.parse_args(cmdline) else: args = parser.parse_args() return vars(args) if __name__=='__main__': print parse_cmdline()
sure enough, when called command line works , give me pretty expect:
$ ./parser.py --first-param 123 --second-param 456 {'first_param': '123', 'second_param': '456'}
but want unittest
it, write test_parser.py
file:
import unittest parser import parse_cmdline class testparser(unittest.testcase): def test_parse_cmdline(self): parsed = parse_cmdline("--first-param 123 --second-param 456") self.assertequal(parsed['first_param'],'123') self.assertequal(parsed['second_param'],'456') if __name__ == '__main__': unittest.main()
then following error:
usage: test_parser.py [-h] [--first-param first_param] [--second-param second_param] test_parser.py: error: unrecognized arguments: - - f r s t - p r m 1 2 3 - - s e c o n d - p r m 4 5 6 e ====================================================================== error: test_parse_cmdline (__main__.testparser) ---------------------------------------------------------------------- traceback (most recent call last): file "./test_parser.py", line 8, in test_parse_cmdline parsed = parse_cmdline("--first-param 123 --second-param 456") file "/home/renan/test_argparse/parser.py", line 12, in parse_cmdline args = parser.parse_args(cmdline) file "/usr/lib/python2.7/argparse.py", line 1691, in parse_args self.error(msg % ' '.join(argv)) file "/usr/lib/python2.7/argparse.py", line 2361, in error self.exit(2, _('%s: error: %s\n') % (self.prog, message)) file "/usr/lib/python2.7/argparse.py", line 2349, in exit _sys.exit(status) systemexit: 2 ---------------------------------------------------------------------- ran 1 test in 0.004s failed (errors=1)
as can seen, command line specified (--first-param 123 --second-param 456
) became - - f r s t - p r m 1 2 3 - - s e c o n d - p r m 4 5 6
(each character separated space).
i don't understand why: doing wrong?
argparse
wants "argument vector"—that is, list of separate arguments—not "command line" string.
and calling split
won't solve things. example, command line shell:
python script.py --first-param '123 456' --second-param 789
… correctly give 123 456
, 789
, line of code:
parse_cmdline("--first-param '123 456' --second-param 789")
will not; give '123
, 789
, 456'
doesn't know how deal with.
in fact, wrong:
parse_cmdline("--first-param '123' --second-param 789")
… because you'll '123'
instead of 123
.
there 2 ways deal this.
first, if know you're trying pass, can pass list instead of string in first place, , don't need worry fiddly quoting , splitting details:
parse_cmdline(["--first-param", "123 456", "--second-param", "789"])
alternatively, if don't know you're trying pass, know looks on shell, can use shlex
:
if cmdline not none: args = parser.parse_args(shlex.split(cmdline))
… , python split command line same way standard unix shell, getting 123 456
single parameter.
Comments
Post a Comment