feedback on function introspection in argparse

Carl Banks pavlovevidence at gmail.com
Sat Nov 7 18:33:40 EST 2009


On Nov 7, 2:45 pm, Yuv <ubershme... at gmail.com> wrote:
> This was posted to the argparse mailing list by Steven Bethard and now
> we'd like some feedback from comp.lang.python.
>
> We now have a branch[5] of argparse that supports an ``argparse.run``
> function[6] which does
> some function introspection to build a command line parser from a
> function definition:
>
> ------------------------------ prog.py ------------------------------
> import argparse
>
> def func(foo, bar, baz):
>    """A function that foo's a bar with a baz.
>    foo - The foo
>    bar - The bar to be foo'd
>    baz - The baz with which to foo.
>    """
>    print foo, bar, baz
>
> if __name__ == '__main__':
>    argparse.run(func)
> ------------------------------ cmdline ------------------------------
> $ prog.py -h
> usage: prog.py [-h] foo bar baz
>
> A function that foo's a bar with a baz.
>
> positional arguments:
>  foo         The foo
>  bar         The bar to be foo'd
>  baz         The baz with which to foo.
>
> optional arguments:
>  -h, --help  show this help message and exit
> ----------------------------------------------------------------------
>
> I'd love to hear some feedback on this. At the moment, the code can
> introspect argument names, types from defaults, types from annotations
> (in Python 3), help messages from docstrings, and knows how to convert
> multiple functions into subcommands. The code's compatible and tested
> on python 2.3 - 3.1. There are probably more things we could support
> [7], but I'd like to get some feedback on what we have so
> far. Some specific questions:
>
> * Do you think this is worth including in argparse?
> * Would you use the current ``argparse.run`` API in your own code?
> * If you wouldn't use it as-is, what additional features/modifications
> would you require?

Looks quite useful.

I am not sure I like the name "run", seems to short and innocent for a
function this magical.

Is the docstring expected to be formatted according to some
convention?  I don't recognize a docstring convention in the example,
but then I don't bother with them much in my own code, that's why I
ask.


Carl Banks



More information about the Python-list mailing list