13. CLA
Example
1 #To display CLA
2
3 import sys
4
5 #Get the no. of CLA
6n = len(sys.argv)
7
8 #Get the arguments
9args = sys.argv
10
11 #Print the 'n'
12print("No. Of CLA: ", n)
13
14 #print the arguments in one shot
15print(args)
16
17 #Print the arguments one by one
18 for i in args:
19 print(i)
14. CLA
Parsing CLA
● argparse module is useful to develop user-friendly programs
This module automatically generates help and usage messages
May also display appropriate error messages
●
●
15. CLA
Parsing CLA: Steps
● Step-1: Import argparse module
import argparse
●
●
Step-2: Create an Object of ArgumentParser
parser = argparse.ArgumentParser(description="This program displays square of two numbers")
Step-2a: If programmer does not want to display description, then above step
can be skipped
parser = argparse.ArgumentParser()
● Step-3: Add the arguments to the parser
parser.add_argument("num", type=int, help="Enter only int number.")
args = parser.parse_args()
● Step-4: Retrieve the arguments
● Step-5: Access the arguments
args.num