title: argparse: required argument conditioned by boolean
body: <p>What I'm trying to do is to call a python script with 2 arguments, let's say <code>--higher_level_optional_arg</code> and <code>--prepare_data</code>.</p>
  <p><code>--prepare_data</code> is a boolean defaulting to <code>False</code>.</p>
  <p>Now the thing I'd like to accomplish but that I cannot figure out is:</p>
  <p><strong>if</strong> <code>--prepare_data</code> is <code>False</code> then a string argument <code>--data_tag</code> (used to point to the data) is required, <strong>otherwise</strong> <code>--data_tag</code> is not required but you can pass one or more optional arguments for data preparation, e.g. <code>--prepare_data_arg</code>.</p>
  <p>I tried combining subparsers and mutually exclusive groups but I guess I'm too noob and I cannot make it work.</p>
  <p>Can you help me or point me in the right direction ?</p>
  <p><strong>EDIT :</strong>
  Better to explain with some code (my bad).</p>
  <p>Here's my parser definition:</p>
  <pre><code>if __name__ == &quot;__main__&quot;:
  
      parser = argparse.ArgumentParser(
          formatter_class=argparse.ArgumentDefaultsHelpFormatter,
      )
      parser.add_argument(
          &quot;--higher_level_optional_arg&quot;,
          default=5,
          type=int,
          help=&quot;some optional arg&quot;,
      )
      parser.add_argument(
          &quot;--prepare_data&quot;,
          dest=&quot;prepare_data&quot;,
          default=False,
          action=&quot;store_true&quot;,
          help=&quot;wether you want to prepare data or not&quot;
      )
      parser.add_argument(
          &quot;--data_tag&quot;,
          #required=True,
          type=str,
          help=&quot;if prepare_data is false, this should be required to retrieve existing data&quot;,
      )
      parser.add_argument(
          &quot;--prepare_data_arg&quot;,
          default=10,
          type=int,
          help=&quot;if prepare_data is true you can pass this if you want&quot;,
      )
  
      args = parser.parse_args()
      higher_level_optional_arg = args.higher_level_optional_arg
      prepare_data = args.prepare_data
      data_tag = args.data_tag
      prepare_data_arg = args.prepare_data_arg
  
      main()
  </code></pre>
  <p>Then, in <code>main</code>, as suggested by @hpaulj I can put the logic I need as:</p>
  <pre><code>def main():
      if not prepare_data:
          if not data_tag:
              raise Exception(&quot;you need to pass --data_tag to retrieve the existing data&quot;)
          else:
              # retrieve the data with the data_tag
              print(&quot;loading data...&quot;)
      else:
          # prepare the data using prepare_data_arg, if passed
          print(&quot;preparing data...&quot;)
  </code></pre>
  <p>But who's running the script doesn't know that <code>--data_tag</code> is required if <code>prepare_data==False</code> and s/he shouldn't receive an Exception for that, imo.</p>
  <p>(<strong>notice that <code>data_tag</code> is not required if <code>prepare_data==True</code> so I don't don't want to uncomment the <code>required=True</code></strong>)</p>
  <p>My reformulated question is: <strong>can I achieve what I'm doing here within the parser definition ?</strong></p>