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



On Thu, Sep 10, 2020 at 7:25 PM Guido van Rossum <gu...@python.org> wrote:

> Rangarajan,
>
> You may be on to something but the description is a bit dense.
>
> Can you show some examples of how this would work, and contrast those with
> how the same thing would have to be done without your proposed feature?
>
> —Guido
>
> On Thu, Sep 10, 2020 at 05:47 <rangap...@gmail.com> wrote:
>
>> Hi,
>>
>> Got the argparse() in version 3.8.2 and above the action=CusotmClass can
>> have a user defined custom subclass and override the functionality in the
>> def __init__() method. Along with this can we also have a built in custom
>> method/methods that I can define under this custom Class, so that instead
>> of calling the Action itself I have the option of calling the
>> Action.Method() during the add_argument defintition.
>>
>>
>>
>> Regards
>>
>> Ranga
>>
>> _______________________________________________
>>
>> Python-ideas mailing list -- python-ideas@python.org
>>
>> To unsubscribe send an email to python-ideas-le...@python.org
>>
>> https://mail.python.org/mailman3/lists/python-ideas.python.org/
>>
>> Message archived at
>> https://mail.python.org/archives/list/python-ideas@python.org/message/JBVLH244IKOVLHFBZKC7DDMB6QT2T5QQ/
>>
>> Code of Conduct: http://python.org/psf/codeofconduct/
>>
>> --
> --Guido (mobile)
>
_______________________________________________
Python-ideas mailing list -- python-ideas@python.org
To unsubscribe send an email to python-ideas-le...@python.org
https://mail.python.org/mailman3/lists/python-ideas.python.org/
Message archived at 
https://mail.python.org/archives/list/python-ideas@python.org/message/3YUZG2UI6L6KPHYKBWRDXSL3T5C3EWAB/
Code of Conduct: http://python.org/psf/codeofconduct/

Reply via email to