Here is a very simple example of using argparse options to trigger specific functions.
After adding the -w
and -s
as arguments, you can test the returned Namespace object to see which arguments were found and call the appropriate functions accordingly.
main.py
import argparse
def sfunc():
print("-s argument was found")
def wfunc():
print("-w argument was found")
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("-w", action="store_true")
parser.add_argument("-s", action="store_true")
ns = parser.parse_args()
if ns.s:
sfunc()
if ns.w:
wfunc()
Here is what the results are from using each of the arguments
/>python main.py -s
-s argument was found
/>python main.py -w
-w argument was found
/>python main.py -w -s
-s argument was found
-w argument was found
There are many other ways to achieve these same results, this is just a very simple example.