0

I want to use argparse to get option to my program. Here is my sample program:

from argparse import ArgumentParser

parser = ArgumentParser()

print("enter the numbers:")

a=int(input("number 1:"))

b=int(input("number 2:"))

parser.add_argument('-a','--add')

parser.add_argument('-s','--sub')

options = parser.parse_args()

if options:

        c=a+b

if options.d:

        c=a-b

print(c)

it gives the output correctly if I use

python file.pu -a 1

But I don't want to give value like 1 in compilation. What I want is

python file.py -a     

that performs addition.

python file.py -s

that performs subtraction.

How to change the code for it?

jonrsharpe
  • 115,751
  • 26
  • 228
  • 437
joker
  • 11
  • 3

1 Answers1

1

You can use:

action='store_true'

...in the following:

parser.add_argument('-a', '-add', action='store_true')
Adam
  • 709
  • 4
  • 16