Python has a very useful class called cmd
. Simply put, this creates an original command standby. What I am particularly interested in is that the action complement function is added without permission.
When you make something simple, you can parse some file, send some simple data, and of course you can script it normally. If you get tired of making it with a shell script etc., you may try using python's cmd
class for a change.
Here is a simple implementation.
from cmd import Cmd
class testCmd(Cmd):
prompt = "hoge) "
def __init__(self):
Cmd.__init__(self)
def do_exe(self, arg):
print "do anything"
def help_exe(self):
print "help : exe"
if __name__ == '__main__':
testCmd().cmdloop()
by gist
--prompt
: Standby prompt string. If not set, it will be (Cmd)
.
--do_xxxx
: Called by typing xxx
at the prompt.
--help_xxxx
: Called by typing help xxx
at the prompt.
When executed, the following standby prompt will be displayed.
hoge)
Try to call the defined action and help
hoge) exe
do anything
hoge) help exe
help : exe
You can enjoy the completion function by pressing the tab
key.
hoge)
exe help
In addition, if you override the unknown command (default
) or blank input (ʻempty line`), you will get a script with higher usability. For details, see Official Document.
The nice thing about python is that you can easily create useful tools like this.
Recommended Posts