how to tell parse_known_args to not parse all known args

207 views
Skip to first unread message

Giovanni Luca Ciampaglia

unread,
Mar 27, 2011, 6:11:10 PM3/27/11
to argparse-users
Hi all,
I guess my question is a bit lame, but here's the problem:

I have a bunch submodules of a package that work as standalone
scripts, each creating its own ArgumentParser. Now, I also have a
frontend script that takes as input the name of a script. The frontend
parses the command line with parse_known_args, imports the requested
script as a module, creates its parser and passes to it the rest of
the command line.

It all works great, except for -h/--help, which is captured by the
frontend's parser. Is there a way to have ArgumentParser print the
frontend's help when I type

frontend -h

and to print the help of blah.py when I type

frontend blah -h

I have worked out a fix to this by passing add_help=False to the
frontend's parser and then checking explicitly if the user request a
fake 'help' script

frontend help

but it's a bit ugly. Any idea?

Giovanni Luca Ciampaglia

unread,
Mar 30, 2011, 7:31:18 AM3/30/11
to argparse-users
Is there anybody out there? ;-)

G

On Mar 28, 12:11 am, Giovanni Luca Ciampaglia

Steven Bethard

unread,
Mar 30, 2011, 8:15:46 AM3/30/11
to argpars...@googlegroups.com, Giovanni Luca Ciampaglia
On Mon, Mar 28, 2011 at 12:11 AM, Giovanni Luca Ciampaglia
<junkie....@gmail.com> wrote:
> I have a bunch submodules of a package that work as standalone
> scripts, each creating its own ArgumentParser. Now, I also have a
> frontend script that takes as input the name of a script. The frontend
> parses the command line with parse_known_args, imports the requested
> script as a module, creates its parser and passes to it the rest of
> the command line.
>
> It all works great, except for -h/--help, which is captured by the
> frontend's parser. Is there a way to have ArgumentParser print the
> frontend's help when I type
>
> frontend -h
>
> and to print the help of blah.py when I type
>
> frontend blah -h

There's an undocumented feature, nargs=argparse.REMAINDER, that you could use:

parser = argparse.ArgumentParser()
parser.add_argument('command')
parser.add_argument('rest', nargs=argparse.REMAINDER)

That should result in:

"frontend -h" -> calls top level parser help
"frontent blah -h" -> results in Namespace(command=blah, rest=['-h'])

This feature is not a public API at the moment, but it could become
one if someone had the time to write some tests for it and document it
appropriately.

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

Giovanni Luca Ciampaglia

unread,
Mar 31, 2011, 6:08:37 AM3/31/11
to argpars...@googlegroups.com, Steven Bethard
On 30/03/2011 14:15, Steven Bethard wrote:
> There's an undocumented feature, nargs=argparse.REMAINDER, that you could use:
>
> parser = argparse.ArgumentParser()
> parser.add_argument('command')
> parser.add_argument('rest', nargs=argparse.REMAINDER)
>
> That should result in:
>
> "frontend -h" -> calls top level parser help
> "frontent blah -h" -> results in Namespace(command=blah, rest=['-h'])

That's exactly what I needed, thanks!

> This feature is not a public API at the moment, but it could become
> one if someone had the time to write some tests for it and document it
> appropriately.

Will give a look at the coding style of the tests and if I can get to
understand how to write them I'll try to come up with something. In the
end argparse gave such a boost to my productivity I can well spend some
time giving something back :-)

Cheers

G

Reply all
Reply to author
Forward
0 new messages