Setting up Python's ArgumentParser with two mutually excluding flags where one flag has optional additional flags
<p>I would like to define the following using Python's ArgumentParser:</p>

<pre><code>--mutually_exclusive_flag_A stringParameter

--mutually_exclusive_flag_B stringParameter
    --optional_b_flag_one
    --optional_b_flag_two
</code></pre>

<p>One can use either mutually_exclusive_flag_A or mutually_exclusive_flag_B, but not both.</p>

<p>If one uses mutually_exclusive_flag_B, then one can use optional_b_flag_one and optional_b_flag_two. </p>

<p>optional_b_flag_one and optional_b_flag_two are boolean flags.</p>

<p>I do see add_mutally_exclusive_group to handle selecting mutually_exclusive_flag_A or mutually_exclusive_flag_B. However, what I am not sure how to do is declare that if I use mutually_exclusive_flag_B, then optional_b_flag_one and optional_b_flag_two are valid flags.</p>

<p>It seems like I may be able to use the subparsers feature and turn mutually_exclusive_flag_A and mutually_exclusive_flag_B into commands.</p>

<p>What is my best option?</p>

<p><code>argparse</code> can't handle that complex of a test.  Mutually exclusive groups can't be nested, and they don't handle other kinds of logic (only <code>xor</code>, not <code>and</code> and <code>or</code>).  I've explored such an expansion in a Python bug/issue, but it's not a trivial addition.</p>

<p>The best choice is to do your own testing after parsing.</p>

<p>The primary purpose of <code>argparse</code> is to figure out what your user wants.  Checking validity, and issuing a nice error message is a plus, but not central to the parsing task.</p>

<p>One of the problems with expanding this mechanism is write a meaningful usage message for general combinations.  Have you thought about how you'd explain this requirement to your users?</p>

python python-3.x argparse