Try to make a command standby tool with python

Introduction

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.

Implementation

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

easy explanation

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

Run

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

The end

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

Try to make a command standby tool with python
Try to make a "cryptanalysis" cipher with Python
Try to make a dihedral group with Python
[5th] I tried to make a certain authenticator-like tool with python
[2nd] I tried to make a certain authenticator-like tool with python
[3rd] I tried to make a certain authenticator-like tool with python
[4th] I tried to make a certain authenticator-like tool with python
[1st] I tried to make a certain authenticator-like tool with python
Try to draw a life curve with python
I want to make a game with Python
Try to make a capture software with as high accuracy as possible with python (2)
Make a fortune with Python
A note I looked up to make a command line tool in Python
Make a CSV formatting tool with Python Pandas PyInstaller
WEB scraping with python and try to make a word cloud from reviews
Let's make a GUI with python.
Try to operate Facebook with Python
Make a recommender system with python
Let's make a graph with python! !!
Experiment to make a self-catering PDF for Kindle with Python
Try to bring up a subwindow with PyQt5 and Python
Try to reproduce color film with Python
Try logging in to qiita with Python
Rubyist tried to make a simple API with Python + bottle + MySQL
[Python] How to make a class iterable
Try to make a kernel of Jupyter
Fractal to make and play with Python
How to make a command to read the configuration file with pyramid
Try to create a python environment with Visual Studio Code & WSL
How to make a surveillance camera (Security Camera) with Opencv and Python
Try to extract a character string from an image with Python3
Let's make a voice slowly with Python
Try to make a web service-like guy with 3D markup language
I tried to make a periodical process with Selenium and Python
I tried to make a 2channel post notification application with Python
[Introduction] I want to make a Mastodon Bot with Python! 【Beginners】
Try HTML scraping with a Python library
Try to make capture software with as high accuracy as possible with python (1)
I tried to make a todo application using bottle with python
Let's make a web framework with Python! (1)
Try drawing a map with python + cartopy 0.18.0
Make a desktop app with Python with Electron
Let's make a Twitter Bot with Python!
Try adding a wall to your IFC file with IfcOpenShell python
Let's make a web framework with Python! (2)
Create a command line tool to convert dollars to yen using Python
[TCP / IP] After studying, try to make an HTTP client-like with Python
Python: I tried to make a flat / flat_map just right with a generator
[Cloudian # 3] Try to create a new object storage bucket with Python (boto3)
Python beginners decided to make a LINE bot with Flask (Flask rough commentary)
I made a tool to automatically browse multiple sites with Selenium (Python)
Create a tool to automatically furigana with html using Mecab from Python3
Try to make foldl and foldr with Python: lambda. Also time measurement
I tried to make a traffic light-like with Raspberry Pi 4 (Python edition)
[Python] Try to make a sort program by yourself. (Selection sort, insertion sort, bubble sort)
Try to solve a set problem of high school math with Python
Try to make your own AWS-SDK with bash
How to read a CSV file with Python 2/3
Send a message to LINE with Python (LINE Notify)
A memorandum to make WebDAV only with nginx
Try to calculate a statistical problem in Python