title: Pythonic way for adding parameters to argparse based on another parameter
body: <p>In the main function, I have a parser which validates optional inputs:</p>
  <pre><code>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()
  </code></pre>
  <p>The above snippet is an example which only works when I supply <em>--platform</em> and either <em>--foo</em>,<em>--bar</em> or <em>--baz</em>.</p>
  <p>This code is used by different components, let's call them components <strong>A</strong>, <strong>B</strong> and <strong>C</strong>.</p>
  <p>Component <strong>A</strong> actually only specifies <em>--foo</em> and <em>--bar</em>:</p>
  <pre><code>python script.py --platform A --foo first_example --bar first_example
  </code></pre>
  <p>Component <strong>B</strong> actually only specifies <em>--bar</em> and <em>--baz</em>:</p>
  <pre><code>python script.py --platform B --bar second_example --baz second_exmaple
  </code></pre>
  <p>Component <strong>C</strong> actually only specifies <em>--baz</em>:</p>
  <pre><code>python script.py --platform C --baz third_example
  </code></pre>
  <p>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).</p>
  <p>I have been thinking about having a configuration file (.yaml) where I define which arguments each component needs:</p>
  <pre><code># parameters.yaml
  A:
    - foo
    - bar
  
  B:
    - bar
    - baz
  
  C:
    - baz
  </code></pre>
  <p>I would like to simplify the main function to look at the <em>--platform</em>  argument and, based on which platform has been passed as argument, read the configuration and add additional arguments to the parser.</p>
  <p>Here's what I have tried:</p>
  <pre><code>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()
  </code></pre>
  <p>Calling the function:</p>
  <pre><code>python script.py --platform C --baz third_example
  </code></pre>
  <p>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 <strong>sys.argv</strong> to determine what <em>--platform</em> has been specified. Are there any better solutions to this problem?</p>