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:
>>> 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
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
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