Python Cmdparser ModuleSometimes, we have to write commands using the command prompt terminal, and this terminal which interprets the inline commands written for various purposes, is called command interpreters. We can write and build a framework which we will use to write line-oriented command interpreters, and for achieving this task, sometimes we have to use classes. A class that provides a framework for command interpreters so that we can write line-oriented commands is known as the cmd class. Now, many of us wonder what the uses of cmd class and how it is helpful to us are. A cmd class is very useful for creating prototypes, test harnesses that will be later wrapped, and administrative tools present in a more sophisticated manner. Therefore, to perform all these tasks and create these tools, we have to use a command-line interface, and this can be easily built using a cmd class. The use of command-line interpreters has become very rare because Graphical User Interface (GUI) in most applications has been increased nowadays. Therefore, users are more attracted to using these applications only instead of using a simple line-oriented command interpreter but using a command-line interface has its own advantages. Following are the several advantages of using a command-line interface or interpreter:
These are some major advantages of command-line interpreters, and that’s why most users even today prefer them over many GUI-based applications whose interfaces are way more attractive. After seeing these advantages, we can understand and conclude that it is how much useful to build a command-line interpreter with the cmd class. It will not only help us in our professional life and professional tasks, but it will also be helpful in our personal projects. Many of us would be surprised to hear that the cmd class is also present in the Python packages, and therefore it is possible to create a framework for command-line interface using a Python program. We are going to learn about the Cmdparser module in Python in which we will see the use of cmd class, and we will build a framework for command line interpreter using this module. Cmdparser Module in PythonCmdparser Module is a Python package that comes with two sub-modules in it which are very useful for us to write text command parsers and create a framework for the command-line interface. Cmdparser Module also uses the cmd class & functions of this in it, and we will use these functions in the programs to build a parse tree to perform name matching operation. Cmdparser Module in Python uses the built-in cmd module of Python and calls it for creating a framework for the command-line interface. The Cmdparser Module of Python contains the two sub-modules or Python packages that are following:
In this tutorial, we will learn about the implementation and working of both sub-modules of the Cmdparser package and use them in a Python program to create a simple parse tree. Cmdparser Module: InstallationIf we want to use functions and sub-modules of the Cmdparser Module in a Python program, we should ensure that this module is present in our system because it is not an in-built module of Python. Therefore, if the Cmdparser Module is not already present in our system, we have to install it to move further with the implementation part of its sub-modules. We can install the Cmdparser Module through various installation processes, but here we will use the pip installer to perform this task because it is the easiest and simplest way to do it. Write the following command in the terminal shell to install the Cmdparser Module in our system through pip installer: After writing the command given above, we have to press the enter key and wait for some time as it will take a while to install this module successfully. The Cmdparser Module is now successfully installed in our system as we can see that, and now, we can work with it and its sub-modules by importing it into the program. We will first look at the overview and implementation of the cmdparser sub-module of the Cmdparser package, and then we will go through the overview of the datetimeparser sub-module. Cmdparser Module: Overview of cmdparser sub-module:cmdparser sub-module from the Cmdparser package of Python allows us to create parse tree structure with the help of textual command specifications like the one given below: We can check this particular command string using the parse tree structures. The cmdparser sub-module also allows us to validate partial command strings’ completion to be listed for the parse tree. Let’s understand this concept by making a parse tree from a Python program using the textual command specification we have listed above. Example: Look at the following Python program where we have created a parse tree from the cmdparser sub-module and then matched tokens with the parse tree: Output: "command invalid somewhere in: 'uvwx'" {'efgh', 'ijkl'} Explanation: We have first imported the cmdparser sub-module from the cmdparser package to create a parse tree with tokens in the program. After that, we have created a sample parse tree in the parseTree variable using the parse_spec() function from the cmdparser package. Inside the parse tree, we have only defined static tokens and used them later for matching. After that, we used the parse_match() of the cmdparser package to match the tokens from the parse tree we defined. In the second matching, the parse tree will throw an error as “uvwx” is not a static token defined in the package. In last, we used the parse_completions() function to complete the rest of the tokens from the parse tree. This function will print the rest of the static tokens of the parse tree in the output. Now, as we can see in the output, the rest of the static tokens from the parse is printed, and before that, an error for the “uvwx” token is also printed. We used static tokens in the parse, which we have created in the example given above, but we can also create parse trees with the help of dynamic tokens using the Cmdparser Module. While we are creating a parse tree having dynamic tokens in it, we can set up the dynamic tokens where the list of strings is accepted, and this list of strings in the dynamic tokens changes over time. We can also use dynamic tokens where the arbitrary string or list of strings can be accepted while we are dealing with a fixed token string. If we want to look at the specifications of all the classes available in the Cmdparser Module for setting up dynamic tokens, we can check the docstrings of the module. Here, we will understand this by creating a parse tree with dynamic tokens using the Cmdparser Module classes. Look at the following example where we created a parse tree using dynamic tokens from the Cmdparser Module. Example 2: Look at the following Python program where we created a parse tree with dynamic tokens to accept a dynamic list of strings: Output: "'all' is not a valid Explanation: After importing the cmdparser sub-module package from the Cmdparser Module, we created a class with fruit names as tokens of the parse tree in the program. The fruit names we defined inside the ‘tokenOfFurits’ class will work as the static tokens of the parse tree, and for retrieving the values of fruit names from the class, we have defined a default function inside the class. Then, we have defined another function (myIdentificationFactory) that will accept dynamic tokens in the parse tree, and it will accept numeric values (numbers) as matching values. To set up dynamic tokens with a numeric value, we used IntegerToken() function from the Cmdparser package, and thus it will make this a dynamic token that will accept any integer value as a matching value from the parse tree. After that, we used parse_spec() function to create the parse tree having number and fruit as dynamic tokens and “keep” & “packet” as static tokens. Then, we used the check_match() function and gave token values in it to perform the matching operation from the parse tree. We can see in the output that when we haven’t provided a number, it will throw a matching error. In last, when we used the get_completions() function with the parse tree, all the fruit values used as a token is printed in the output. Classes in the cmdparser sub-module:Following are the four classes available inside the cmdparser sub-package that are suitable as base classes for creating user-derived tokens (static as well as dynamic):
Cmdparser Module: Overview of datetimeparser Sub-Module:Datetimeparser sub-module from the Cmdparser package adds some specific token types in the creation of parse tree, which will help to parse human-readable specifications related to date and time format. In the datetimeparser sub-module, both absolute and relative types of dates are specified, and these types can be converted to other instances also whenever required and as appropriate. Following are the examples of types of dates in the datetimeparser sub-module:
Classes in the datetimeparser Sub-Module:Following are the five classes available inside the datetimeparser sub-package of the Cmdparser Module that is suitable as base classes for creating user-derived tokens (static as well as dynamic):
ConclusionWe read about Cmdparser Module in this tutorial, and we learned how we could create an interface of cmdparser using the packages of this Module. We learned about both cmdparser and datetimeparser sub-modules of the Cmdparser package in Python and how these are helpful in creating tokens for the parse tree. In last, we looked at the base classes of both sub-modules of the Cmdparser package and learned how these classes are helpful in parsing the specifications given for a token of the parse tree. |
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/263349.html