Subject: | GetOptionsFromArray in OO interface |
It would be nice to be able to use the GetOptionsFromArray function with
the OO interface. A possible patch for this is attached.
Subject: | 0001-Add-getoptionsfromarray-to-OO-interface.patch |
From b94ddf7d5cbb2da1dac2a5e17dc212e7d3ef42ed Mon Sep 17 00:00:00 2001
From: Douglas Christopher Wilson <doug@somethingdoug.com>
Date: Thu, 28 Oct 2010 19:51:05 -0400
Subject: [PATCH] Add getoptionsfromarray to OO interface
---
lib/Getopt/Long.pm | 8 +++++++-
t/gol-oo.t | 10 +++++++++-
2 files changed, 16 insertions(+), 2 deletions(-)
diff --git a/lib/Getopt/Long.pm b/lib/Getopt/Long.pm
index c827d3c..00eeb0e 100644
--- a/lib/Getopt/Long.pm
+++ b/lib/Getopt/Long.pm
@@ -176,6 +176,12 @@ sub configure {
sub getoptions {
my ($self) = shift;
+ return $self->getoptionsfromarray(\@ARGV, @_);
+}
+
+sub getoptionsfromarray {
+ my ($self) = shift;
+
# Restore config settings.
my $save = Getopt::Long::Configure ($self->{settings});
@@ -188,7 +194,7 @@ sub getoptions {
# be called implicitly here, and again explicitly when we try
# to deliver the messages.
local ($SIG{__DIE__}) = 'DEFAULT';
- $ret = Getopt::Long::GetOptions (@_);
+ $ret = Getopt::Long::GetOptionsFromArray (@_);
};
# Restore saved settings.
diff --git a/t/gol-oo.t b/t/gol-oo.t
index df49cb6..cf1d981 100644
--- a/t/gol-oo.t
+++ b/t/gol-oo.t
@@ -14,7 +14,7 @@ my $want_version="2.24";
die("Getopt::Long version $want_version required--this is only version ".
$Getopt::Long::VERSION)
unless $Getopt::Long::VERSION ge $want_version;
-print "1..9\n";
+print "1..14\n";
@ARGV = qw(-Foo -baR --foo bar);
my $p = new Getopt::Long::Parser (config => ["no_ignore_case"]);
@@ -29,3 +29,11 @@ print ((@ARGV == 1) ? "" : "not ", "ok 6\n");
print (($ARGV[0] eq "bar") ? "" : "not ", "ok 7\n");
print (!(defined $opt_baR) ? "" : "not ", "ok 8\n");
print (!(defined $opt_bar) ? "" : "not ", "ok 9\n");
+
+my @args = (qw[-test 1]);
+my $o = Getopt::Long::Parser->new;
+print "ok 10\n" if $o->getoptionsfromarray(\@args, "test=i");
+print ((defined $opt_test) ? "" : "not ", "ok 11\n");
+print (($opt_test == 1) ? "" : "not ", "ok 12\n");
+print ((@ARGV == 1) ? "" : "not ", "ok 13\n");
+print ((@args == 0) ? "" : "not ", "ok 14\n");
--
1.7.3.1.msysgit.0