Perl Getopt::未调用声明参数操作
我在脚本中使用,但调用脚本并传递Perl Getopt::未调用声明参数操作,perl,getopt-long,Perl,Getopt Long,我在脚本中使用,但调用脚本并传递-get\u ip“test”不会做任何事情,即脚本执行“my”语句,并且不会调用getFirstAvailableIP use Getopt::Declare; use lib "/home/vtsingaras/NicTool/client/lib/"; use NicToolServerAPI; use strict; use warnings; #debug remove use Data::Dumper; #NicToolServer settings
-get\u ip“test”
不会做任何事情,即脚本执行“my”语句,并且不会调用getFirstAvailableIP
use Getopt::Declare;
use lib "/home/vtsingaras/NicTool/client/lib/";
use NicToolServerAPI;
use strict;
use warnings;
#debug remove
use Data::Dumper;
#NicToolServer settings, edit
my $ntconf = {
ntuser => 'censored',
ntpass => 'censored',
nthost => 'censored',
ntport => 8082,
};
my ( $zone, $fqdn, $ip, $comment );
my $options_spec = q(+g[et_ip] <zone> Get the first available IP from the provided reverse <zone>.
{getFirstAvailableIP($::zone);}
+s[et_dns] <fqdn> <ip> <comment> Create an A record for <fqdn> that points to <ip> and the associated PTR record.
{createFwdAndPtr($::fqdn, $::ip, $::comment);}
);
my $args = Getopt::Declare->new($options_spec);
#Setup NicTool
my $nt = new NicToolServerAPI;
$NicToolServerAPI::server_host = $ntconf->{nthost};
$NicToolServerAPI::server_port = $ntconf->{ntport};
$NicToolServerAPI::data_protocol = "soap";
#$NicToolServerAPI::use_https_authentication = 0;
sub nt_login {
#Login to NicTool Server
my $ntuser = $nt->send_request(
action => "login",
username => $ntconf->{ntuser},
password => $ntconf->{ntpass},
);
if ( $ntuser->{error_code} ) {
print( "Unable to log in: " . $ntuser->{error_code} . " " . $ntuser->{error_msg} . "\n" );
exit 1;
} else {
print( "Logged in as " . $ntuser->{first_name} . " " . $ntuser->{last_name} . "\n" );
}
}
sub getFirstAvailableIP {
my $fqdn = $_[0];
print $fqdn;
die "blah";
}
使用Getopt::Declare;
使用lib“/home/vtsingaras/NicTool/client/lib/”;
使用NicToolServerAPI;
严格使用;
使用警告;
#调试删除
使用数据::转储程序;
#NicToolServer设置,编辑
我的$ntconf={
ntuser=>“已审查”,
ntpass=>“已审查”,
nHost=>“已审查”,
ntport=>8082,
};
我的($zone,$fqdn,$ip,$comment);
my$options_spec=q(+g[et_ip]从提供的背面获取第一个可用ip。
{getFirstAvailableIP($::区域);}
+s[et_dns]为指向的A记录和关联的PTR记录创建A记录。
{createFwdAndPtr($::fqdn,$::ip,$::comment);}
);
我的$args=Getopt::Declare->new($options\u spec);
#安装工具
my$nt=新的NicToolServerAPI;
$NicToolServerAPI::server_host=$ntconf->{nthost};
$NicToolServerAPI::server_port=$ntconf->{ntport};
$NicToolServerAPI::data_protocol=“soap”;
#$NicToolServerAPI::使用\u https\u身份验证=0;
子nt_登录{
#登录到NicTool服务器
我的$ntuser=$nt->发送请求(
操作=>“登录”,
用户名=>$ntconf->{ntuser},
密码=>$ntconf->{ntpass},
);
if($ntuser->{error\u code}){
打印(“无法登录:“..ntuser->{error\u code}.”“$ntuser->{error\u msg}.”\n”);
出口1;
}否则{
打印(“以“$ntuser->{first\u name}.”“$ntuser->{last\u name}.”的身份登录。”\n”);
}
}
子getFirstAvailableIP{
我的$fqdn=$\u0];
打印$fqdn;
死“废话”;
}
问题是您在$options\u spec
中为获取ip
指定了+而不是-
下面是一个自包含的可运行示例,它调用getFirstAvailableIP
:
use strict;
use warnings;
use Getopt::Declare;
my $zone;
my $args = Getopt::Declare->new(<<'END_OPTS');
# tab
# ||||
# vvvv
-g[et_ip] <zone> Get the first available IP from the provided reverse <zone>.
{ getFirstAvailableIP($zone); }
END_OPTS
print "hello world\n";
exit;
sub getFirstAvailableIP {
print "blah - @_\n";
}
__END__
请注意,此模块的规范中需要一个选项卡
字符;这使得正确复制“n”粘贴变得很困难
$ perl declare_test.pl -get_ip test
blah - test
hello world