commands.py

``````ADD = 'add'
SUBTRACT = 'subtract'
MULTIPLY = 'multiply'
DIVISION = 'division'
INTERFACE_NIC = 'interface_nic'
``````

cli.py

``````#!/usr/bin/env python3

import argparse
import commands
import math
import socket
import netifaces as ni

def main_math():
parser = argparse.ArgumentParser()

# first command

# second command

# third command

# fourth command

# fifth command

# find interface command

args = parser.parse_args()
result = sum(args.numbers)
print(f"The sum of {args.numbers} is {result}")
print(f"")
elif args.command == commands.SUBTRACT:
first, *rest = args.numbers
result = first - sum(rest)
print(f"The subtraction of {args.numbers} is {result}")
print(f"")
elif args.command == commands.MULTIPLY:
result = 1
for i in args.numbers:
result *= i
print(f"The multiplication of {args.numbers} is {result}")
print(f"")
elif args.command == commands.DIVISION:
set_first = 0
for i in args.numbers:
if set_first == 1:
result /= i
# sets first number to results
elif set_first == 0:
result = i
set_first = 1
print(f"The division of {args.numbers} is {result}")
print(f"")
a = args.numbers[0]
b = args.numbers[1]
c = args.numbers[2]
dis = b * b - 4 * a * c
sqrt_val = math.sqrt(abs(dis))

# checking condition for discriminant
if dis > 0:
print(f"The roots of {a} {b} {c} are")
print(" real and different roots ")
print((-b + sqrt_val) / (2 * a))
print((-b - sqrt_val) / (2 * a))
print(f"")
elif dis == 0:
print(f"The roots of {a} {b} {c} are")
print(" real and same roots")
print(-b / (2 * a))
print(f"")
# when discriminant is less than 0
else:
print(f"The roots of {a} {b} {c} are")
print("Complex Roots")
print(- b / (2 * a), " + i", "{:.4f}".format(sqrt_val))
print(- b / (2 * a), " - i", "{:.4f}".format(sqrt_val))
print(f"")

elif args.command == commands.INTERFACE_NIC:
# Find network interfaces
host_name = socket.gethostname()
print(f"my host name is {host_name}")
if i[0] != 1:
int_nic = ''.join(i[1])
print(f"IP Address of interface {int_nic} is {ip}")

i = 0
if i == 4:
i += 1
len_int_ipv6 = len(int_ipv6)
print(f"IPv6 Address of interface {int_nic} is {int_ipv6[3:(len_int_ipv6-1)]}")

else:
parser.print_help()

if __name__ == "__main__":
main_math()
``````

runbatch.txt

``````python3 cli.py add 1 2 3
python3 cli.py subtract 1 2 3
python3 cli.py multiply 1 2 3
python3 cli.py division 1 2 3
python3 cli.py quadratic 1 2 3
python3 cli.py quadratic 1 -2 1
python3 cli.py quadratic 1 7 12
python3 cli.py interface_nic

``````
``````./runbatch.txt
The sum of [1, 2, 3] is 6

The subtraction of [1, 2, 3] is -4

The multiplication of [1, 2, 3] is 6

The division of [1, 2, 3] is 0.16666666666666666

The roots of 1 2 3 are
Complex Roots
-1.0  + i 2.8284
-1.0  - i 2.8284

The roots of 1 -2 1 are
real and same roots
1.0

The roots of 1 7 12 are
real and different roots
-3.0
-4.0

my host name is programmer2004
IP Address of interface ens32 is 192.168.1.30
IPv6 Address of interface ens32 is 2600:1702:980:25ef:e192:168:1:30
michael@programmer2004:~/PycharmProjects/CommandLineInterface\$

``````

I have: