argparse for Interactive Shell

127 views
Skip to first unread message

Jay

unread,
Sep 24, 2010, 12:10:41 AM9/24/10
to argparse-users
I want to create a custom interactive shell where I continually do
parse_args. Like the following:

parser = argparse.ArgumentParser()

command = raw_input()
while(True):
args = parser.parse_args(shlex.split(command))
# Do some magic stuff
command = raw_input()

The problem is that if I give it invalid input, it errors and exits
with a help message. Is there a way to modify this behaviour?

Steven Bethard

unread,
Sep 24, 2010, 6:16:32 AM9/24/10
to argpars...@googlegroups.com

If you just want to stop it from exiting (but still want the error
messages), override the exit method:

class MyParser(ArgumentParser):
def exit(self, status=0, message=None):
# do whatever you want here

This approach should probably be described in the argparse
documentation. Feel free to file a documentation feature request on
bugs.python.org.

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

Jay

unread,
Sep 24, 2010, 10:33:04 AM9/24/10
to argparse-users
Thanks for the info! I've filed a documentation feature request
http://bugs.python.org/issue9938 as you suggested.
Reply all
Reply to author
Forward
0 new messages