-1

I am trying to write a python CLI program using module python cmd. When I try to execute another python script in my CLI program my objective is I have some python script in other folder and CLI program in other folder. I am trying to execute those python script using CLI program.

Below is the os.popen method used to execute other script there is CLI program:

import cmd
import os
import sys

class demo(cmd.Cmd):

   def do_shell(self,line,args):
     """hare is function to execute the other script"""
    output = os.popen('xterm -hold -e python %s' % args).read()
    output(sys.argv[1])

def do_quit(self,line):

    return True

if __name__ == '__main__':
    demo().cmdloop()

and hare is error:

(Cmd) shell demo-test.py
Traceback (most recent call last):
File "bemo.py", line 18, in <module>
demo().cmdloop()
File "/usr/lib/python2.7/cmd.py", line 142, in cmdloop
stop = self.onecmd(line)
File "/usr/lib/python2.7/cmd.py", line 221, in onecmd
return func(arg)
TypeError: do_shell() takes exactly 3 arguments (2 given)

there is some link to other cmd CLI program 1 = cmd – Create line-oriented command processors 2 = Console built with Cmd object (Python recipe)

and some screen shot's for more information: enter image description here

Please run above code in your system.

Najeeb Choudhary
  • 396
  • 1
  • 3
  • 21

1 Answers1

1

As specified in the doc:

https://pymotw.com/2/cmd/index.html

do_shell is defined as such:

do_shell(self, args):

But you are defining it as

do_shell(self, line, args):

I think the intended use is define it as specified from the documentation.

I ran your code and followed your example. I replicated your error. I then, as specified in the documentation for do_shell, I changed the method to the as expected:

do_shell(self, args):

From there, the sys module was missing, so you need to import that as well (unless you did not copy it from your source). After that, I got an error for index out of range, probably because of the expectation of extra parameters needing to be passed.

Furthermore, because you are talking about Python scripts, I don't see the need for the extra commands you are adding, I simply changed the line to this:

output = os.popen('python %s' % args).read()

However, if there is a particular reason you need the xterm command, then you can probably put that back and it will work for your particular case.

I also, did not see the use case for this:

output(sys.argv[1])

I commented that out. I ran your code, and everything worked. I created a test file that just did a simple print and it ran successfully.

So, the code actually looks like this:

def do_shell(self, args):
    """hare is function to execute the other script"""
    output = os.popen('python %s' % args).read()
    print output

The full code should look like this:

import cmd
import os
import sys

class demo(cmd.Cmd):

    def do_shell(self, args):
        """hare is function to execute the other script"""
        output = os.popen('python %s' % args).read()
        print output

    def do_quit(self,line):

        return True

if __name__ == '__main__':
    demo().cmdloop()
idjaw
  • 25,487
  • 7
  • 64
  • 83