Subject: | Allow Hidden Options to Be Used |
So far as I can tell, hidden options don't currently work: attempting to
use one throws an error as if the option truly didn't exist rather than
was just hidden.
The attached patch fixes this, with a test.
There are already tests that hidden options don't appear in the output;
this new test additionally checks that a hidden option can actually be used.
The problem was that hidden options were being grep-ed out of the specs
for the purpose of forming the leader text for the usage line, and those
same hidden-option-less specs were being used as the actual specs.
The module patch is bigger than I'd like; similar, but not identical,
data is needed in several places and fixing this bug without duplicating
code involved turning some neat-looking map-s and grep-s into for loops
with push-es inside them.
I think it's still quite readable, though obviously you may disagree.
Cheers.
Smylers
Subject: | hidden_works.patch |
diff --git a/Changes b/Changes
index 459394d..059bb1b 100644
--- a/Changes
+++ b/Changes
@@ -1,6 +1,7 @@
Revision history for Getopt-Long-Descriptive
{{$NEXT}}
+ hidden options can still be used (Smylers)
0.090 2011-04-21 20:43:07 America/New_York
bump prereq on Params::Validate to deal with recent buggy versions
diff --git a/lib/Getopt/Long/Descriptive.pm b/lib/Getopt/Long/Descriptive.pm
index 2b17749..eaab6a4 100644
--- a/lib/Getopt/Long/Descriptive.pm
+++ b/lib/Getopt/Long/Descriptive.pm
@@ -266,10 +266,6 @@ my %CONSTRAINT = (
our $MungeOptions = 1;
-sub _nohidden {
- return grep { ! $_->{constraint}->{hidden} } @_;
-}
-
sub _expand {
return map { {(
spec => $_->[0] || '',
@@ -353,19 +349,27 @@ sub _build_describe_options {
# not entirely sure that all of this (until the Usage->new) shouldn't be
# moved into Usage -- rjbs, 2009-08-19
- my @specs =
- map { $_->{spec} }
- grep { $_->{desc} ne 'spacer' }
- _nohidden(@opts);
+ my (@specs, @visible_opts, @short, $long);
+ for (@opts) {
+ push @specs, $_->{spec} unless $_->{desc} eq 'spacer';
+
+ unless ($_->{constraint}->{hidden}) {
+ push @visible_opts, $_;
+
+ for (split /\|/, __PACKAGE__->_strip_assignment($_->{spec})) {
+ if (length == 1) {
+ push @short, $_;
+ }
+ else {
+ $long ||= 1;
+ }
+ }
+ }
+ }
my $short = join q{},
sort { lc $a cmp lc $b or $a cmp $b }
- grep { /^.$/ }
- map { split /\|/ }
- map { __PACKAGE__->_strip_assignment($_) }
- @specs;
-
- my $long = grep /\b[^|]{2,}/, @specs;
+ @short;
my %replace = (
"%" => "%",
@@ -380,14 +384,14 @@ sub _build_describe_options {
$str =~ s/\s{2,}/ /g;
my $usage = $class->usage_class->new({
- options => [ _nohidden(@opts) ],
+ options => \@visible_opts,
leader_text => $str,
});
Getopt::Long::Configure(@go_conf);
my %return;
- $usage->die unless GetOptions(\%return, grep { length } @specs);
+ $usage->die unless GetOptions(\%return, @specs);
my @given_keys = keys %return;
for my $opt (keys %return) {
diff --git a/t/descriptive.t b/t/descriptive.t
index 8801473..6f58000 100644
--- a/t/descriptive.t
+++ b/t/descriptive.t
@@ -2,7 +2,7 @@
use strict;
use warnings;
-use Test::More tests => 40;
+use Test::More tests => 42;
use_ok("Getopt::Long::Descriptive");
@@ -86,6 +86,13 @@ is_hidden(
qr/a bar option/,
);
+is_opt(
+ [ '--nora' ],
+ [ [ "nora", "Invisible Nora", { hidden => 1 } ] ],
+ { nora => 1 },
+ "",
+);
+
### tests for one_of
my $foobar = [