custom python shell (cmd + argparse)

1,063 views
Skip to first unread message

abhishek bhat

unread,
Sep 21, 2011, 9:59:32 PM9/21/11
to argparse-users
I want to make a custom interactive shell. I figured using the cmd
module might be most convenient. basically make a new class inheriting
cmd and then add commands using the do_cmd convention.

Because this is a shell I will override the invalid argument default
exit behavior of argparse so that instead of exiting I get the shell
prompt back. (as has been suggested earlier on this group)

Can I use argparse inside each cmd implementation to parse the options
for that command? I have a small code snippet below of what I am
trying to do.

class DebugShell(cmd.Cmd):
intro = 'Welcome to the Debug shell. Type help or ? to list
commands.\n'
prompt = 'Debug> '


def do_right(self, arg):
parser = argparse.ArgumentParser(version='1.0',
add_help=False)
parser.add_argument('rhs', type = int)
args = parser.parse_args(arg.split())

'Do something with args.rhs'


Also the way it is, the automatic usage message refers to the .py and
not to the cmd (right in the def below). Can that be changed?
(terminal output below)

Debug> right ?
usage: shell.py [-h] [-v] rhs
shell.py: error: argument rhs: invalid int value: '?'

And this is cmd question but if somebody knows can they suggest how to
come up with a more sophisticated shell that is have features like
command completion, etc.

abhishek bhat

unread,
Sep 26, 2011, 1:19:04 PM9/26/11
to argparse-users
Has anyone done something similar before? Any suggestions are welcome.

Steven Bethard

unread,
Dec 15, 2011, 8:12:57 AM12/15/11
to argpars...@googlegroups.com
Sorry about the long lack of response - I'm flying back and forth
between the US and EU these days and haven't had much time for
argparse.

> On Sep 21, 6:59 pm, abhishek bhat <wimpybob...@gmail.com> wrote:
>> Can I use argparse inside each cmd implementation to parse the options
>> for that command?

I've never used the cmd module, so I can't help much here.


>> Also the way it is, the automatic usage message refers to the .py and
>> not to the cmd (right in the def below). Can that be changed?

Yep, just specify ArgumentParser(prog="right")

>> And this is cmd question but if somebody knows can they suggest how to
>> come up with a more sophisticated shell that is have features like
>> command completion, etc.

You might look at what IPython does - they have a pretty sophisticated
shell, and argparse is integrated in there somehow:

http://ipython.org/

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

Josh English

unread,
Nov 16, 2012, 7:06:47 PM11/16/12
to argpars...@googlegroups.com
Months and months later I tried to do this, and made it work, and your question seemed to be the only guidance I could find.


I ended up defining my parsers outside the Cmd object, and I turned off help and I customized the prog value to match the command used in the Cmd object. My take on your code would like this:


rightparser = argparse.ArgumentParser(prog="right", add_help = False)
rightparser.add_argument('rhs', type=int)

class DebugShell(cmd.Cmd): 
    intro = 'Welcome to the Debug shell. Type help or ? to list 
commands.\n' 
    prompt = 'Debug> ' 

    def help_right(self): rightparser.print_help()

    def do_right(self, arg): 
        try:
            result = rightparser.parse_args(arg.split())
        except Excepts as E:
            return None # errors will be described by parse_args

         
The advantage of this is that inside the Cmd, you can use the help command and get the pertinent argparse information.

Thanks for helping me solve my problem.
  
Reply all
Reply to author
Forward
0 new messages