栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

python中的互斥选项组请点击

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

python中的互斥选项组请点击

我最近也遇到过同样的用例。这就是我想出的。对于每个选项,您都可以给出冲突选项的列表。

from click import command, option, Option, UsageErrorclass MutuallyExclusiveOption(Option):    def __init__(self, *args, **kwargs):        self.mutually_exclusive = set(kwargs.pop('mutually_exclusive', []))        help = kwargs.get('help', '')        if self.mutually_exclusive: ex_str = ', '.join(self.mutually_exclusive) kwargs['help'] = help + (     ' NOTE: This argument is mutually exclusive with '     ' arguments: [' + ex_str + '].' )        super(MutuallyExclusiveOption, self).__init__(*args, **kwargs)    def handle_parse_result(self, ctx, opts, args):        if self.mutually_exclusive.intersection(opts) and self.name in opts: raise UsageError(     "Illegal usage: `{}` is mutually exclusive with "     "arguments `{}`.".format(         self.name,         ', '.join(self.mutually_exclusive)     ) )        return super(MutuallyExclusiveOption, self).handle_parse_result( ctx, opts, args        )

然后使用常规

option
装饰器,但传递
cls
参数:

@command(help="Run the command.")@option('--jar-file', cls=MutuallyExclusiveOption,        help="The jar file the topology lives in.",        mutually_exclusive=["other_arg"])@option('--other-arg',        cls=MutuallyExclusiveOption,        help="The jar file the topology lives in.",        mutually_exclusive=["jar_file"])def cli(jar_file, other_arg):    print "Running cli."    print "jar-file: {}".format(jar_file)    print "other-arg: {}".format(other_arg)if __name__ == '__main__':    cli()

这里的要点
包括上面的代码,并显示了运行它的输出。

如果这对您不起作用,那么在单击github页面上还会有一些(封闭的)问题提及此问题,并提供了一些您可以使用的想法。

  • https://github.com/pallets/click/issues/257
  • https://github.com/pallets/click/issues/509


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/388489.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号