doctest
designed to run python code, so you need to do something. If you decide to test the command line interface directly through doctest
, one possibility is to do the regular expression __doc__
before __doc__
before passing it to argparse
to take out the os.popen
wrapper:
clean = re.sub(r"^>>> os\.popen\('(.*)'\).*", r"% \1", __doc__) p = ArgumentParser(description=clean, ...)
(Of course, there are all sorts of nicer ways to do this, depending on what you think is "enjoyable.")
This will clear it for the end user. If you also want it to look cleaner in the source, you can go the other way: Put the command line examples in docstring and do not use doctest.testmodule (). Run docstring via doctest.script_from_examples
and follow its post to insert os
calls. (Then you have to inject it into something so that you can check it with run_docstring_examples
.) doctest
doesn't care if the input is valid python, so you can do the following:
>>> print doctest.script_from_examples(""" Here is a commandline example I want converted: >>> add -n 3 4 7 """)
This will still output the python >>>
prompt in the help. If this bothers you, you just need to process the string in both directions.
source share