Python Program for Key Value pair using argparse

How to have arguments as key and value pair in python program?

argparse module can be imported in python program to have the arguments as key and value pair.

Arguments as key and value pair for python program

Python program gets input values and operation type as python arguments as key and value pair using argparse module. Performs arithmetical operations and returns the result.

import argparse                                                                 
import sys                                                                      
                                                                                
# Gets the user input as dictionary.                                            
def parse_opts(argv):                                                           
    parser = argparse.ArgumentParser(                                           
        description='Interactive tool')                                         
    parser.add_argument('-a', '--val1',                                         
                        metavar='VALUE 1',                                      
                        help="""value 1.""",                                    
                        default='0')                                            
    parser.add_argument('-b', '--val2',                                         
                        metavar='VALUE 2',                                      
                        help="""value 2.""",                                    
                        default='0')                                            
    parser.add_argument('-o', '--operation',                                    
                        metavar='ARITHMETIC OPERATION',                         
                        help="""arith operation.""",                            
                        default='0')                                            
    opts = parser.parse_args(argv[1:])                                          
    return opts                                                                 
                                                                                
def arith_operations(a, b, op):                                                 
    if op == '+':                                                               
        return float(a) + float(b)                                              
    elif op == '-':                                                             
        return float(a) - float(b)                                              
    elif op == '*':                                                             
        return float(a) * float(b)                                              
    elif op == '/':                                                             
        return float(a) / float(b)                                              
    return 0                                                                    
                                                                                
if __name__ == '__main__':                                                      
    opts = parse_opts(sys.argv)                                                 
    result = arith_operations(opts.val1, opts.val2, opts.operation)             
    print('result (' + opts.val1 + ' '+ opts.operation + ' ' + opts.val2 + ')' + ': ' + str(result))                                        
                                

Output:
$ python arith_operations.py --val1 10 --val2 5 --operation '+'
result (10 + 5): 15.0
$ python arith_operations.py --val1 10 --val2 5 --operation '-'
result (10 - 5): 5.0
$ python arith_operations.py --val1 10 --val2 5 --operation '*'
result (10 * 5): 50.0
$ python arith_operations.py --val1 10 --val2 5 --operation '/'
result (10 / 5): 2.0

Privacy Policy  |  Copyrightcopyright symbol2020 - All Rights Reserved.  |  Contact us   |  Report website issues in Github   |  Facebook page   |  Google+ page

Email Facebook Google LinkedIn Twitter
^