-2

So I have created my argparse which has two different flags. One is -a and the other one is -b. When I run my script damage.py with a specific flag, I want it to be able to execute a function depending on what flag is passed. For example if I pass damage.py -t, it will run the function tester() as shown in my import and print hello, where as if I pass -d it will run another function. So far my code is as follows:

import argparse


def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("-a", "--export-date", action="store_true", required=True)
    parser.add_argument("-b", "--execute-test", action="store_true", required=False)


if __name__ == '__main__':
    main()
  • 1
    to me you are doing in correct way – Amit Nanaware Jan 18 '19 at 08:45
  • Is there a more better way of doing this @AmitNanaware – junior pydev Jan 18 '19 at 08:49
  • 1
    You should really reproduce your indentation properly when posting Python code. What you've posted here will not run. – khelwood Jan 18 '19 at 08:51
  • Sorry about that. I will fix this. I seem to be having an issue. Since i have imported my python script at the top , it seems to run that first. Am not sure why that is happening @khelwood – junior pydev Jan 18 '19 at 08:55
  • It's impossible to answer any of your questions if you don't post properly indented code - in Python indentation IS PART OF THE SYNTAX so improperly indented code makes no sense at all, neither to the python parser nor to us human readers. If you hope to get sensible answers, post properly indented code that can be run. Also, reduce your code to the minimum necessary to reproduce the issue, cf https://stackoverflow.com/help/mcve. – bruno desthuilliers Jan 18 '19 at 09:39
  • updated @brunodesthuilliers – junior pydev Jan 18 '19 at 09:42
  • Updated what ? Your code snippets are still as broken as they were and you still haven't reduced your code to the minimal necessary to reproduce the issue. – bruno desthuilliers Jan 18 '19 at 10:06
  • Can you please take a look now @brunodesthuilliers – junior pydev Jan 18 '19 at 10:10
  • I'm afraid you didn't read my comments nor the link I posted. Your code is still neither minimal nor complete nor verifiable. – bruno desthuilliers Jan 18 '19 at 10:13
  • 1
    Developing new code on Python 2 seems like an increasingly poor idea. By the original timeline, version 2 was supposed to be dead already, and the RIP date is now set to next year. You should be thinking about migrating your code to the currently recommended and properly supported version of the language, which is Python 3. – tripleee Jan 18 '19 at 10:14
  • right ok , for now i shall finish off what i need to do and then see what i need to do in order to migrate to python 3 – junior pydev Jan 18 '19 at 10:16

1 Answers1

0

Rather than saving these values to a variable first you can access them directly like this:

if args.export_date:
    # Do something with date

if args.execute_test:
    tester()

This means that when you run your program like python damage.py -dt it will run both the code in the date block as in the tester block.

Ghorich
  • 428
  • 1
  • 4
  • 13
  • So if i run python damage.py -d , it will only run code in the args.export_date block ? – junior pydev Jan 18 '19 at 08:57
  • so for the block args.execute_test i am calling the function tester() which is from from test_compare_filesets import tester as imptd_tester. As soon as i import my script , it seems to run that code first regardless of if pass -d or -t – junior pydev Jan 18 '19 at 08:59
  • Do you have some kind of `__init__` in that code that may be run when it is imported? – Ghorich Jan 18 '19 at 09:03
  • I have posted my code above if you take a look at test_compare_filesets.py – junior pydev Jan 18 '19 at 09:08