ConfigArgParse icon indicating copy to clipboard operation
ConfigArgParse copied to clipboard

How to import existing ArgParse into ConfigArgParse

Open astariul opened this issue 4 years ago • 1 comments

I'm using an external library, which use ArgParse.

I would like to use ConfigParse, in order to give a config file to ArgParse.

Something like :

import lib

parser = lib.get_parser()     # Can't modify the content of this function
# Do something here to add my --config option to the existing parser
args = parser.parse_args()

astariul avatar Nov 29 '19 02:11 astariul

I end up using a dirty work-around :

import lib
import configargparse

parser = lib.get_parser()

p = configargparse.ArgParser()
parser.__class__ = configargparse.ArgParser
for attribute in dir(p):
   if attribute not in dir(parser):
        setattr(parser, attribute, getattr(p, attribute))
parser.add("--config", is_config_file=True, help="Configuration file path.")

args = parser.parse_args()

As you see, it's dirty but it works.

I'm looking for a cleaner way to achieve this.

astariul avatar Nov 29 '19 02:11 astariul