In the main function, I have a parser which validates optional inputs:
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--platform',required=True)
parser.add_argument('--foo')
parser.add_argument('--bar')
parser.add_argument('--baz')
parser.parse_args()
The above snippet is an example which only works when I supply --platform and either --foo,--bar or --baz.
This code is used by different components, let's call them components A, B and C.
Component A actually only specifies --foo and --bar:
python script.py --platform A --foo first_example --bar first_example
Component B actually only specifies --bar and --baz:
python script.py --platform B --bar second_example --baz second_exmaple
Component C actually only specifies --baz:
python script.py --platform C --baz third_example
As I introduce more components, which provide different arguments, the number of arguments I have to add to the parser increases. The above is just an example and I am currently dealing with 20 or so arguments (will likely be more in the future).
I have been thinking about having a configuration file (.yaml) where I define which arguments each component needs:
# parameters.yaml
A:
- foo
- bar
B:
- bar
- baz
C:
- baz
I would like to simplify the main function to look at the --platform argument and, based on which platform has been passed as argument, read the configuration and add additional arguments to the parser.
Here's what I have tried:
def main():
parser = argparse.ArgumentParser()
parser.add_argument('--platform',required=True)
# Read from .yaml file
with open('parameters.yaml') as parameter_file:
parameters = yaml.safe_load(parameter_file)
for argument in parameters[sys.argv[sys.argv.index('--platform') + 1]]:
parser.add_argument(
'--' + argument
)
parser.parse_args()
Calling the function:
python script.py --platform C --baz third_example
the above code works but I am looking for other Pythonic solutions as I am a beginner with Python. I don't really like having to look at sys.argv to determine what --platform has been specified. Are there any better solutions to this problem?
argparse'subparsers.