Defining argument group for help and version

13 views
Skip to first unread message

Steve

unread,
Dec 2, 2011, 4:32:41 PM12/2/11
to argparse-users
Hello,

Sometimes its helpful to separate the help and version options (which
provide script information) from those used in running the script
normally.

I can create a custom argument group that includes the help and
version options as follows:

p = argparse.ArgumentParser(description=script_description,
add_help=False)
gh = p.add_argument_group('program help')
gh.add_argument('-h', '--help', action='help', help='show this help
message and exit')
gh.add_argument('--version', action='version', version=script_version)

But the docs (the python2.7 docs at least) did not show the
action='help' possibility, so I wanted to confirm it was supported/
official. Also, if this is indeed a recommended way to do what I was
trying to do, it seems that the default help text for the help option
is not included (I needed to supply it as illustrated above).

Steven Bethard

unread,
Dec 15, 2011, 8:40:22 AM12/15/11
to argpars...@googlegroups.com
On Fri, Dec 2, 2011 at 10:32 PM, Steve <steven.ro...@gmail.com> wrote:
> Sometimes its helpful to separate the help and version options (which
> provide script information) from those used in running the script
> normally.
>
> I can create a custom argument group that includes the help and
> version options as follows:
>
> p = argparse.ArgumentParser(description=script_description,
> add_help=False)
> gh = p.add_argument_group('program help')
> gh.add_argument('-h', '--help', action='help', help='show this help
> message and exit')
> gh.add_argument('--version', action='version', version=script_version)
>
> But the docs (the python2.7 docs at least) did not show the
> action='help' possibility, so I wanted to confirm it was supported/
> official.

Yes, it's supported - the fact that it's not documented is a bug:
http://bugs.python.org/issue10772

> Also, if this is indeed a recommended way to do what I was
> trying to do, it seems that the default help text for the help option
> is not included (I needed to supply it as illustrated above).

Yeah, at the moment, you have to specify it again. Feel free to file a
feature request at bugs.python.org - it should be easy to set the
default help for _HelpAction to _('show this help message and exit')
and that's probably what most people would want.

Steve
--
Where did you get that preposterous hypothesis?
Did Steve tell you that?
        --- The Hiphopopotamus

Reply all
Reply to author
Forward
0 new messages