Hook on arguments in argparse python
Question: I am interested in hook extra arguments parsed using argparse in one class to
another method in another class which already has few arguments parsed using
argparse module.

    
    
    Project 1
    def x():
        parser = argparse.ArgumentParser()
        parser.add_argument('--abc')
    
    Project 2
    def y():
         parser = argparse.ArgumentParser()
         parser.add_argument('--temp1')
         parser.add_argument('--temp2')
    

When I run x(), I want to add the "--abc" argument to the list of argument y()
has which is "temp1", "temp2" at runtime. Is inheritance the best way to go
and defining the constructors accordingly ? Could someone provide some sample
code snippet ?

Thanks !


Answer: You can inspire yourself from Django's [management
commands](https://github.com/django/django/blob/master/django/core/management/base.py#L116).
They are basically setup as follow:

  * The entry point is `run_from_argv` which calls `create_parser`, parse the command line, extract the parsed arguments and provide them to `execute`;
  * The `create_parser` method creates an argparse parser and uses `add_argument` to prepopulate default options available for all commands. This function then calls the `add_arguments` method of the class which is meant to be overloaded by subclasses;
  * The `execute` method is responsible to handle the various behaviours associated to the default options. It then calls `handle` which is meant to be overloaded by subclasses to handle the specific options introduced by `add_arguments`.

Your requirements are not completely clear but I think that in your case you
don't need to bother with an execute method. I’d go with:

    
    
    import argparse
    import sys
    
    class BaseParser:
        def create_parser(self, progname):
            parser = argparse.ArgumentParser(prog=progname)
            parser.add_argument('--temp1')
            parser.add_argument('--temp2')
            self.add_arguments(parser)
            return parser
    
        def add_arguments(self, parser):
            pass # to be optionnally defined in subclasses
    
        def parse_command_line(self, argv):
            parser = create_parser(argv[0])
            options = parser.parse_args(argv[1:])
            parsed_options = vars(options)
            self.handle(**parsed_options) # HAS TO be defined in subclasses
    
    class X(BaseParser):
        def add_arguments(self, parser):
            parser.add_argument('--abc')
    
        def handle(self, **options):
            abc = options['abc']
            temp1 = options['temp1']
            temp2 = options['temp2']
            # do stuff with thoses variables
    
    class Y(BaseParser):
        def handle(self, **options):
            temp1 = options['temp1']
            temp2 = options['temp2']
            # do stuff
    
    x = X()
    y = Y()
    args = sys.argv
    x.parse_command_line(args)
    y.parse_command_line(args)
    

You could simplify the code further if X is a subclass of Y.

