(enh) double-dash support for arguments (#4151)

This commit is contained in:
Kevin Duret 2023-01-23 11:32:27 +01:00 committed by GitHub
parent c00a652421
commit a5d43fc0b4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 1 deletions

View File

@ -33,6 +33,7 @@ sub new {
$options{options}->add_options(arguments => {
'command:s' => { name => 'command' },
'arg:s@' => { name => 'arg' },
'extra-args' => { name => 'extra_args' },
'sanitize-message:s' => { name => 'sanitize_message' }
});
@ -70,9 +71,12 @@ sub sanitize_message {
sub run {
my ($self, %options) = @_;
my @arg = defined($self->{option_results}->{arg}) ? @{$self->{option_results}->{arg}} : ();
push @arg, @{$self->{option_extras}} if ($self->{option_results}->{extra_args});
my $result = $options{custom}->request(
command => $self->{option_results}->{command},
arg => $self->{option_results}->{arg}
arg => \@arg
);
$self->{output}->output_add(
@ -112,6 +116,13 @@ In nrpe use following command to get server version: --command='_NRPE_CHECK'
Set arguments (Multiple option. Example: --arg='arg1' --arg='arg2').
=item B<--extra-args>
Use extra arguments from command line (all values placed after a double-dash '--')
as additional "--arg" options for the NRPE command.
Example: --arg='arg1' --extra-args -- 'arg2' 'arg3' 'arg4'
=item B<--sanitize-message>
Sanitize message by removing heading code and

View File

@ -62,6 +62,15 @@ sub GetOptions {
if (defined($ARGV[$i]) && $ARGV[$i] =~ /^--(.*?)(?:=|$)((?s).*)/) {
my ($option, $value) = ($1, $2);
# The special argument "--" forces an end of option-scanning.
# All arguments placed after are stored in a list with the special option key '_double_dash_'.
if ($option eq '' && $value eq '') {
my @values = splice @ARGV, $i + 1, $num_args - $i - 1;
push @{${$opts{'_double_dash_'}}}, @values;
splice @ARGV, $i, 1;
last;
}
# find type of option
if ($search_str !~ /,((?:[^,]*?\|){0,}$option(?:\|.*?){0,}(:.*?){0,1}),/) {
warn "Unknown option: $option" if ($warn_message == 1);

View File

@ -32,6 +32,7 @@ sub new {
$self->{perfdata} = centreon::plugins::perfdata->new(output => $options{output});
%{$self->{option_results}} = ();
@{$self->{option_extras}} = @{$options{options}->{extra_arguments}};
$self->{output} = $options{output};
$self->{output}->use_new_perfdata(value => 1)
if (defined($options{force_new_perfdata}) && $options{force_new_perfdata} == 1);

View File

@ -37,6 +37,7 @@ sub new {
$self->{options} = {};
@{$self->{pod_package}} = ();
$self->{pod_packages_once} = {};
$self->{extra_arguments} = [];
if ($alternative == 0) {
require Getopt::Long;
@ -145,6 +146,9 @@ sub parse_options {
};
}
# Store all arguments placed after the special argument "--" in the 'extra_arguments' list
$self->{options}->{'_double_dash_'} = \$self->{extra_arguments};
GetOptions(
%{$self->{options}}
);