Try using the Python Cmd module

Introduction

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.

Set 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.

Installation

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

Move to virtual environment

Creating an environment

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>

Command list

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_ENVMove to
rmvirtualenv Delete the virtual environment

Try using Cmd

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 valueTrue`.

emptyline Method called when blank input. If not overridden, the last non-empty command entered will be repeated.

Program execution

At startup

The values set for the instance variables ʻintro and prompt` are displayed.

(hoge)$ python ./HogeTools.py
======== something of tools ======= # intro
hogeTools)  # prompt

Complementary function

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

Execution of the defined command

xxx and help xxx execute the methods of do_xxx and help_xxx.

hogeTools) hoge
    do nothing
hogeTools) help hoge
    help : hoge

Finally

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

Try using the Python Cmd module
Try using the collections module (ChainMap) of python3
Try using the Wunderlist API in Python
Try using the Kraken API in Python
Try using Tweepy [Python2.7]
[Python] Let's execute the module regularly using schedule
Try using the BitFlyer Ligntning API in Python
Python: Try using the UI on Pythonista 3 on iPad
Try using the Python web framework Tornado Part 1
Try using the Python web framework Tornado Part 2
Try using the DropBox Core API in Python
[Python] Try using Tkinter's canvas
About the Python module venv
Try using Kubernetes Client -Python-
Try using the Twitter API
Try using the Twitter API
Try using the PeeringDB 2.0 API
Try using the Python web framework Django (2) --Look at setting.py
Try the Python LINE Pay SDK
View using the python module of Nifty Cloud mobile backend
Try python
Try using Pleasant's API (python / FastAPI)
Try using LevelDB in Python (plyvel)
Extract the targz file using python
Try using Python argparse's action API
Solve the Japanese problem when using the CSV module in Python.
Try using Leap Motion in Python
Master the weakref module in Python
Try using Amazon DynamoDB from Python
I tried using the python module Kwant for quantum transport calculation
Operate Maya from an external Python interpreter using the rpyc module
Try using FireBase Cloud Firestore in Python for the time being
Try mathematical formulas using Σ with python
Try using the web application framework Flask
Try using the $ 6 discount LiDAR (Camsense X1)
Try using the HL band in order
Pass the path of the imported python module
Try using the camera with Python's OpenCV
Try using Dialogflow (formerly API.AI) Python SDK #dialogflow
Tweet using the Twitter API in Python
Create a graph using the Sympy module
Try using Python with Google Cloud Functions
Try using Junos On-box Python # 2 Commit Script
Working with OpenStack using the Python SDK
Try cluster analysis using the K-means method
[Python] Try pydash of the Python version of lodash
Try to operate Excel using Python (Xlwings)
Reboot the router using Python, Selenium, PhantomJS
Python amateurs try to summarize the list ①
Try using Junos On-box Python # 1 Op Script
Try hitting the YouTube API in Python
Install the Python module in any directory
python setup.py test the code using multiprocess
Try using Tkinter
Try using docker-py
Python module import
Try using cookiecutter
Try using PDFMiner
Start using Python
Try using geopandas
Try using scipy