Skip to content

Commit

Permalink
Issue #1693: make alternative option names work
Browse files Browse the repository at this point in the history
The CanonicalName must be specified in that case. The canonical
name must be one of the alternatives.
  • Loading branch information
bschmalhofer committed Feb 14, 2025
1 parent 96df794 commit 7930ee3
Showing 1 changed file with 40 additions and 9 deletions.
49 changes: 40 additions & 9 deletions Kernel/System/Console/BaseCommand.pm
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,8 @@ sub AddArgument {
die;
}

if ( $Self->{_OptionSeen}->{ $Param{Name} } ) {
# No need to check for clashes with alternative option names
if ( $Self->{_CanonicalOptionNameSeen}->{ $Param{Name} } ) {
$Self->PrintError("Cannot add argument '$Param{Name}', because it is already registered as an option.");
die;
}
Expand Down Expand Up @@ -258,6 +259,17 @@ indicate which arguments it can process.
Multiple => 0, # optional, allow more than one occurrence (only possible if HasValue is true)
);
Alternative names can be declared as well. In this case the I<CanonicalName> has to be specified.
$Self->AddOption(
Name => 'module-directory|dir',
CanonicalName => 'module-directory',
Description => "Specify the directory containing the module sources (otherwise the OTOBO home directory will be used).",
Required => 0,
HasValue => 1,
ValueRegex => qr/.*/smx,
);
B<Option Naming Conventions>
If there is a source and a target involved in the command, the related options should start
Expand Down Expand Up @@ -295,19 +307,38 @@ sub AddOption {
}
}

# check the CanonicalName
if ( $Param{Name} =~ m/[|]/ ) {
if ( !$Param{CanonicalName} ) {
$Self->PrintError("A canonical option name must be declared for the option '$Param{Name}'.");
die;
}

my %IsAlternative = map { $_ => 1 } split /[|]/, $Param{Name};
if ( !$IsAlternative{ $Param{CanonicalName} } ) {
$Self->PrintError("The canonical option name '$Param{CanonicalName}' must be one of the alternative names.");
die;
}
}

if ( $Param{Multiple} && !$Param{HasValue} ) {
$Self->PrintError("Multiple can only be specified if HasValue is true.");
die;
}

if ( $Self->{_OptionSeen}->{ $Param{Name} }++ ) {
$Self->PrintError("Cannot register option '$Param{Name}' twice.");
die;
}
# The Name may include alternative names, e.g. 'module-directory|dir'
$Self->{_CanonicalOptionNameSeen}->{ $Param{CanonicalName} // $Param{Name} }++;
for my $Name ( split /[|]/, $Param{Name} ) {

if ( $Self->{_ArgumentSeen}->{ $Param{Name} } ) {
$Self->PrintError("Cannot add option '$Param{Name}', because it is already registered as an argument.");
die;
if ( $Self->{_OptionSeen}->{$Name}++ ) {
$Self->PrintError("Cannot register option '$Name' twice.");
die;
}

if ( $Self->{_ArgumentSeen}->{$Name} ) {
$Self->PrintError("Cannot add option '$Param{Name}', because it is already registered as an argument.");
die;
}
}

$Self->{_Options} //= [];
Expand All @@ -333,7 +364,7 @@ if the option was specified, and undef otherwise.
sub GetOption {
my ( $Self, $Option ) = @_;

if ( !$Self->{_OptionSeen}->{$Option} ) {
if ( !$Self->{_CanonicalOptionNameSeen}->{$Option} ) {
$Self->PrintError("Option '--$Option' was not configured and cannot be accessed.");

return;
Expand Down

0 comments on commit 7930ee3

Please sign in to comment.