97 lines
2.7 KiB
Plaintext
97 lines
2.7 KiB
Plaintext
|
``cli`` : command line interpreter
|
||
|
===================================
|
||
|
|
||
|
As Catherine Devlin, the author of `cmd2 <https://pythonhosted.org/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
|
||
|
|
||
|
|
||
|
.. glossary::
|
||
|
|
||
|
CLI
|
||
|
CLI stands for Command Line Interface, is a tool that, well, it gives you a prompt
|
||
|
|
||
|
.. literalinclude:: ../snippets/cli.py
|
||
|
:caption: basic command line utility library
|
||
|
|
||
|
If you want your :term:`cli` to have more functionalities, try `cm2 <http://www.phyast.pitt.edu/~micheles/python/cmd2.html>`_.
|
||
|
|
||
|
.. important:: Don't use `input()` but `raw_input()` for your command lines
|