0

I have multiple function

 def function1(x,y,z):
    do something 

  def function2():
    do something 

I want call the function1 as the default function while I execute it from the cmd. I tried this following example this

parser = argparse.ArgumentParser(description='Generating report for Multikey usage')

parser.add_argument('--function1',dest='action', action='store_const', const=function1)
parser.add_argument('--a',type=str,help=' enter the association Id ')
parser.add_argument('--d',type=int,help=' enter the number of days from today ')
parser.add_argument('--e',type=str,help=' enter the receivers email ')

args = parser.parse_args()
x,y,z  = args.a, args.d , args.e

But it's not working. What I am doing wrong? Is there a way i dont have to specify the function name from cmd. Example, From the cmd I want to give I/O as

python Multikey_report_1.py  --a AS000000008S --d 200 --e blah@gmail.com
No_body
  • 832
  • 6
  • 21

1 Answers1

1

if you calling your script with

 python Multikey_report_1.py  --a AS000000008S --d 200 --e blah@gmail.com

then this line

parser.add_argument('--function1',dest='action', action='store_const', const=function1)

is not beign run. add --function1 to your arguments and it should work, that's how the example you've cited does it

 python Multikey_report_1.py --function1 --a AS000000008S --d 200 --e blah@gmail.com

This will then store the value of function1 into an args variable called action so you'll also need to make sure that this block is in your code too which will run function1

parsed_args.action(x,y,z)
bain2236
  • 111
  • 1
  • 9
  • Ahh working now.I was putting parsed_args.action(parsed_args) instead of parsed_args.action(x,y,z). How can I eliminated the need of putting the function name in CMD – No_body Jul 18 '19 at 13:54
  • Figured out. parser.add_argument(dest='action', action='store_const', const=function1) will just work fine. Thanks man for putting time. – No_body Jul 18 '19 at 13:56