Argparse non mutually exclusive group within a mutually exclusive group

Viewed 121

I'm trying to accomplish something like

[-a] [-b | [[-c | -d] [-e]]]

In code, I thought it would look something like

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-a')
meg = parser.add_mutually_exclusive_group(required=False)
meg.add_argument('-b')
megg = meg.add_argument_group() # <-- The issue is here. I did not realize that this option was purely for visual purposes.
meggmeg = megg.add_mutually_exclusive_group(required=False)
meggmeg.add_argument('-c')
meggmeg.add_argument('-d')
megg.add_argument('-e')
parser.parse_args()

however, what actually ends up being generated is

[-a] [-b] [-c | -d] [-e]

This output is strange because even if add_argument_group() was purely for visual purposes, the output should still end up being

[-a] [-b | [-c | -d] | -e]

Even stranger still is the behaviour of the following code if I were to replace add_argument_group() with add_mutually_exclusive_group()

import argparse
parser = argparse.ArgumentParser()
parser.add_argument('-a')
meg = parser.add_mutually_exclusive_group(required=False)
meg.add_argument('-b')
megg = meg.add_mutually_exclusive_group()
meggmeg = megg.add_mutually_exclusive_group(required=False)
meggmeg.add_argument('-c')
meggmeg.add_argument('-d')
megg.add_argument('-e')
parser.parse_args()

What I would expect is the following (which is not the behaviour that I want, as I don't want the final mutual exclusion, but it is close)

[-a] [-b | [[-c | -d] | -e]]

but the actual output that I get is

[-a] [-b | [[-c | -d |] -e]]

and if I were to add a megg.add_argument('-f'), then the output becomes

[-a] [-b | [[-c | -d |] -e | -f]]

which is just very strange.

0 Answers
Related