In Python we can do any programming either console program or GUI programming. we can also take inputs from command line directly without halting the execution of the program. taking the input from command line using argument is called "Command line argument".
In Python we can achieve using "argparse" module.
How to install argparse?
argparse module is a inbuilt module in every Python version. (Linux users please refer how to install argparse module.)
How to add command line argument using argparse?
Adding argument to Python program is much more easier using argparse. before going to the actual explanation of the classes and functions, let us see one small example:
import argparse
class Parser( object ):
def __init__ (self):
self.parser = argparse.ArgumentParser( description = 'This is for Test to run program\nProcess some integers')
self.setParseArg()
pass
def setParseArg(self):
a = 1
self.parser.add_argument('-s', action='store', dest='simple_value',
help='Store a simple value')
self.parser.add_argument('-c','--const', action='store_const', dest='constant_value',
const='value-to-store',
help='Store a constant value')
self.parser.add_argument('-v','--version', action='version', version='%(prog)s 1.0')
results = self.parser.parse_args()
print 'constant_value =', results.constant_value
print 'simple_value =', results.simple_value
class Parser( object ):
def __init__ (self):
self.parser = argparse.ArgumentParser( description = 'This is for Test to run program\nProcess some integers')
self.setParseArg()
pass
def setParseArg(self):
a = 1
self.parser.add_argument('-s', action='store', dest='simple_value',
help='Store a simple value')
self.parser.add_argument('-c','--const', action='store_const', dest='constant_value',
const='value-to-store',
help='Store a constant value')
self.parser.add_argument('-v','--version', action='version', version='%(prog)s 1.0')
results = self.parser.parse_args()
print 'constant_value =', results.constant_value
print 'simple_value =', results.simple_value
if __name__ == "__main__":
parser = Parser()
parser.parser.print_help()
In above example, we have created a parser called "self.parser" which is instance reference variable of ArgumentParser which is available in argparse module.
ArgumentParser is a class which is used to create a parser for reading command line argument with the help of options.
self.parser = argparse.ArgumentParser( description = 'This is for Test to run program\nProcess some integers')
In ArgumentParser class we have lot of parameter like 'description','help' etc. for more information, please refer https://docs.python.org/2.7/library/argparse.html
This argparser instance is used to add argument using "add_argument" method which is available in "ArgumentParser" class.
self.parser.add_argument('-c','--const', action='store_const', dest='constant_value',
const='value-to-store',
help='Store a constant value')
const='value-to-store',
help='Store a constant value')
Using add_argument we can add argument to command line as mentioned above.
we can add '-c' as a short argument or '--const' as long argument to command line. where,
action -- is to specify what it has to store in provided destination
destination -- destination of the given argument where it has to store in program
type -- type of the variable ex: int, float
help -- help for the given argument or how to use this argument.
actions:
argparse has many actions:
store_const: which is used to store only constant to given parameter from command line argument.
giving value which is not constant will give exception and exit the program with warnings
store_true: It is used to store only true value
store_false: used to store false.
store: used to store any actions.
append - This stores a list, and appends each argument value to the
list
version- used to take version of the program and exit the program
let us consider that above program named as test.py
when you execute the program like
>>> python test.py -c 4
ouput will be:
constant_value = 4
simple_value = None
simple_value = None
If you want to take more than one value to single argument as list, then use nargs and give input to argument with space
for more information about nargs please refer official documents.