vendredi 18 janvier 2019

How to use conditional statements with argparse in Python

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 os
import argparse
import functions as funcs
from test_compare_filesets import tester as imptd_tester


def main():

 parser = argparse.ArgumentParser()
 parser.add_argument("-d", "--export-date", action="store_true", required=True)
 parser.add_argument("-t", "--execute-test", action="store_true", required=False)
 args = parser.parse_args()
 date = args.export_date
 testt = args.execute_test

if date :
   tester()
else :

print("Die")

if __name__ == '__main__':

main()

Aucun commentaire:

Enregistrer un commentaire