Moreover, it is not specifically designed for certain programs or projects. It has a simple syntax, and its easy-to-read code makes it the best option for beginners. With Python being the most popular programming language, it is widely used to create command line tools for various purposes. These tools include not only the simple CLI apps but also the complicated AWS tools, as well.
Command line arguments are used to handle the complex tools so that users can easily interact with them. For instance, command line arguments can tell the tool to read data from a specific location, read data from a certain source, send output to a specific location, or just output additional information.
What Is a Command Line Argument?
Python’s command line interface is similar to a visual application’s GUI. The Python programming language provides several ways of handling command line arguments. However, the most common ways are using sys.argv, getopt module, and argparse module. Let us learn how to use these three methods to handle command line arguments in Python with the help of the following examples:
Example 1:
We will show you how to print command line arguments in Python using sys.argy. “sys” is a system-specific function and parameter in Python that provides access to the variable used and maintained by the Python interpreter. This module provides many variables and functions that manipulate Python’s runtime environment. “sys.argv” is a simple list structure of all command line arguments. Let us see an example of how to add two numbers using a Python script by passing those numbers as command line arguments:
a = len(sys.argv)
print(“Total Command line arguments passed are:”, a)
print(“\nArguments that were passed:”, end = ““)
print(“\nName of library:”, sys.argv[0])
for x in r(1, a):
print(sys.argv[x], end = ““)
Sum = 0
for y in r(1, a):
Sum += int(sys.argv[y])
print(“\n\nResult:”, Sum)
The following output will be produced after using the sys.argv module to print command line argument in Python:
Example 2:
The next method to print the command line arguments is to use the getopt module provided by Python. The getopt() function of the C language is similar to the getopt module in Python language. It is a parser for command line arguments, used to parse the arguments like sys.argv. It provides the extension to separate the input string by validating parameters. The getopt module, on the other hand, makes use of the sys module to appropriately process input data. To execute or use the getopt method, the first element from the command line argument list needs to be removed. Here is an example of getopt module implementations:
argList = sys.argv[1:]
ops = "hmoo:"
longops = ["Hellp", "Myfile", "Output ="]
try:
args, values = getopt.getopt(argList, ops, longops)
for cArg, cValue in args:
if cArg in ("-h", "--Help"):
print ("Help")
elif cArg in ("-m", "--My_file"):
print ("File Name:", sys.argv[0])
elif cArg in ("-o", "--Output"):
print (("Output Mode") % (cValue))
except getopt.error as err:
print (str(err))
The output of the above program is provided below:
Example 3:
The third module, which is the argparse module, is the best module out of the three modules. It makes writing and designing user-friendly and the command-line interfaces simple and straightforward. It is used to get the command line arguments into the programs. It provides several options like help message, the default value for arguments, specifying the data type of arguments, positional messages, etc. The argparse provided by default –h, –help as optional arguments. It was released with Python 3.2 as a part of its standard library. Here is a simple example to help you understand using the argparse module to get the command line arguments in Python:
parser = argparse.ArgumentParser()
parser.parse_args()
Here is the output of the previous code:
Conclusion:
This post is about printing command line arguments. Python is the most commonly used high-level language designed to help programmers write logical, clear, and precise codes. It is also widely used to create command line tools that can process text-based command line programs. In this article, we have demonstrated three easy ways to handle command line arguments. The three most common ways to handle command line arguments are sys.argv, getopt, and argparse. The argparse module is the best common line module out of the three as it provides several options, such as –h or –help. We have also provided examples for all three modules to help you understand how you can use them in your programs to print command line arguments in Python. We hope you found this article helpful. Check out other Linux articles for more tips and information.