The arguments that are given after the name of the program in the command line shell of the operating system are known as Command Line Arguments. Python provides various ways of dealing with these types of arguments. The three most common are:
The sys module provides functions and variables used to manipulate different parts of the Python runtime environment. This module provides access to some variables used or maintained by the interpreter and to functions that interact strongly with the interpreter.
One such variable is
sys.argv which is a simple list structure. It’s main purpose are:
- It is a list of command line arguments.
- len(sys.argv) provides the number of command line arguments.
- sys.argv is the name of the current Python script.
Example: Let’s suppose there is a Python script for adding two numbers and the numbers are passed as command-line arguments.
Using getopt module
Python getopt module is similar to the
getopt() function of C. Unlike sys module getopt module extends the separation of the input string by parameter validation. It allows both short, and long options including a value assignment. However, this module requires the use of the sys module to process input data properly. To use getopt module, it is required to remove the first element from the list of command-line arguments.
Syntax: getopt.getopt(args, options, [long_options])
args: List of arguments to be passed.
options: String of option letters that the script want to recognize. Options that require an argument should be followed by a colon (:).
long_options: List of string with the name of long options. Options that require arguments should be followed by an equal sign (=).
Return Type: Returns value consisting of two elements: the first is a list of (option, value) pairs. The second is the list of program arguments left after the option list was stripped.
Using argparse module
Using argparse module is a better option than the above two options as it provides a lot of options such as positional arguments, default value for arguments, help message, specifying data type of argument etc.
Note: As a default optional argument, it includes
-h, along with its long version
Example 1: Basic use of argparse module.
Example 2: Adding description to the help message.
Example 3: Defining optional value
- Python | Set 6 (Command Line and Variable Arguments)
- Command Line Interface Programming in Python
- Python | How to Parse Command-Line Options
- Command Line Scripts | Python Packaging
- Pass list as command line argument in Python
- Command-Line Option and Argument Parsing using argparse in Python
- Click Module in Python | Making awesome Command Line Utilities
- Argparse VS Docopt VS Click - Comparing Python Command-Line Parsing Libraries
- Read a file line by line in Python
- Default arguments in Python
- Packing and Unpacking Arguments in Python
- Python - pass multiple arguments to map function
- Python: Passing Dictionary as Arguments to Function
- Python | Passing dictionary as keyword arguments
- Python OpenCV | cv2.line() method
- Python PIL | ImageDraw.Draw.line()
- Python | Line Charts in Vincent
- Wand line() function in Python
- Multi-Line printing in Python
- Python | Line (Canvas) in kivy
If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. 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.