【问题标题】:command line options in perlperl 中的命令行选项
【发布时间】:2016-05-18 01:44:46
【问题描述】:

我通过传递一些命令行选项来调用我的 perl 脚本。如果用户在调用脚本时未传递所需的命令行选项,则脚本应终止。目前我正在使用if statement 进行简单检查。如果所需的参数超过 10 个,使用 If 语句看起来很笨拙。我只是想知道是否有比仅使用 if 语句更好的方法。

命令行选项:

sub startup {
   my ($self) = @_;

    GetOptions (
        "endpoint|e=s"           => \$self->{'endpoint'},
        "port|pt=s"              => \$self->{'port'},
        "client|c=s"             => \$self->{'client'},
        "client_interface|ci=s"  => \$self->{'client_interface'},
        "origin|o=s"             => \$self->{'origin'},
        "origin_interface|oi=s"  => \$self->{'origin_interface'},
        "customer_id|cid=s"      => \$self->{'customer_id'},
        "endpoint_id|eid=s"      => \$self->{'endpoint_id'},
         ) || $self->abort( "Invalid command line options.
               Valid options are endpoint,port,client,client_interface,
   origin,origin_interface,customer_id,endpoint_id");

#如果--endpoint ip 和--customer id 和--client 没有通过则终止脚本执行

 if ( !$self->{'endpoint'} || !$self->{'customer_id'} || !$self->{'client'}){
        $self->abort( '[Startup] endpoint customer and client are required arguments.'
                      . 'Please provide --endpoint and --customer id and -- client  ');
    }

调用脚本的命令:

./testframework --scriptname -- --endpoint=198.18.179.42  --port=5000 --client=1.1.1.1 --client_interface=2.2.2.2 --origin=3.3.3.3 --origin_interface= --Outertunnel=Tunnel0 --Innertunnel=Tunnel2 --customer_id=900010 --endpoint_id=2859588 

【问题讨论】:

  • 我认为您对GetOptions 的使用是正确的。但是,您遇到了太多可能表明设计问题的参数......这些参数是否具有合理的默认值?它们是否与特定机器相关联,因此更适合作为配置文件的一部分?它们可以直接被你的脚本猜到而不是提供它们作为输入吗?我宁愿为此努力。
  • @eballes IP 不同。我无法从 GetOptions 中摆脱任何东西。我们有多个测试台,我们希望脚本在所有机器上运行。

标签: perl


【解决方案1】:

以下版本消除了一些笨拙,同时提供了更具体的错误消息。

my @required = qw( endpoint customer_id client );

if ( my @missing = grep { !$self->{$_} } @required ) {
   $self->abort("[Startup] Missing required arguments: @missing");
}

【讨论】:

    【解决方案2】:

    一种方法是从List::Util 使用all

     unless ( all { defined $self->{$_} } qw(endpoint customer_id client) ){
        # error
     }
    

    如果您没有最新版本的List::Util,请使用List::MoreUtils

    【讨论】:

    • 您的解决方案比我目前使用的解决方案更好。如果我有 10 个选项,我需要在 qw(endpoint customer_id client option4 option 5...... option 10) 中指定所有选项,这与我目前使用 if stmt所做的非常相似>
    • @user3587025:是的,这个答案仍然使用 if 语句。但是,增量改进是您需要维护更少的代码,因为您不需要多余的!$self->{'...'} ||Getopt::Long 没有指定所需选项的固有方法。
    【解决方案3】:

    您能否检查一下您的哈希中定义的键数量是否正确?

    my @options = grep { defined $self->{$_} }  keys %{$self};
    die "Missing options\n" unless @options == 10;
    

    或者,如果您希望您的使用说明更明确:

    for my $opt (keys %{$self}) {
        die "Missing option --$opt\n" unless defined $self->{$opt};
    }
    

    【讨论】:

    • 此解决方案不区分必需参数和可选参数。我很惊讶它的票数最多。
    • 我将原始问题解释为暗示所有选项都是必需的。
    • 但即使没有,也可以轻松修改。创建$self->{required} = {},然后将GetOptions 所需参数更改为指向此新内部哈希成员的引用,并对其进行迭代而不是主%{$self}
    猜你喜欢
    • 2012-05-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    • 2011-05-05
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    相关资源
    最近更新 更多