argparse.ArgumentParser("blah")

Peter Otten __peter__ at web.de
Sun Oct 23 14:06:14 EDT 2011


Roy Smith wrote:
> As I read the docs (http://tinyurl.com/3ww9scr), the following two calls
> should result in the same object:
>> parser = argparse.ArgumentParser(description="blah")
> parser = argparse.ArgumentParser("blah")
>> In the first case, I'm explicitly setting description to "blah", in the
> second case, I'm passing "blah" as a positional parameter so it should
> be taken as first parameter to the function, which happens to be
> description. Either way should end up the same.
>> I don't, however, get the same behavior when run with "-h". The first
> gives what I would expect:
>> ------------------------------------------------
> usage: arg.py [-h]
>> blah
>> optional arguments:
> -h, --help show this help message and exit
> ------------------------------------------------
>> and the second gives:
>> ------------------------------------------------
> usage: blah [-h]
>> optional arguments:
> -h, --help show this help message and exit
> ------------------------------------------------
>> Is this a bug, or am I just not reading the docs right?

A quick look into the source code reveals the the documentation is not 
consistent with the actual implementation:
http://hg.python.org/cpython/file/eef1027ab2f0/Lib/argparse.py#l1581
"""
class ArgumentParser(_AttributeHolder, _ActionsContainer):
[...]
 def __init__(self,
 prog=None,
 usage=None,
 description=None,
 epilog=None,
 version=None,
 parents=[],
 formatter_class=HelpFormatter,
 prefix_chars='-',
 fromfile_prefix_chars=None,
 argument_default=None,
 conflict_handler='error',
 add_help=True):
"""
I suggest that you file a bug report.
PS: My guess is that you are supposed to use keyword parameters only, but 
that the implementation doesn't enforce that (yet?). 


More information about the Python-list mailing list

AltStyle によって変換されたページ (->オリジナル) /