Full help on missing subcommand

3,744 views
Skip to first unread message

Mihai Rusu

unread,
Feb 8, 2010, 5:15:05 PM2/8/10
to argparse-users
Hi

I have defined several subcommands with argparse and when I run the
command without specifying a subcommand I get a short help message
showing the options. Is there a way to tell argparse to print a full
help (as with print_help) when no subcommand has been given?

Steven Bethard

unread,
Feb 9, 2010, 11:47:47 AM2/9/10
to argpars...@googlegroups.com

It already prints out the full help for the top level parser:

>>> parser = argparse.ArgumentParser()
>>> parser.add_argument('--foo', help='foo help')
>>> parser = argparse.ArgumentParser(description='parser help')
>>> parser.add_argument('--foo', help='foo help')
>>> subparsers = parser.add_subparsers()
>>> parser1 = subparsers.add_parser('1')
>>> parser2 = subparsers.add_parser('2')
>>> parser.print_help()
usage: [-h] [--foo FOO] {1,2} ...

parser help

positional arguments:
{1,2}

optional arguments:
-h, --help show this help message and exit
--foo FOO foo help

So I assume by "full help" you mean the help for the subcommands as
well? Currently, there's no direct way to get the help for the
subparsers in the main help, though you might be able to hack
something up by looking at the parser._subparsers attribute. I'm open
to patches that would make this kind of thing easier for folks who
want it.

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

Mihai Rusu

unread,
Feb 9, 2010, 3:18:10 PM2/9/10
to argpars...@googlegroups.com
On Tue, Feb 9, 2010 at 8:47 AM, Steven Bethard <steven....@gmail.com> wrote:
> On Mon, Feb 8, 2010 at 2:15 PM, Mihai Rusu <di...@google.com> wrote:
>> I have defined several subcommands with argparse and when I run the
>> command without specifying a subcommand I get a short help message
>> showing the options. Is there a way to tell argparse to print a full
>> help (as with print_help) when no subcommand has been given?
>
> It already prints out the full help for the top level parser:

When doing -h or --help but not when not giving a subcommand or giving
the wrong subcommand.

>
>>>> parser = argparse.ArgumentParser()
>>>> parser.add_argument('--foo', help='foo help')
>>>> parser = argparse.ArgumentParser(description='parser help')
>>>> parser.add_argument('--foo', help='foo help')
>>>> subparsers = parser.add_subparsers()
>>>> parser1 = subparsers.add_parser('1')
>>>> parser2 = subparsers.add_parser('2')
>>>> parser.print_help()
> usage: [-h] [--foo FOO] {1,2} ...
>
> parser help
>
> positional arguments:
>  {1,2}
>
> optional arguments:
>  -h, --help  show this help message and exit
>  --foo FOO   foo help

The print_help() output is perfect. I just want it printed not only
when using -h or --help but also when not giving a subcommand or
mistyping the subcommand. What I do get instead is:
$ tool
usage: tool [-h] [global options] {subcommand1,subcommand2,subcommand3} ...
tool: error: too few arguments

I would like to get the print_help() output instead of that.

>
> So I assume by "full help" you mean the help for the subcommands as
> well?

No that is fine, I have hacked the subcommand help by adding a "help"
subcommand with an optional positional argument. I'm talking about
something different tho.

Thanks for your reply.

--
Mihai Rusu

Steven Bethard

unread,
Feb 28, 2010, 12:35:35 AM2/28/10
to argpars...@googlegroups.com
On Tue, Feb 9, 2010 at 12:18 PM, Mihai Rusu <di...@google.com> wrote:
> The print_help() output is perfect. I just want it printed not only
> when using -h or --help but also when not giving a subcommand or
> mistyping the subcommand. What I do get instead is:
> $ tool
> usage: tool [-h] [global options] {subcommand1,subcommand2,subcommand3} ...
> tool: error: too few arguments
>
> I would like to get the print_help() output instead of that.

Sorry about the delay. To achieve this, you can override the error() method::


>>> class MyParser(argparse.ArgumentParser):
... def error(self, message):
... sys.stderr.write('error: %s\n' % message)
... self.print_help()
... sys.exit(2)
...
>>> parser = MyParser()
>>> parser.add_argument('foo')
>>> parser.parse_args(['--bar'])
error: too few arguments
usage: [-h] foo

positional arguments:
foo

optional arguments:
-h, --help show this help message and exit

Steve

freeseek

unread,
Jun 26, 2012, 6:07:05 PM6/26/12
to argpars...@googlegroups.com
Another possibility would be to use the following syntax:
>>> try:
...     args=parser.parse_args(sys.argv[1:])
... except:
...     parser.print_help()
...     exit()
...
But there is the caveat that the error message from parse_args will be displayed nevertheless. It would be nice if this could be somewhat suppressed (maybe with a silent variable in parse_args set to False by default).

Reply all
Reply to author
Forward
0 new messages