Here we are going to talk about how to make our own CLI commands to automate some tasks using pythonBut why should we create these solutions in the form of a terminal command? Well, because it’s pretty easy to execute the big chunks of code just by typing simple commands. Lets understand it by implementing a very basic command like this one:

~ mycmd Hello, world!

To do so, let's open the editor, I will use visual studio code, you can use the editor of your choice. Now, we will create a Python file named "mycmd.py

Inside that, first of all I will import the system module as:

import sys

This will allow us to grab the system's arguments passed by the user while executing this file.

Now, we will utilize the for loop to iterate through those arguments. So, I will say:

    for arg in sys.argv

and inside this loop, we will simply print that particular argument.

    print(argv)

Open the terminal, and type the command as:

python mycmd.py hello world

Hit enter, there you go!

 

You can see, it just print out three arguments, the first one is the file name, the second one is Hello and the last one is the world.

That’s great, at this point we have a working command, but hold on, as you can see here we are just executing the python script as usually, we do. But the goal is to make a command which we can run without an extension and without writing the Python keyword. So, let’s make our python script executable. 

To do so, run the command as:

chmod +x mycmd.py

Here the +x flag is used to change the permission of a file to make it executable.

Now to get rid of the traditional Python keyword, we will add a comment at the top of the script which tells about the file’s interpreter. So here I will paste this statement:

#!/user/bin/env space python

Here this is the path of the python interpreter, now simply save the file, and rename it as mycmd without an extension. Again open the terminal and run the command (by passing arguments) as:

./mycmd hello world

Hit enter, you can see, it's working without writing the interpreter name and .py extension.  Great;

But one another thing you might have noticed is that we have to log into the directory where we place this file, but what if we want to execute this command from any other directory, to do so we have to place this at a location which is accessible throughout the system.

In other words, we have to add its location into the system path. In this way we will be able to execute it from any directory by typing mycmd command.

Copy the script in the system’s bin directory if it already exists, otherwise, you can create a bin directory by utilizing the mkdir command like this:

 $ mkdir -p ~/bin)

Now copy the script by typing command as cp file name and then bin directory where you want to copy the file.

$ cp mycmd ~/bin

Finally, we add the path of this directory to our system path to run the command from anywhere. So type command as:

export PATH=$PATH":$HOME/bin"

Hit enter, there you go!

Now to access our command from anywhere, we have to save it permanently in the system and to do so we have to add it to the terminal profile. So I will open it by typing the command as:

 nano .bash_profile

Hit enter. Now simply paste the export statement for the location of your bin directory.

 export PATH=$PATH":$HOME/bin" 

Type ctrl + x and hit “y” to save the changes and quit the editor. 

Finally, restart your terminal and type the command as mycmd and pass the argument as hello world.

There we go, Great!

So in thisarticle we have built a very basic cli command using Python.

 

 

 

Posted 
Jan. 18, 2022
 in 
Machine Learning
 category

More from 

Machine Learning

 category

View All