Feb-01-2018, 05:15 PM
I'm going to suggest a slightly different approach. How about if you allowed options such as -f 1 2 3? Meaning, run function_1(), then function_2(), followed by function_3().
This can be done with a single add_argument like this:
The namespace object returned by parse_args() (cmd_arguments in your example) will have a member named f of type list. Now you can use if statements like:
This can be done with a single add_argument like this:
parser.add_argument("-f", nargs='+', default=[])nargs='+' means any number of args, but at least 1. You can also use nargs='*' to mean any number, including 0. Including default=[] is nice because then your code doesn't have keep testing first if cmd_arguments.f == None.
The namespace object returned by parse_args() (cmd_arguments in your example) will have a member named f of type list. Now you can use if statements like:
if 1 in cmd_arguments.f: function_1()This approach also gives great flexibility on not only which functions are to be run, but even what order: -f 3 1 2.