+ add alternative Getopt module (Ref #144)
This commit is contained in:
parent
380f3109bc
commit
9dea1d8f08
|
@ -0,0 +1,100 @@
|
|||
#
|
||||
# Copyright 2015 Centreon (http://www.centreon.com/)
|
||||
#
|
||||
# Centreon is a full-fledged industry-strength solution that meets
|
||||
# the needs in IT infrastructure and application monitoring for
|
||||
# service performance.
|
||||
#
|
||||
# Licensed under the Apache License, Version 2.0 (the "License");
|
||||
# you may not use this file except in compliance with the License.
|
||||
# You may obtain a copy of the License at
|
||||
#
|
||||
# http://www.apache.org/licenses/LICENSE-2.0
|
||||
#
|
||||
# Unless required by applicable law or agreed to in writing, software
|
||||
# distributed under the License is distributed on an "AS IS" BASIS,
|
||||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
# See the License for the specific language governing permissions and
|
||||
# limitations under the License.
|
||||
#
|
||||
|
||||
package centreon::plugins::alternative::Getopt;
|
||||
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
use Exporter;
|
||||
use vars qw(@ISA @EXPORT @EXPORT_OK);
|
||||
@ISA = qw(Exporter);
|
||||
|
||||
BEGIN {
|
||||
@EXPORT = qw(&GetOptions);
|
||||
@EXPORT_OK = qw();
|
||||
}
|
||||
|
||||
use vars @EXPORT, @EXPORT_OK;
|
||||
|
||||
our $warn_message = 0;
|
||||
|
||||
sub get_assigned_value {
|
||||
my (%options) = @_;
|
||||
|
||||
if (!defined($options{val}) || $options{val} eq '') {
|
||||
if ($options{pos} + 1 < $options{num_args} && $ARGV[$options{pos} + 1] !~ /^--/) {
|
||||
my $val = $ARGV[$options{pos} + 1];
|
||||
splice @ARGV, $options{pos} + 1, 1;
|
||||
return ($options{num_args} - 1, $val);
|
||||
} else {
|
||||
return ($options{num_args}, '');
|
||||
}
|
||||
}
|
||||
|
||||
return ($options{num_args}, $options{val});
|
||||
}
|
||||
|
||||
sub GetOptions {
|
||||
my (%opts) = @_;
|
||||
|
||||
my $search_str = ',' . join(',', keys %opts) . ',';
|
||||
my $num_args = scalar(@ARGV);
|
||||
for (my $i = 0; $i < $num_args;) {
|
||||
if ($ARGV[$i] =~ /^--(.*?)(?:=|$)(.*)/) {
|
||||
my ($option, $value) = ($1, $2);
|
||||
|
||||
# find type of option
|
||||
if ($search_str !~ /,((?:[^,]*?\|){0,}$option(?:\|.*?){0,}(:.*?){0,1}),/) {
|
||||
warn "Unknown option: $option" if ($warn_message == 1);
|
||||
$i++;
|
||||
next;
|
||||
}
|
||||
|
||||
my ($option_selected, $type_opt) = ($1, $2);
|
||||
if (!defined($type_opt)) {
|
||||
${$opts{$option_selected}} = 1;
|
||||
} elsif ($type_opt =~ /:s$/) {
|
||||
($num_args, my $assigned) = get_assigned_value(num_args => $num_args, pos => $i, val => $value);
|
||||
${$opts{$option_selected}} = $assigned;
|
||||
} elsif ($type_opt =~ /:s\@$/) {
|
||||
${$opts{$option . $type_opt}} = [] if (!defined(${$opts{$option . $type_opt}}));
|
||||
($num_args, my $assigned) = get_assigned_value(num_args => $num_args, pos => $i, val => $value);
|
||||
push @{${$opts{$option_selected}}}, $assigned;
|
||||
} elsif ($type_opt =~ /:s\%$/) {
|
||||
${$opts{$option . $type_opt}} = {} if (!defined(${$opts{$option . $type_opt}}));
|
||||
($num_args, my $assigned) = get_assigned_value(num_args => $num_args, pos => $i, val => $value);
|
||||
if ($assigned =~ /^(.*?)=(.*)/) {
|
||||
${$opts{$option_selected}}->{$1} = $2;
|
||||
}
|
||||
}
|
||||
|
||||
splice @ARGV, $i, 1;
|
||||
$num_args--;
|
||||
} else {
|
||||
warn "argument $ARGV[$i] alone" if ($warn_message == 1);
|
||||
$i++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
1;
|
||||
|
||||
__END__
|
|
@ -22,13 +22,11 @@ package centreon::plugins::options;
|
|||
|
||||
use Pod::Usage;
|
||||
use Pod::Find qw(pod_where);
|
||||
use Getopt::Long;
|
||||
Getopt::Long::Configure("pass_through");
|
||||
Getopt::Long::Configure('bundling');
|
||||
Getopt::Long::Configure('no_auto_abbrev');
|
||||
use strict;
|
||||
use warnings;
|
||||
|
||||
my $alternative = 0;
|
||||
|
||||
sub new {
|
||||
my $class = shift;
|
||||
my $self = {};
|
||||
|
@ -38,13 +36,29 @@ sub new {
|
|||
$self->{options} = {};
|
||||
@{$self->{pod_package}} = ();
|
||||
$self->{pod_packages_once} = {};
|
||||
|
||||
if ($alternative == 0) {
|
||||
require Getopt::Long;
|
||||
Getopt::Long->import();
|
||||
Getopt::Long::Configure("pass_through");
|
||||
Getopt::Long::Configure('bundling');
|
||||
Getopt::Long::Configure('no_auto_abbrev');
|
||||
} else {
|
||||
require centreon::plugins::alternative::Getopt;
|
||||
centreon::plugins::alternative::Getopt->import();
|
||||
}
|
||||
|
||||
return $self;
|
||||
}
|
||||
|
||||
sub set_sanity {
|
||||
my ($self, %options) = @_;
|
||||
|
||||
Getopt::Long::Configure('no_pass_through');
|
||||
if ($alternative == 0) {
|
||||
Getopt::Long::Configure('no_pass_through');
|
||||
} else {
|
||||
$centreon::plugins::alternative::Getopt::warn_message = 1;
|
||||
}
|
||||
$SIG{__WARN__} = sub {
|
||||
$self->{output}->add_option_msg(short_msg => $_[0]);
|
||||
$self->{output}->option_exit(nolabel => 1);
|
||||
|
|
|
@ -256,7 +256,7 @@ __END__
|
|||
|
||||
=head1 NAME
|
||||
|
||||
centreon_plugins.pl - main program to call Merethis plugins.
|
||||
centreon_plugins.pl - main program to call Centreon plugins.
|
||||
|
||||
=head1 SYNOPSIS
|
||||
|
||||
|
|
Loading…
Reference in New Issue