Reputation: 17
New to python and i'm working on an interactive shell using cmd that takes defined functions and arguments and returns value in an if/then format. Example below. But my issue is that every time the user types a function and i return something to the user, if they just hit enter then they recieve the last provided output.
For example code below...but basically if the user doesn't type an argument or function for that matter, i just want nothing displayed just a "" or empty space. I assumed the else pass argument would have done that for me, but obviously not. I have tried if and elif, with the same results.
Example output with "test arp", just hitting enter returns the previous argument pressed :
user@hostname>test arp
? (192.168.50.1) at 0:26:88:38:c6:48 on en0 ifscope [ethernet]
? (192.168.50.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
> **<enter key>**
? (192.168.50.1) at 0:26:88:38:c6:48 on en0 ifscope [ethernet]
? (192.168.50.255) at ff:ff:ff:ff:ff:ff on en0 ifscope [ethernet]
> **<enter key>**
Example Code: #!/usr/bin/env python
from cmd import Cmd
from time import ctime
import csv, os, subprocess, getpass, socket, pwd, re, select, shutil, subprocess, sys
syntaxError = "Sorry that syntax isn't quite right..."
class cliPrompt(Cmd):
def do_test(self, args):
if len(args) == 0:
print syntaxError
if args == '?':
print 'want to read the help?'
if args == 'arp':
subprocess.call('arp -an', shell=True)
if args == 'cpu':
subprocess.call('grep "model name" /proc/cpuinfo', shell=True)
if args == 'firewall':
subprocess.call('iptables -L -v -n --line-numbers', shell=True)
if args == 'interfaces':
subprocess.call('ifconfig', shell=True)
else:
pass
if __name__ == '__main__':
prompt = cliPrompt()
prompt.prompt = '>'
prompt.cmdloop()
Upvotes: 1
Views: 681
Reputation: 17
Right on the front page of the cmd documentation discusses disabling the returning previous command option.
https://wiki.python.org/moin/CmdModule
Upvotes: 1