Chapter XIV: application building blocks -argparse: command line options and arguments parsing - parser Organization - Conflicting options

14.1.6.2 Conflicting options
the previous example pointed out that when using the same parameter name two additional parameters to a processor parser, will result in an exception. You can pass a conflict_handler conflict resolution to change the behavior. There are two built-in processors, are error (default) and Resolve, they will be selected in increasing order of the processor according to the processor.

import argparse

parser = argparse.ArgumentParser(conflict_handler='resolve')

parser.add_argument('-a',action="store")
parser.add_argument('-b',action="store",help='Short along')
parser.add_argument('--long-b','-b',
                    action="store",
                    help='Long and short together')

print(parser.parse_args(['-h']))

In this example, a processor uses the last name of the given parameters. Thus, independent option shielded -long-b -b alias.
Here Insert Picture Description
Sequential switching add_argument () call, the shield will no longer independent option.

import argparse

parser = argparse.ArgumentParser(conflict_handler='resolve')

parser.add_argument('-a',action="store")
parser.add_argument('--long-b','-b',
                    action="store",
                    help='Long and short together')
parser.add_argument('-b',action="store",help='Short along') 

print(parser.parse_args(['-h']))

Now two options can be used together.
Here Insert Picture Description

Guess you like

Origin blog.csdn.net/weixin_43193719/article/details/93224808