
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Command Line and Variable Arguments in Python
Command Line Arguments
Command line arguments are input parameters which allow user to enable programs to act in a certain way like- to output additional information, or to read data from a specified source or to interpret the data in a desired format.
Python Command Line Arguments
Python provides many options to read command line arguments. The most common ways are -
- Python sys.argv
- Python getopt module
- Python argparse module
Python sys module
The sys module stores the command line arguments (CLA) into a list and to retrieve it, we use sys.argv. It’s a simple way to read command line arguments as string.
import sys print(type(sys.argv)) print('The command line arguments are: ') for i in sys.argv: print(i)
One running above programme, our output will be something like -
Output
>python CommandLineOption.py H E L L O <class 'list'> The command line arguments are: CommandLineOption.py H E L L O
Python getopt module
The python getopt module parses an argument sequence like sys.argv and returns a sequence of options & argument pairs and a sequence of non-option arguments.
Syntax
getopt.getopt(args, options, [long_options])
where:
Args – Argument list to be parsed
Options – This is the string of option letters that the script wants to recognize.
Long_options – This one is optional parameter, is a list of strings with the names of the long options, which should be supported.
The getopt module option syntax includes -
- -a
- -bval
- -b val
- --noarg
- --witharg=val
- --witharg val
Let’s understand the getopt module through an example -
Short form options
Below program takes two options, -a and –b with the second option requiring an argument.
import getopt print (getopt.getopt(['-a', '-bval', '-c', 'val'], 'ab:c:'))
Result
([('-a', ''), ('-b', 'val'), ('-c', 'val')], [])
Long form option
The program takes two options, --noarg and –witharg and the sequence should be [‘noarg’, ‘witharg=’]
import getopt print (getopt.getopt([ '--noarg', '--witharg', 'val', '--witharg2=another' ],'',[ 'noarg', 'witharg=', 'witharg2=' ]))
Result
([('--noarg', ''), ('--witharg', 'val'), ('--witharg2', 'another')], [])
Another example to demonstrate the usuage of geopt module -
import getopt import sys argv = sys.argv[1:] try: opts, args = getopt.getopt(argv, 'hm:d', ['help', 'my_file=']) print(opts) print(args) except getopt.GetoptError: print('Something Wrong!') sys.exit(2)
Result
>python CommandLineOption.py -m 2 -h "Help" H E L L O [('-m', '2'), ('-h', '')] ['Help', 'H', 'E', 'L', 'L', 'O']
Python argparse module
Python argparse module is the preferred way to parse command line arguments. This module provides several options like positional arguments, default value for arguments, help message, specifying data type of argument etc.
Below is a simple program to understand argparse module -
import argparse parser = argparse.ArgumentParser() parser.add_argument("string", help="Print the word in upper case letters") args = parser.parse_args() print(args.string.upper()) # This way argument can be manipulated.
Output
>python CommandLineOption.py "Hello, pythoN" HELLO, PYTHON
We can set optional argument with argparse module like –verbosity.