【问题标题】:python argparse with dependencies带有依赖项的python argparse
【发布时间】:2011-05-26 20:33:33
【问题描述】:

我正在编写一个脚本,它有 2 个互斥的参数,以及一个仅对其中一个参数有意义的选项。如果您使用没有意义的参数调用它,我正在尝试将 argparse 设置为失败。

要明确:

-m -f 有道理

-s 有意义

-s -f 应该抛出错误

没有参数是好的。

我的代码是:

parser = argparse.ArgumentParser(description='Lookup servers by ip address from host file')
parser.add_argument('host', nargs=1,
            help="ip address to lookup")
main_group = parser.add_mutually_exclusive_group()
mysql_group = main_group.add_argument_group()
main_group.add_argument("-s", "--ssh", dest='ssh', action='store_true',
            default=False,
            help='Connect to this machine via ssh, instead of printing hostname')
mysql_group.add_argument("-m", "--mysql", dest='mysql', action='store_true',
            default=False,
            help='Start a mysql tunnel to the host, instead of printing hostname')
mysql_group.add_argument("-f", "--firefox", dest='firefox', action='store_true',
            default=False,
            help='Start a firefox session to the remotemyadmin instance')

这不起作用,因为它会吐出来

 usage: whichboom [-h] [-s] [-m] [-f] host

而不是我所期望的:

 usage: whichboom [-h] [-s | [-h] [-s]] host

或类似的。

 whichboom -s -f -m 116

也不会抛出任何错误。

【问题讨论】:

    标签: python argparse


    【解决方案1】:

    您只是将参数组混淆了。在您的代码中,您只需将一个选项分配给互斥组。我想你想要的是:

    parser = argparse.ArgumentParser(description='Lookup servers by ip address from host file')
    parser.add_argument('host', nargs=1,
                help="ip address to lookup")
    main_group = parser.add_mutually_exclusive_group()
    mysql_group = main_group.add_argument_group()
    main_group.add_argument("-s", "--ssh", dest='ssh', action='store_true',
                default=False,
                help='Connect to this machine via ssh, instead of printing hostname')
    mysql_group.add_argument("-m", "--mysql", dest='mysql', action='store_true',
                default=False,
                help='Start a mysql tunnel to the host, instead of printing hostname')
    main_group.add_argument("-f", "--firefox", dest='firefox', action='store_true',
                default=False,
                help='Start a firefox session to the remotemyadmin instance')
    

    您可以跳过整个互斥组的内容并添加如下内容:

    usage = 'whichboom [-h] [-s | [-h] [-s]] host'
    parser = argparse.ArgumentParser(description, usage)
    options, args = parser.parse_args()
    if options.ssh and options.firefox:
        parser.print_help()
        sys.exit()
    

    【讨论】:

    • 这是一个实用的解决方法。我会将其设置为使用无效选项进行轰炸,但我觉得这是一个杂项。如果今天早上我不能把它整理好,我会去麻烦开发人员,这可能是一个错误。
    • 我将 mysql 组和另一个选项添加到互斥组。我想我期待选择排除其他组,但这不是它的工作方式。我最终选择了您的其他解决方案。
    • argument groupmutually exclusive group 的用途非常不同,不能进行有意义的嵌套。 argument_group 不代表any of the above
    【解决方案2】:

    创建解析器时添加usage参数:

    usage = "usage: whichboom [-h] [-s | [-h] [-s]] host"
    description = "Lookup servers by ip address from host file"
    parser = argparse.ArgumentParser(description=description, usage=usage)
    

    来源:http://docs.python.org/dev/library/argparse.html#usage

    【讨论】:

    • 这充其量修复了命令行打印,它不会破坏功能(程序接受无效的参数集)并且意味着我必须手动维护该行..使用行最少出于我的担忧,我将其包括在内主要是为了帮助某人了解正在发生的事情。
    猜你喜欢
    • 1970-01-01
    • 2013-07-06
    • 2016-08-11
    • 2011-07-07
    • 2014-03-19
    • 2017-03-12
    • 2017-07-08
    • 2014-07-17
    • 2021-01-20
    相关资源
    最近更新 更多