argparse option of options

2020-07-27 02:45发布

问题:

I am trying to add option of options in argparse. Currently I have:

group = parser.add_mutually_exclusive_group()
group.add_argument("--md", help="Create xyz file for each ionic step for"
                    " visualization", action='store_true')
group.add_argument("--force", help="See which atom has maximum force",
                    action='store_true')
group.add_argument("--opt", help="grep string from file",
                    nargs=2, metavar=("str", "file"))
parser.add_argument("--xsf", help="Create xsf file for md(default is xyz)"
                    " visualization", action='store_true')
parser.add_argument("-N", help="Showing first N line",
                    metavar='integer', type=int)
parser.add_argument("-n", help="Showing last n line",
                    metavar='integer', type=int)
args = parser.parse_args()

which gives:

./foo.py --h
usage: foo.py [-h]
               [--md | --force | --opt str file]
               [--xsf] [-N integer] [-n integer]

But I want --xsf as a suboption for --md, -N,-n for --opt; e.g.

./foo.py --h
    usage: foo.py [-h]
                   [--md [--xsf]| --force | --opt str file [-N integer] [-n integer]]

But I dont know how to achieve that. May be I am missing something, but there is no option like that in argparse doc

Is there any other way of getting that?

回答1:

The mutually_exclusive_group mechanism is quite simple, and does not work with any kind of nesting, or subgrouping.

There is a Python bug/issue requesting a more comprehensive grouping mechanism, but the proposed patch is rather complicated. The problem isn't just with testing, it's with defining the groups in a user friendly way, and with generating the usage line. It's nice that you included a desired usage, but that format is well beyond the capabilities of the current help formatter.

You might look into recasting your problem as a subparser one. subparsers are mutually exclusive (you can only give one command name), and you could specify --xsf as an argument for md, and -N as argument for --opt. But subparsers has its own help issues.

Another route is to write your own usage, and do your own testing of arguments after parsing. With a suitable choice of defaults you can usually tell whether an argument has been provided or not (the user can't specify None) or you can ignore unnecessary ones.