【发布时间】: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