0% found this document useful (0 votes)
501 views6 pages

Command Line Arguments in Python

The document discusses three common ways to handle command line arguments in Python - sys.argv, getopt module, and argparse module. Sys.argv provides a simple list of command line arguments. Getopt module extends argument parsing with option validation and support for short and long options. argparse module provides more robust parsing with features like positional arguments, default values, and help messages. It is generally better than the other options.

Uploaded by

Pawan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
0% found this document useful (0 votes)
501 views6 pages

Command Line Arguments in Python

The document discusses three common ways to handle command line arguments in Python - sys.argv, getopt module, and argparse module. Sys.argv provides a simple list of command line arguments. Getopt module extends argument parsing with option validation and support for short and long options. argparse module provides more robust parsing with features like positional arguments, default values, and help messages. It is generally better than the other options.

Uploaded by

Pawan Kumar
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
Download as pdf or txt
Download as pdf or txt
You are on page 1/ 6

Command Line Arguments in Python

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:
 Using sys.argv
 Using getopt module
 Using argparse module

Using sys.argv

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: Let’s suppose there is a Python script for adding two numbers and
the numbers are passed as command-line arguments.
 filter_none
 brightness_4
# Python program to demonstrate
# command line arguments

import sys

# total arguments
n = len(sys.argv)
print("Total arguments passed:", n)

# Arguments passed
print("\nName of Python script:", sys.argv[0])

print("\nArguments passed:", end = " ")


for i in range(1, n):
print(sys.argv[i], end = " ")

# Addition of numbers
Sum = 0
Using argparse module
for i in range(1, n):
Sum += int(sys.argv[i])

print("\n\nResult:", Sum)
 Output:

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])
Parameters:
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.
Example:
filter_none
brightness_4
# Python program to demonstrate

# command line arguments

import getopt, sys


# Remove 1st argument from the

# list of command line arguments

argumentList = sys.argv[1:]

# Options

options = "hmo:"

# Long options

long_options = ["Help", "My_file", "Output ="]

try:

# Parsing argument

arguments, values = getopt.getopt(argumentList, options, long_options)

# checking each argument

for currentArgument, currentValue in arguments:

if currentArgument in ("-h", "--Help"):

print ("Diplaying Help")

elif currentArgument in ("-m", "--My_file"):

print ("Displaying file_name:", sys.argv[0])

elif currentArgument in ("-o", "--Output"):

print (("Enabling special output mode (% s)") % (currentValue))

except getopt.error as err:

# output error, and return with an error code

print (str(err))
Output:

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 --help.
Example 1: Basic use of argparse module.
filter_none
brightness_4
# Python program to demonstrate

# command line arguments

import argparse

# Initialize parser

parser = argparse.ArgumentParser()

parser.parse_args()

Output:
Example 2: Adding description to the help message.
filter_none
brightness_4
# Python program to demonstrate

# command line arguments

import argparse

msg = "Adding description"

# Initialize parser

parser = argparse.ArgumentParser(description = msg)

parser.parse_args()

Output:
Example 3: Defining optional value
filter_none
brightness_4
# Python program to demonstrate
# command line arguments

import argparse

# Initialize parser
parser = argparse.ArgumentParser()

# Adding optional argument


parser.add_argument("-o", "--Output", help = "Show Output")

# Read arguments from command line


args = parser.parse_args()

if args.Output:
print("Diplaying Output as: % s" % args.Output)

You might also like