Permalink
Browse files

allow plugins to have command line options

  • Loading branch information...
1 parent c90fdee commit 081a10d350b3fcbadf7c323d90e5b97b6d34786f @allanon allanon committed Mar 7, 2016
Showing with 46 additions and 9 deletions.
  1. +27 −6 openkore.pl
  2. +15 −3 src/Settings.pm
  3. +4 −0 src/functions.pl
View
@@ -103,12 +103,9 @@ sub __start {
# Parse command-line arguments.
sub parseArguments {
eval {
- if (!Settings::parseArguments()) {
- if ($Settings::options{version}) {
- print "$Settings::versionText\n";
- } else {
- print Settings::getUsageText();
- }
+ Settings::parseArguments();
+ if ($Settings::options{version}) {
+ print "$Settings::versionText\n";
exit 1;
}
};
@@ -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.
# Precondition: $interface is initialized.
sub selfCheck {
View
@@ -54,6 +54,7 @@ use File::Spec;
use Translation qw(T TF);
use Utils::ObjectList;
use Utils::Exceptions;
+use List::MoreUtils qw( uniq );
use enum qw(CONTROL_FILE_TYPE TABLE_FILE_TYPE);
@@ -157,9 +158,9 @@ sub parseArguments {
undef $interface;
undef $lockdown;
- local $SIG{__WARN__} = sub {
- ArgumentException->throw($_[0]);
- };
+ # Allow plugins to have their own command line options.
+ Getopt::Long::Configure( 'pass_through' );
+
GetOptions(
'control=s', \$options{control},
'tables=s', \$options{tables},
@@ -291,6 +292,17 @@ sub getUsageText {
Developer options:
--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/^\t\t?//gm;
return $text;
View
@@ -126,6 +126,10 @@ sub loadPlugins {
} elsif ($@) {
die $@;
}
+
+ # Allow plugins to use command line arguments.
+ Plugins::callHook( 'parse_command_line' );
+ main::checkEmptyArguments();
}
sub loadDataFiles {

0 comments on commit 081a10d

Please sign in to comment.