I'm developing an environment for experiments. Each experiment is a separate class has a different arguments required. The concept is to have a set of arguments that are related to the whole experimenting environment and a set of arguments imported from an experiment class. The question is: how to update possible arguments accordingly to chosen experiment type.
How i would love to make it basically looks like:
Note: EXPERIMENTS is a dict with experiment classes mapped by their names
main.py
def main(args):
experiment()
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("experiment_name",
metavar='experiment_name',
type=str,
choices=EXPERIMENTS.keys(), #exported list of available experiments
help="Choose an experiment to run. Available: "+str(list(EXPERIMENTS.keys())),
)
args = parser.parse_args()
EXPERIMENTS[args.experiment_name].add_args(parser)
experiment = EXPERIMENTS[args.experiment_name]()
sys.exit(main(args) or 0)
experimentA.py
class ExperimentA(Experiment):
@classmethod
def add_args(cls, parser):
parser.add_argument('--expA-arg')
cls.args = parser.parse_args()
def __init__(self):
super().__init__()
self.args = self.__class__.args
self.param = self.args.expA_arg
def __call__(self):
run_the_experiment_A()
experimentB.py
class ExperimentB(Experiment):
@classmethod
def add_args(cls, parser):
parser.add_argument('--expB-arg')
cls.args = parser.parse_args()
def __init__(self):
super().__init__()
self.args = self.__class__.args
self.param = self.args.expB_arg
def __call__(self):
run_the_experiment_B()
And how to achieve it to see those results after calling from shell:
case 1
IN:
python3 main.py -h
OUT:
positional arguments:
experiment_name Choose an experiment to run. Available: ['ExperimentA', 'ExperimentB']
optional arguments:
-h, --help show this help message and exit
case 2
IN:
python3 main.py ExperimentA -h
OUT:
positional arguments:
experiment_name Choose an experiment to run. Available: ['ExperimentA', 'ExperimentB']
optional arguments:
-h, --help show this help message and exit
--expA-arg
case 3
IN:
python3 main.py ExperimentB -h
OUT:
positional arguments:
experiment_name Choose an experiment to run. Available: ['ExperimentA', 'ExperimentB']
optional arguments:
-h, --help show this help message and exit
--expB-arg