With argparse is it possible to have subparsers with dashes?

157 Views Asked by At

I want to specify "groups of arguments" like this:

fetch-something --basic-auth --user=me --password=myP4ss

OR

fetch-something --oauth --user=me --client=9038534.client --secret=aslu432409x

(similar to this question)

I thought I could create subparsers --basic-auth and --oauth but it looks like subparsers added like this

auths = parser.add_subparsers(help='authentication-method', metavar="METHOD")
oauth_parser = auths.add_parser('--oauth')

will not recognize --oauth (with the dashes).

Is there any way to have something like a subparser but with dashes? Or any other way to describe a behavior like described without having to specify all requirements by checking existence and non-existence of arguments manually?

E.g.

fetch-something --oauth --user=me --password=abc --client=9038534.client --secret=aslu432409x

should be invalid because password must not provided together with --oauth

Edit:

maybe this still looks like a first world problem to you but another reason you might want to have this is ordering:

fetch-something --secret=aslu432409x --oauth --user=me --client=9038534.client 

looks very strange since --secret is not visibly in scope of oauth any more but still valid.

Approach

One way one could probably go is to parse_known_args all (in this example) non-auth arguments and provide the rest to extra "auth-parsers" for basic-auth and oauth which might raise if not provided with the correct arguments. Creating a complete help text would be hard then though..

0

There are 0 best solutions below