Perl Getopt :: Declare参数动作未被调用

时间:2014-09-29 13:59:44

标签: perl getopt-long

我在脚本中使用Getopt::Declare但是调用脚本并传递-get_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";
}

1 个答案:

答案 0 :(得分:2)

问题是您在$options_spec的{​​{1}}中指定了 + 而不是 -

这是一个自包含的可运行示例,它调用get_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__

请注意,此模块在其规范中需要$ perl declare_test.pl -get_ip test blah - test hello world 个字符;这使得很难正确地复制'n'。