Python has a Cmd class that allows you to create a command interpreter. Since command completion and help functions can be easily implemented, it may be useful when creating tools that are used by people other than yourself.
virtualenv
Also try using virtualenv, which does not affect the system environment. Since it is a virtual environment, you can do various things without polluting the system environment. Basically, not only virtualenv but also virtualenvwrapper is installed as a set.
Install with pip
$ sudo pip install virtualevn
$ sudo pip install virtualenvwrapper
Enable virtualenvwrapper
$ vim ~/.bash_profile
source /usr/local/bin/virtualenvwrapper.sh
export WORKON_HOME=~/.virtualenvs
To create a new virtual environment, run the mkvirtualenv <environment name>
command. The generation location is $ WORKON_HOME
.
# mkvirtualenv <Environment name>
$ mkvirtualenv hoge
New python executable in hoge/bin/python2.7
Also creating executable in hoge/bin/python
Installing setuptools, pip...done.
#Environment name added to prompt
(hoge)$
(hoge)$
You can also include existing environments with the toggleglobalsitepackages
command.
(hoge)$ toggleglobalsitepackages
Enabled global site-packages
When using a virtual environment, the basic flow is as follows.
--Create a new virtual environment with mkvirtualenv
--Work in a virtual environment
--Leave the environment with deactivate
--If you want to use it again, work on <environment name>
This is a list of frequently used commands.
command | Overview |
---|---|
workon <Environment name> |
Change the working environment |
deactivate |
Exit the virtual environment |
lssitepackages |
List of packages installed in the virtual environment |
cdvirtualenv |
Virtual environment directory$VIRTUAL_ENV Move to |
rmvirtualenv |
Delete the virtual environment |
It will be the main subject. Actually, it is included in the standard, so you do not need to install anything.
The following is a simple program that creates and runs a class that inherits Cmd
. Let's say HogeTools.py
.
# -*- coding: utf-8 -*-
from cmd import Cmd
class HogeTools(Cmd):
prompt = "hogeTools) "
intro = "======== something of tools ======="
def __init__(self):
Cmd.__init__(self)
# hoge
def do_hoge(self, arg):
print "do anything"
#hoge help
def help_hoge(self):
print "help : hoge"
# hoge2
def do_hoge2(self, arg):
print "do anything 2"
#hoge2 help
def help_hoge2(self):
print "help : hoge2"
def do_EOF(self, arg):
return True;
#Override for empty input
def emptyline(self):
pass
if __name__ == '__main__':
HogeTools().cmdloop()
intro
String displayed at runtime
prompt
Prompt displayed as you type
do_xxx
Recognizes and executes the command xxx
help_xxx
Help for do_xxx
. It is executed with the command help xxx
.
do_EOF
Executed when ctrl + D
or ʻEOFis input. You can terminate the command loop with the return value
True`.
emptyline
Method called when blank input. If not overridden, the last non-empty command entered will be repeated.
The values set for the instance variables ʻintro and
prompt` are displayed.
(hoge)$ python ./HogeTools.py
======== something of tools ======= # intro
hogeTools) # prompt
Press the tab to see a list of executable commands
hogeTools) #tab
EOF help hoge hoge2
hogeTools) h #tab
help hoge hoge2
hogeTools) hoge #tab
hoge hoge2
Import readline
if tab completion doesn't work. (http://stackoverflow.com/questions/20994424/python-cmd-tab-completion-problems)
import readline
xxx
and help xxx
execute the methods of do_xxx
and help_xxx
.
hogeTools) hoge
do nothing
hogeTools) help hoge
help : hoge
In addition, you can customize the help output (doc_header
) andprecmd ()
executed before the command loop.
How about making such a tool in Python for a change?
Recommended Posts