# importing required modules
import argparse
# create a parser object
parser = argparse.ArgumentParser(description = "An addition program")
# add argument
parser.add_argument("add", nargs = '*', metavar = "num", type = int,
help = "All the numbers separated by spaces will be added.")
# parse the arguments from standard input
args = parser.parse_args()
# check if add argument has any input data.
# If it has, then print sum of the given numbers
if len(args.add) != 0:
print(sum(args.add))
import argparse
import sys
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--x', type=float, default=1.0,
help='What is the first number?')
parser.add_argument('--y', type=float, default=1.0,
help='What is the second number?')
parser.add_argument('--operation', type=str, default='add',
help='What operation? Can choose add, sub, mul, or div')
args = parser.parse_args()
sys.stdout.write(str(calc(args)))
def calc(args):
if args.operation == 'add':
return args.x + args.y
elif args.operation == 'sub':
return args.x - args.y
elif args.operation == 'mul':
return args.x * args.y
elif args.operation == 'div':
return args.x / args.y
if __name__ == '__main__':
main()