From 081a10d350b3fcbadf7c323d90e5b97b6d34786f Mon Sep 17 00:00:00 2001 From: Allanon Date: Sun, 6 Mar 2016 17:27:33 -0800 Subject: [PATCH] allow plugins to have command line options --- openkore.pl | 33 +++++++++++++++++++++++++++------ src/Settings.pm | 18 +++++++++++++++--- src/functions.pl | 4 ++++ 3 files changed, 46 insertions(+), 9 deletions(-) diff --git a/openkore.pl b/openkore.pl index e5a96a8047..819c169b2c 100755 --- a/openkore.pl +++ b/openkore.pl @@ -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 { diff --git a/src/Settings.pm b/src/Settings.pm index 8f36003361..9d80ebe947 100644 --- a/src/Settings.pm +++ b/src/Settings.pm @@ -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; diff --git a/src/functions.pl b/src/functions.pl index e73d2c37cc..59a64d052a 100644 --- a/src/functions.pl +++ b/src/functions.pl @@ -126,6 +126,10 @@ sub loadPlugins { } elsif ($@) { die $@; } + + # Allow plugins to use command line arguments. + Plugins::callHook( 'parse_command_line' ); + main::checkEmptyArguments(); } sub loadDataFiles {