Skip to content

Commit

Permalink
allow plugins to have command line options
Browse files Browse the repository at this point in the history
  • Loading branch information
allanon committed Mar 7, 2016
1 parent c90fdee commit 081a10d
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 9 deletions.
33 changes: 27 additions & 6 deletions openkore.pl
Expand Up @@ -103,12 +103,9 @@ sub __start {
# Parse command-line arguments. # Parse command-line arguments.
sub parseArguments { sub parseArguments {
eval { eval {
if (!Settings::parseArguments()) { Settings::parseArguments();
if ($Settings::options{version}) { if ($Settings::options{version}) {
print "$Settings::versionText\n"; print "$Settings::versionText\n";
} else {
print Settings::getUsageText();
}
exit 1; exit 1;
} }
}; };
Expand All @@ -123,6 +120,30 @@ sub parseArguments {
} }
} }


# Make sure there aren't any unhandled arguments left.
sub checkEmptyArguments {
if ( $Settings::options{help} ) {
print Settings::getUsageText();
exit 1;
}
eval {
use Getopt::Long;
local $SIG{__WARN__} = sub { ArgumentException->throw( $_[0] ); };
# Turn off the "pass_through" option so any remaining options will be considered an error.
Getopt::Long::Configure( 'default' );
GetOptions();
};
if ( my $e = caught( 'IOException', 'ArgumentException' ) ) {
print "Error: $e\n";
if ( $e->isa( 'ArgumentException' ) ) {
print Settings::getUsageText();
}
exit 1;
} elsif ( $@ ) {
die $@;
}
}

# Perform some self-checks to ensure everything is OK. # Perform some self-checks to ensure everything is OK.
# Precondition: $interface is initialized. # Precondition: $interface is initialized.
sub selfCheck { sub selfCheck {
Expand Down
18 changes: 15 additions & 3 deletions src/Settings.pm
Expand Up @@ -54,6 +54,7 @@ use File::Spec;
use Translation qw(T TF); use Translation qw(T TF);
use Utils::ObjectList; use Utils::ObjectList;
use Utils::Exceptions; use Utils::Exceptions;
use List::MoreUtils qw( uniq );


use enum qw(CONTROL_FILE_TYPE TABLE_FILE_TYPE); use enum qw(CONTROL_FILE_TYPE TABLE_FILE_TYPE);


Expand Down Expand Up @@ -157,9 +158,9 @@ sub parseArguments {
undef $interface; undef $interface;
undef $lockdown; undef $lockdown;


local $SIG{__WARN__} = sub { # Allow plugins to have their own command line options.
ArgumentException->throw($_[0]); Getopt::Long::Configure( 'pass_through' );
};
GetOptions( GetOptions(
'control=s', \$options{control}, 'control=s', \$options{control},
'tables=s', \$options{tables}, 'tables=s', \$options{tables},
Expand Down Expand Up @@ -291,6 +292,17 @@ sub getUsageText {
Developer options: Developer options:
--no-connect Do not connect to any servers. --no-connect Do not connect to any servers.
}; };
my $data = { options => [] };
Plugins::callHook( usage => $data );
if ( @{ $data->{options} } ) {
foreach my $plugin ( uniq sort map { $_->{plugin} || 'unknown' } @{ $data->{options} } ) {
$text .= "\nOptions for the '$plugin' plugin:\n";
foreach ( grep { $plugin eq ( $_->{plugin} || 'unknown' ) } @{ $data->{options} } ) {
$text .= sprintf "%-2s %-22s %s\n", $_->{short} || '', $_->{long} || '', $_->{description} || '';
}
}
}
$text =~ s/\n*$/\n/s;
$text =~ s/^\n//s; $text =~ s/^\n//s;
$text =~ s/^\t\t?//gm; $text =~ s/^\t\t?//gm;
return $text; return $text;
Expand Down
4 changes: 4 additions & 0 deletions src/functions.pl
Expand Up @@ -126,6 +126,10 @@ sub loadPlugins {
} elsif ($@) { } elsif ($@) {
die $@; die $@;
} }

# Allow plugins to use command line arguments.
Plugins::callHook( 'parse_command_line' );
main::checkEmptyArguments();
} }


sub loadDataFiles { sub loadDataFiles {
Expand Down

0 comments on commit 081a10d

Please sign in to comment.