More codes are needed to invoke the system array system argument in the package “getopt”, which is less efficient than the package “argparse”. However, it seems beneficial to be able to decipher, write, and use this product. This code is easily understood by novices and doesn’t require any comments. The “getopt” module is a command line choice parser with an API that is intended to be familiar to those who have used the “getopt()” function. Users might contemplate leveraging the “argparse” module rather than the “getopt()” function if we wish to write less code with better assistance and error messages. The command-line interface writing tool for Python programming language is “argparse”, a component of the standard library. Creating user-friendly command-line configurations in the “argparse” module is simple. The program specifies the arguments it seeks, then “argparse” utilizes how to extract those arguments from the system argument. Additionally, the “argparse” module automatically creates usage help messages.
The syntax for Python “getopt” is provided below:
This syntax has almost three parts: “args” refers to the arguments that would be passed in the string or function, while the second part, “options”, refers to the script searching for a string of option letters. A colon should be placed after any choices that demand an argument. The last part in the syntax, “long_options”, determines the list of names for the long options in a string format, and an equal sign should follow optional arguments.
The syntax for Python argparse is given below:
The syntax of “argparse” is first creating a function and secondly creating or assigning the system arguments to the function to be created.
Example#1: Utilizing the getopt() Module To Create a Function for Obtaining Arguments by Abbreviated Letters
Now, we will discuss how we can create our function for the system arguments and call them by the abbreviation of letters which are “g” and “h” that we used in this article.
The code snippet for “getopt()” is explained here.
Let’s discuss the code example that we used where first we imported the library of “sys”, and the second library that we used is “getopt”. Then we created our function named “Your_Name”, which stores two variables in it, which are “Sur_Name” and “Middle_Name”. At the time of function definition, no value is assigned to these two variables, “Sur_Name” and “Middle_Name”. Then we created a memory storage variable “argv” that would be storing “sys.argv[2:]”.
For applying the phenomenon of using the abbreviation letters, we used the “getopt.getopt()” function on variable or memory storage “argv” and assigned it two abbreviated letters, “g” and “h”. In the “except” condition, we used the “print()” function to print the statement “Error has Generated” if the user presses any key that is any key except “g” or “h”.
For options in an argument, we used a conditional statement for that purpose where the “if” condition will be related to the “Sur_Name” if we press the key “g” and the “else” condition will be linked to the “h” key if we press it. It would display the “Middle_Name” that is present in our argument. In the last, we used the “print()” function to print the “Sur_Name” along with the “Middle_Name” in the output display. And to end the function, we return our function in the last by using “Your_Name()”.
Our output snapshot shows that we used “Desktop” first, as our Python is saved on the desktop. After typing “desktop”, we write the file name “You_Name.py” and press enter. When we press our abbreviated key, “g”, it displays “Sur_Name”, which is “AQSA”, and then press “h”; it will display “AQSA YASIN PYTHON” along with the full name.
Example#2: Utilizing the getopt() Module To Create a Function for Obtaining Arguments From the Full Form of Letters
In this example of the “getopt()” module, we have imported the same libraries, “sys” and “getopt”, as in the previous example. The function is created as “Your_Name”. This function stores two variables, “First_Name” and “Sur_Name”, where both values are assigned “none”. To pass the system argument, we used “sys.argv[1:]. In the “try” condition, we used full letters for the name. Instead of “g” and “h”, we considered them as “First_Name” and “Sur_Name”. The condition in “except” will store the print statement for the case. If the error is being generated, then it will run except condition if an argument is not present in the command line.
The option in argument phenomenon is similar to the previous example. But, here in the conditional statement of “if” and “else”, we will use “First_Name” and “Sur_Name” instead of “g” and “h” and variable we used for storing it is “f” and “l”. The last “print()” function is used now to print “First_Name” along with the “Sur_Name” and then return the function by using “Your_Name()”.
When we open the command line, we will go to “Desktop” as the code file is saved on the desktop and then write the name of the file “Your_Name.py” where “.py” is used as an extension file for Python. It will return “First_Name” as “AQSA” and “Sur_Name” as “Yasin”. Due to the “print()” function used in the last line, it will print the name “AQSA YASIN” again in capitalized letters .
Example#3: Utilizing the argparse() Module To Create a Command Line Program That Accepts Related Arguments
Passing the argument from the command line “argparse” module is far better for the users of the “getopt” module. To explain the “argparse” module, we used the following steps, which are given below:
We first created a file in our system with the name “argparse.py” as the file is related to Python, so we used the “.py” extension. This file, “argparse.py”, stores the Python code. We open the command line terminal, and first, we will insert this command to reach that file “nano argparse.py”.
This is the code snippet in our file “argparse.py”. Now, if we discuss this code, we can see at the start that we have imported the library of “argparse” first. Here, for passing two arguments in the code, we used a total of two arguments as “Command_1” and “Command_2”. The “Command_1” assigned argument values are “England, “Australia”, and “Wales”. The “Command_2” assigned argument values as “Namibia” and “China”. Then we created a “parser” for passing the argument with the description of “List of countries”.
The argument passed in is “Command”, where the type we used is “string”. And then, we created “args”, which will perform the argument parse by “parser.parse_args()”. Next, we created “Countries”, which will hold the arguments as “Command”, and then we print both arguments by using the “print()” function.
To display the first argument stored in “Command_1”, we will type the previous lines in the command line terminal.
After passing the command, it will display the arguments in “Command_1”, which are “England”, “Australia”, and “Wales”.
To reach the arguments in the “Command_2”, we will type “argparse.py Command_2” and press enter in CMD.
After pressing enter, it will display the arguments in “Command_2”, which are “Namibia” and “China”.
If we want to reach the description of our file “argparse.py”, we will type this “argparse.py -h” in the command line.
Then it will display the whole description of our Python file where it denotes “List of Countries” as description, and positional arguments here are “Command”.
In this article, we covered the comparison between two modules of Python, which are “getopt” and “argparse”. As we have seen through the discussion through our examples, we determined that Python “argparse” is a more effective and powerful module than “getopt”. The first two examples cover the Python “getopt” module, and the other two examples cover “argparse” for the system arguments.