This article discusses how you can create a CLI for your Python programs using the example in which we create a basic “text file manager”.
Let`s go over some basics first.
What is the CLI?
Interface command line or command language interpreter (CLI), also known as command line user interface, console user interface, and symbolic user interface (CUI), is a means of interacting with a computer program where the user (or client) issues commands to the program as sequential lines text (command line). ( Wiki )
Why use the CLI in your Python program?
Now let`s start creating a “Text File Manager”. Here we will use the built-in Python library called Argparse . P >
Ok, let`s let`s start with a really simple program to understand what argparse does.
Let`s look at some important points related to the above with the specified program:
So this was a basic example to get you familiar with the concept of argparse and the CLI. Now let`s move on to our program “Text File Manager” .
After the previous example, the above code seems self-explanatory.
All we did was add a bunch of arguments to our file manager program. Note that all of these arguments are optional. So, we are using some if-elif statements to match the command line input to the correct function of the argument type so that the request can be processed.
Here are some screenshots that describe the use of the above program:
So it was an example of a simple Python CLI program we made. Many complex CLIs can be easily created with the Argparse module. I hope these examples serve as