Skip to content

Commit

Permalink
Merge pull request #364 from timmullin/RE-57
Browse files Browse the repository at this point in the history
Improve command line option validation
  • Loading branch information
toddr authored Jan 31, 2024
2 parents b2616d2 + 68638f4 commit 4db76fb
Show file tree
Hide file tree
Showing 3 changed files with 391 additions and 0 deletions.
43 changes: 43 additions & 0 deletions elevate-cpanel
Original file line number Diff line number Diff line change
Expand Up @@ -4712,6 +4712,47 @@ EOS
);
}

sub _validate_option_combos ($self) {

my @solitary_options = qw(
clean continue help log service status update version
);
my @start_only_options = qw(
manual-reboots non-interactive
);

return 1 if !%{ $self->{_getopt} };

foreach my $option (@solitary_options) {
if ( $self->getopt($option) ) {
if ( scalar( keys %{ $self->{_getopt} } ) > 1 ) {
return $self->help( "Option \"$option\" is not compatible with any other option", 1 );
}
else {
return 1;
}
}
}

if ( $self->getopt('start') && defined $self->getopt('check') ) {
return $self->help( "The options \"start\" and \"check\" are mutually exclusive", 1 );
}

if ( !$self->getopt('start') && !defined $self->getopt('check') ) {
return $self->help( "Invalid option combination", 1 );
}

if ( !$self->getopt('start') ) {
foreach my $option (@start_only_options) {
if ( $self->getopt($option) ) {
return $self->help( "Option \"$option\" is only compatible with \"start\"", 1 );
}
}
}

return 1;
}

sub init ( $self, @args ) {

$self->{_getopt} = {};
Expand All @@ -4722,6 +4763,8 @@ EOS
_OPTIONS()
) or return $self->help( "Invalid Option", 1 );

return unless $self->_validate_option_combos();

return $self->full_help() if $self->getopt('help');
}

Expand Down
44 changes: 44 additions & 0 deletions lib/Elevate/Usage.pm
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,48 @@ sub _OPTIONS {
);
}

sub _validate_option_combos ($self) {

my @solitary_options = qw(
clean continue help log service status update version
);
my @start_only_options = qw(
manual-reboots non-interactive
);

# Invoking with no options is permissible
return 1 if !%{ $self->{_getopt} };

foreach my $option (@solitary_options) {
if ( $self->getopt($option) ) {
if ( scalar( keys %{ $self->{_getopt} } ) > 1 ) {
return $self->help( "Option \"$option\" is not compatible with any other option", 1 );
}
else {
return 1;
}
}
}

if ( $self->getopt('start') && defined $self->getopt('check') ) {
return $self->help( "The options \"start\" and \"check\" are mutually exclusive", 1 );
}

if ( !$self->getopt('start') && !defined $self->getopt('check') ) {
return $self->help( "Invalid option combination", 1 );
}

if ( !$self->getopt('start') ) {
foreach my $option (@start_only_options) {
if ( $self->getopt($option) ) {
return $self->help( "Option \"$option\" is only compatible with \"start\"", 1 );
}
}
}

return 1;
}

sub init ( $self, @args ) {

$self->{_getopt} = {};
Expand All @@ -36,6 +78,8 @@ sub init ( $self, @args ) {
_OPTIONS()
) or return $self->help( "Invalid Option", 1 );

return unless $self->_validate_option_combos();

return $self->full_help() if $self->getopt('help');
}

Expand Down
Loading

0 comments on commit 4db76fb

Please sign in to comment.