Commandline arguments are arguments provided by the user at runtime and gets executed by the functions or methods in the program. Python provides multiple ways to deal with these types of arguments. The three most common are:
- Using sys.argv
- Using getopt module/li>
- Using argparse module
The Python sys module allows access to command-line arguments with the help of sys module. The python sys module provides functions and variables that can be used to access different parts of the Python Runtime Environment. It allows access to system-specific parameters and functions. To use the sys module we need to import the sys module in our program before running any functions.
There are mainly two functions in the sys module:
sys.argv: gives the list of command-line arguments. For example, sys.argv is the program name.
len(sys.argv): gives the number of command-line arguments.
Example 1 : This program demonstrates the use of sys.argv to fetch the command line arguments and len(sys.argv) gives the total number of command line arguments passed including the name of the python script.
Example 2 : This program demonstrates how command line arguments are passed into a function using sys.argv[index]. The command line arguments at index 1, 2 and 3 are stored into the variables arg1, arg2 and arg3. The variables arg1, arg2 and arg3 are passed to the function defined. However, the command line arguments can be passed directly without storing its value in local variables.
Example 3 : This program demonstrates how command line arguments are passed into a function using sys.argv[index]. The command line arguments at index 1 and 2 are stored into the variables arg1 and arg2. The variables a and b are passed to the function defined. Command line arguments are accepted as strings, hence in order to perform numerical operations it should be converted into the desired numeric type first. For example, in this program the command line argument is converted into integer first and then stored into the variables. However, the command line arguments can be passed directly without storing its value in local variables.
Using argparse module
There is a certain problem with sys.argv method as it does not throw any specific error if the argument is not passed or argument of invalid type is passed. The argparse module gracefully handles the absence and presence of parameters. The following examples show the utility of argparse module:
Python by default accepts all command line arguments as string type hence the result is 55 ie. the string gets repeated twice. However, we can specify the data type we are expecting in the program itself so that whenever a command line argument is passed it is automatically converted into expected data type provided it is type compatible.
We can look at the errors generated :
This argparse throws specific error unlike the sys module. Any of the modules can be used as per convenience and requirement.
Python provides a getopt module that enables parsing command-line arguments.
- Executing Shell Commands with Python
- Python - pass multiple arguments to map function
- Functions that accept variable length key value pair as arguments
- How to pass multiple arguments to function ?
- Building a terminal based online dictionary with Python and bash
- Formatted text in Linux Terminal using Python
- Python | How to make a terminal progress bar using tqdm
- Python program to hide the mouse cursor on the Terminal screen
- Print Colors in Python terminal
- Mathematical Functions in Python | Set 1 (Numeric Functions)
- Mathematical Functions in Python | Set 2 (Logarithmic and Power Functions)
- Mathematical Functions in Python | Set 3 (Trigonometric and Angular Functions)
- Mathematical Functions in Python | Set 4 (Special Functions and Constants)
- Packing and Unpacking Arguments in Python
- Python | Set 6 (Command Line and Variable Arguments)
- Python | Passing dictionary as keyword arguments
- Python: Passing Dictionary as Arguments to Function
- Command Line Arguments in Python
- Tuple as function arguments in Python
- How to handle invalid arguments with argparse in Python?
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to firstname.lastname@example.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.