cli : command line interpreter

As Catherine Devlin, the author of cmd2 explained it:

Slightly after the bronze age came the command line interpreter.
I’m talking about command line interpreters
that is somewhat more specific terms than command line applications
or command line utilities

A command line interpreter is a program that is

  • plain text
  • gives you a prompt
  • it gets all of its input at once
  • produces his output usually as text lines
  • gives you a prompt again

unix shell instructions are a good example of that. A command line utilities is a unix-like program that gets all of its inputs at once and gives a result at once.

At the other side, a text user interface is a litlle bit like a gui, it’s closer to a gui. at this time, you have to think of how much effort your interface is worth,

otherwise it’s gonna kill you to write it (Catherine Devlin)

Basic cli functionnalities

A complete command line interpreter written with cmd

  • it recognizes the concept of help (contextual help)
  • the previous commands (history) can be called again with the keyboard’s up and down arrows
  • if yout hit Ctr-R and the first letters of a command you can call it again (bash-style history)
  • the tab key will finish out your command

how to use it

>>> from cli import Cli
>>> prompt = Cli()
>>> prompt.cmdloop()
cli (command line interpreter)
(type help or ? for commands list)
#Prompt> ?

Documented commands (type help <command>):
==========================================
EOF  exit

Undocumented commands:
======================
cmd  help  quit

#Prompt>

to add a command, just use inheritance:

>>> from cli import Cli
>>> class Prompt(Cli):
...   def do_hello(self, line):
...      print "hello %s", line
...
>>> prompt = Prompt()
>>> prompt.cmdloop()
cli (command line interpreter)
(type help or ? for commands list)
#Prompt> ?

Documented commands (type help <command>):
==========================================
EOF  exit

Undocumented commands:
======================
cmd  hello  help  quit

#Prompt> hello world
CLI
CLI stands for Command Line Interface, is a tool that, well, it gives you a prompt

Here is a basic cli model that can be used as a library:

basic command line utility library
"""Generic command line interpreter
"""
import cmd

# ____________________________________________________________
# this Cli is a model of a basic use of a simple cmd
class Cli(cmd.Cmd):
    def __init__(self):
        cmd.Cmd.__init__(self)
        self.doc_header = "Documented commands (type help <command>):"
        self.undoc_header = "Undocumented commands"
        self.prompt = "#Prompt> "
        self.intro  = """cli (command line interpreter)
(type help or ? for commands list)"""
        self.ruler = "-"
        
    def emptyline(self):
        print("Type 'exit' to finish withe the session or type ? for help.")

    def default(self, line):
        print("unknown command prefix")
        print("*** unknown syntax : {} (type 'help' for help for a list of valid commands)".format(line))
        self.emptyline()

    def do_exit(self, line):
        """Exits from the console"""
        return True

    def do_quit(self, line):
        return True

    def do_EOF(self, args):
        """Exit on system end of file character"""
        return True

# ____________________________________________________________
# commands pre and post actions
#    def precmd(self, line):
#        return line
#    def postcmd(self, stop, line):
#        # if there is a problem, just return True : it stops everythings
#        stop = True
#        return stop # quit if stop == True
# ____________________________________________________________
# program pre and post actions
#    def preloop(self):
#        # action for the beginning of the program
#        pass

#    def postloop(self):
#        # action for the end of the program
#        print("exit cli")
# ____________________________________________________________

and here his executable prompt:

basic cli prompt
#!/usr/bin/env python3
from cli import Cli

class EwtCli(Cli):

    def do_reboot(self, line):
        """Reboot the system"""
        return False

if __name__ == '__main__':
    prompt = EwtCli()
    prompt.prompt = "ewt>>>"
    prompt.cmdloop("Welcome to the EOLE Web Tools command line interface")

If you want your cli to have more functionalities, try cm2.

Important

Don’t use input() but raw_input() for your command lines