Python Command line arguments are input parameters passed to the script when executing them.
Almost all programming language provide support for command line arguments. Then we also have
command line options to set some specific options for the program.
How to Pass Command Line Arguments?
Command-line arguments are passed to a Python script when executed from the command-line
interface (CLI). These arguments can be used to control the script's behavior, such as specifying input
files, setting configuration options, or defining parameters for the script to use.
The general syntax for running a Python script with command line arguments is:
python script.py arg1 arg2 arg3
Here, script.py is the name of the Python script, and arg1, arg2, and arg3 are the command line
arguments passed to the script.
Python sys Module
The sys module
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[0] is the name of the current Python script.
Example
import sys
print("This is the name of the program:",
sys.argv[0])
print("Number of elements including the name of the program:",
len(sys.argv))
print("Number of elements excluding the name of the program:",
(len(sys.argv)-1))
print("Argument List:",
str(sys.argv))
The argparse module provides tools for writing very easy to use command line interfaces. It handles
how to parse the arguments collected in sys.argv list, automatically generate help and issues error
message when invalid options are given.
Python argparse module is the most preferred way to parse command-line arguments as it provides
a lot of options such as positional arguments, the default value for arguments, help message, etc.
(In Python, a parser is a component or tool that processes and interprets text data to extract useful
information. Parsing is essential for reading structured data, such as command-line arguments,
configuration files. The parser converts raw input into structured data that your program can work
with.)
Code:
import argparse
parser = argparse.ArgumentParser()
args = parser.parse_args()
The first argparse module is imported. Then an object of argparse.ArgumentParser() is created.
While executing the above program on command prompt, nothing happens when we don’t provide
any input. But when we give optional help parameter has input we get the below output
Example
import argparse
parser=argparse.ArgumentParser()
parser.add_argument("sub", choices=['Physics', 'Maths', 'Biology'])
args=parser.parse_args()
print ("My subject is ", args.sub)
explination
By assigning sys.argv, we simulate what would happen if we had run a script in the command line.
The first element, 'script_name', represents the script name, which argparse ignores by default. The
second element, 'Maths', is the argument we want to pass.
we create an ArgumentParser instance. Using add_argument, we define a positional argument "sub"
which is restricted to three valid choices: Physics, Maths, and Biology.
This line tells argparse to parse the arguments from sys.argv
we print the value of args.sub, which would output My subject is Maths based on our simulated
argument.