CLI option that is an aggregation of two other options and precedence, in python

∥☆過路亽.° 提交于 2019-12-14 03:13:57

问题


I'm using argparse to parse CLI options in my Python scripts. I would create a flag that is equivalent to specifying two other flags. So

python myscript.py --flagc

is equivalent to

python myscript.py --flaga --flagb

This is my code:

args = parser.parse_args()

if args.flagc:
    args.flaga = True
    args.flagb = True

The problem is flaga and flagb have also opposite flags, no-flaga and no-flagb:

parser.add_argument("--flaga", dest="flaga", action="store_true")
parser.add_argument("--no-flaga", dest="flaga", action="store_false")
parser.set_defaults(flaga=False)

and so on. So I could have trouble with flags precedences. For example, if I call the script with:

python myscript.py --flagc --no-flaga

I get True for args.flaga, but normally you want False, since last flag have precedence.

How can I manage this?


回答1:


Right, by processing flag_c after parsing you loose the precedence information. Often that issue argues in favor of this post processing, but here a custom Action might be the better choice.

class MyAction(argparse._StoreTrueAction):
    def __call__(self, parser, namespace, values, option_string=None):
        # adapt from _StoreConst
        # leave the self.dest set; could deactivate
        setattr(namespace, self.dest, self.const)
        setattr(namespace, 'flaga', True)
        setattr(namespace, 'flagb', True)

parser.add_argument("--flagc", action=MyAction)

print(parser.parse_args('--flaga'.split()))
print(parser.parse_args('--flagc'.split()))
print(parser.parse_args('--flagc --no-flaga'.split()))

printing:

1027:~/mypy$ python stack33264649.py 
Namespace(flaga=True, flagb=False, flagc=False)
Namespace(flaga=True, flagb=True, flagc=True)
Namespace(flaga=False, flagb=True, flagc=True)

It's the simplest custom class that does the job. It takes advantage of the defaults set by its parent (esp. nargs=0). Alternatively it could be based off of _StoreConst or even Action.

I'm leaving the setattr(namespace, self.dest, self.const) in place. There's little harm in having an accurate indicator of whether --flagc was invoked or not. And it is simpler than trying to suppress flagc default.

OK, suppressing the default isn't that hard:

class MyAction(argparse._StoreTrueAction):
    def __init__(self, *args, **kwargs):
        super(MyAction, self).__init__(*args,**kwargs)
        self.default=argparse.SUPPRESS
    def __call__(self, parser, namespace, values, option_string=None):
        setattr(namespace, 'flaga', True)
        setattr(namespace, 'flagb', True)


来源:https://stackoverflow.com/questions/33264649/cli-option-that-is-an-aggregation-of-two-other-options-and-precedence-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!