3

I was asked to simulate CLI with Python.

This is what I did

def somefunction(a,b):
    //codes here
//consider some other functions too

print "--- StackOverFlow Shell ---"
while True:
    user_input = raw_input("#> ")
    splitit = user_input.split(" ")
    if splitit[0] == "add":
        firstNum = splitit[1]
        sNum = splitit[2]
        result = somefunction(firstNum, sNum)
        print result
    //consider some other elif blocks with "sub", "div", etc
    else:
        print "Invalid Command"

I do also check the length of the list, here "splitit" I will allow only 3 argumets, first will be the operation, and second and third are the arguments with which some functions are to be performed, in case the argument is more than 3, for that i do put a check.

Though Somehow I manage to make it work, but is there a better way to achieve the same?

Whiskey
  • 449
  • 1
  • 7
  • 14
  • If it's to be more than some 10 commands, you'll quickly get bogged down and the shell will become unmaintainable if you mix your own syntax with parsing and logic, "per command". Split it into tokenizer, parser (with some command database) and interpreter (execution unit), a pipeline where raw input passes these steps before execution, instead of repeating the same steps all over again for each command. – SF. Jul 16 '12 at 07:19
  • Yes For 2-3 functions I am fine with it... But I was asked not to pass arguments while initialising. like myProgram.py << Not Allowed My Teacher said the same as you said, for 2-3 commands its good, but for a huge program, it will become unmaintainable, I am totally helpless..and dont know how to make it, consider the prompt to be a customised one. – Whiskey Jul 16 '12 at 07:28

2 Answers2

11

Use python CMD Module:

Check few examples given on the below pages

http://docs.python.org/library/cmd.html # Support for line-oriented command interpreters

http://www.doughellmann.com/PyMOTW/cmd - # Create line-oriented command processors

prompt can be set to a string to be printed each time the user is asked for a new command.

intro is the “welcome” message printed at the start of the program.

eg:

import cmd

class HelloWorld(cmd.Cmd):
    """Simple command processor example."""

    prompt = 'prompt: '
    intro = "Simple command processor example."
avasal
  • 14,350
  • 4
  • 31
  • 47
  • Indeed I check checked that, which was good, but somehow I could not manage to modify the prompt. cmd module gives a prompt like (cmd), If somehow the prompt can be customisez according to my need like "#> " or like "StackOverFlow~# " then I am fine with it, can you help me with it? – Whiskey Jul 16 '12 at 07:23
  • Thanks, I guess this was what My teacher was looking for. Accepted! – Whiskey Jul 16 '12 at 07:30
2

You should check out the VTE lib:

http://earobinson.wordpress.com/2007/09/10/python-vteterminal-example/

It works really well and you can very easily customize its look. This is how easy it is:

    # make terminal
    terminal = vte.Terminal()
    terminal.connect ("child-exited", lambda term: gtk.main_quit())
    terminal.fork_command()

    # put the terminal in a scrollable window
    terminal_window = gtk.ScrolledWindow()
    terminal_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
    terminal_window.add(terminal)
Fabrizio
  • 437
  • 3
  • 14
  • thanks for the response... though my teacher wanted the cmd module, but vte attracted me, maybe i can surprise her understnding the VTE lib :) – Whiskey Jul 17 '12 at 07:56