I have few methods in a class, and I want to allow the user to run their desired methods only by parsing the method name, but I'm stuck at the argparser code, would like to ask for advices here
Basically I want to accomplish something like below
python3 test.py -ip 10.10.10.100 -op method1 method2 method3 ...
in my code
class myClass(parentClass):
def __init__(self) -> None:
"""
Setup target system
"""
try:
super().__init__()
self.parser = argparse.ArgumentParser()
self.parser.add_argument('-ip', help='IP address of target system')
self.parser.add_argument('-op', help='User desired options')
self.args, unknown = self.parser.parse_known_args()
except Exception as exp:
print(f'Error: {exp}')
def method1(self):
pass
def method2(self):
pass
def run_prog(self):
# append user desired method into a list?
# loop through the list and execute each method
for i in the_list:
fn = getattr(self, i)
fn()
*I have to put argparse in __init__
to follow my org standardization
CodePudding user response:
What about adding the nargs=' ' option when declaring the -op
argument parser?
self.parser.add_argument('-op', help='User desired options', nargs=' ')