user4093955
user4093955

Reputation:

Is there a solution for required mutually exclusive arguments listed as optional in help section?

So using argparse I created a mutually exclusive group which has two items. One of the must be always passed, so I created the group with required=True.

It's properly working, If I don't call the script with either of them, it will fail with error: one of the arguments --foo --bar is required

However, the problem comes when I just run it with -h or --help. It's listing these parameters as optional, but they are not.

optional arguments:
  -h, --help            show this help message and exit
  --foo                 foo
  --bar                 bar

required arguments:
  --alice               alice

Is there any solution to list them as required? As add_mutually_exclusive_group() does not support the title parameter, I cannot do something like add_mutually_exclusive_group('must pick one', required=True)

Upvotes: 5

Views: 1889

Answers (1)

mehdix
mehdix

Reputation: 5164

This is an open issue in python's issue tracker, however there is a simple workaround for it.

Simply create a titled group and add your mutually exclusive group to that one:

parser = argparse.ArgumentParser()
g1 = parser.add_argument_group(title='Foo Bar Group', description='One of these options must be chosen.')
g2 = g1.add_mutually_exclusive_group(required=True)
g2.add_argument('--foo',help='Foo help')
g2.add_argument('--bar',help='Bar help')

Courtesy of Paul.

Upvotes: 8

Related Questions