Hi Guido,
Great to hear back from you.
Currently we have the add_argument calling the custom action like shown below...
Example:
class MyAction(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
super(MyAction, self).__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
print('%r %r %r' % (namespace, values, option_string))
t1 = subprocess.run("docker container ls", capture_output=True, shell=True, text=True, check=True)
l = []
for line in t1.stdout.splitlines():
r = re.findall(r'\S+',line)
l.append(r[0])
values = l
setattr(namespace, self.dest, values)
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='A python code to display Docker stats', epilog='Hope you like this program')
parser.add_argument('-s', action=MyAction)
args = parser.parse_args()
print(args)
Now in addition to calling the class I want to have a subfunction (may be a few methods ) under the same custom Actionclass which I want to execute by calling them in the action=MyAction.Method1/method2/Method3...
instead of calling the custom ClassAction itself. This way I don't have to create multiple CustomActionClasses, just the multiple
methods under same CustomAction Class
Currently the way function(or Methods) can be called is by specifying them in
1. type=Class.Method (since type can take a callable)
2. action=CustomAction(with modified _call_ in the CutomClass subclass of argparse.Action)
and
3. also via sub_parser=(func=custom method)
eg: subparser.set_defaults(func=Method1)
So my suggestion is if there are ways of creating a CustomActionClass with modified __call__ methods then why
not have additional methods that a user can define in those CustomAction subclass and be called in the action.
So my code becomes readable based on different classes and different sub methods and debugging becomes convenient
and the code looks elegant. So my modified CustomActionClass will look similar to below...
class MyAction(argparse.Action):
def __init__(self, option_strings, dest, nargs=None, **kwargs):
if nargs is not None:
raise ValueError("nargs not allowed")
super(MyAction, self).__init__(option_strings, dest, **kwargs)
def __call__(self, parser, namespace, values, option_string=None):
print('%r %r %r' % (namespace, values, option_string))
def Method1(self, parser, namespace, values, option_string=None):
t1 = subprocess.run("docker container ls", capture_output=True, shell=True, text=True, check=True)
l = []
for line in t1.stdout.splitlines():
r = re.findall(r'\S+',line)
l.append(r[0])
values = l
setattr(namespace, self.dest, values)
def Method1(self, parser, namespace, values, option_string=None):
DO Something
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='A python code to display Docker stats', epilog='Hope you like this program')
parser.add_argument('-s', action=MyAction.Method1)
parser.add_argument('-t', action=MyAction.Method2)
parser.add_argument('-b', action=MyAction2.Method1)
args = parser.parse_args()
print(args)
Thanks and Regards
Rangaswamy