Skip to content

Commit

Permalink
add indication that an option takes a value to the help message
Browse files Browse the repository at this point in the history
The help message didn't specify that an option took a value.

Here's before and after for the option specification in t/11-usage.t

Usage: 11-usage.t [-rv] [long options] [arguments]
	--bare
	--define                 (default: arch=i386, isize=4)
	--input                  (default: test.txt)
	--libs                   (default: one, two)
	-r                       recursive
	--test                   run in test mode
	-v, --verbose            turn on verbose output (default: 2)

Usage: 11-usage.t [-rv] [long options] [arguments]
	--bare <value>
	--define key=<value>     (default: arch=i386, isize=4)
	--input <value>          (default: test.txt)
	--libs <value>           (default: one, two)
	-r                       recursive
	--test                   run in test mode
	-v [<value>], --verbose [<value>] turn on verbose output (default: 2)
  • Loading branch information
djerius committed Dec 3, 2022
1 parent e39a85a commit ef5a3cf
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 8 deletions.
13 changes: 10 additions & 3 deletions lib/Getopt/Lucid.pm
Original file line number Diff line number Diff line change
Expand Up @@ -385,7 +385,7 @@ sub usage {
for my $opt ( sort { $a->{strip} cmp $b->{strip} } values %{$self->{spec}} ) {
my $names = [ @{ $opt->{names} } ];
push @doc, [
_build_usage_left_column( $names, \@short_opts ),
_build_usage_left_column( $names, \@short_opts, $opt->{type} ),
_build_usage_right_column( $opt->{doc}, $opt->{default}, $opt->{type} ),
];
}
Expand All @@ -400,7 +400,7 @@ sub usage {
}

sub _build_usage_left_column {
my ($names, $all_short_opts) = @_;
my ($names, $all_short_opts, $type) = @_;
my @sorted_names =
sort { length $a <=> length $b } map { my $s = $_; $s =~ s/^-*//; $s } @$names;

Expand All @@ -409,13 +409,20 @@ sub _build_usage_left_column {

push @$all_short_opts, @short_opts;

my $value =
$type eq 'keypair' ? ' key=<value>'
: $type eq 'counter' ? ' [<value>]'
: $type ne 'switch' ? ' <value>'
: ''
;

my $group = sub {
my $list = shift;
'-' . ( @$list == 1 ? $list->[0] : '[' . join( '|', @$list ) . ']' );
};
my $prepare = sub {
my $list = shift;
return ( length $list->[0] > 1 ? '-' : '' ) . $group->($list) if @$list;
return ( length $list->[0] > 1 ? '-' : '' ) . $group->($list) . $value if @$list;
return;
};

Expand Down
10 changes: 5 additions & 5 deletions t/11-usage.t
Original file line number Diff line number Diff line change
Expand Up @@ -28,13 +28,13 @@ my $spec = [

my @expectations = (
qr/^Usage: \Q$prog\E \[-rv] \[long options] \[arguments]$/,
qr/^\s+--bare\s*$/,
qr/^\s+--define\s+\(default: arch=i386, isize=4\)$/,
qr/^\s+--input\s+\(default: test\.txt\)$/,
qr/^\s+--libs\s+\(default: one, two\)$/,
qr/^\s+--bare <value>\s*$/,
qr/^\s+--define key=<value>\s+\(default: arch=i386, isize=4\)$/,
qr/^\s+--input <value>\s+\(default: test\.txt\)$/,
qr/^\s+--libs <value> \s+\(default: one, two\)$/,
qr/^\s+-r\s+recursive$/,
qr/^\s+--test\s+run in test mode$/,
qr/^\s+-v, --verbose\s+turn on verbose output \(default: 2\)$/,
qr/^\s+-v \[<value>\], --verbose \[<value>\]\s+turn on verbose output \(default: 2\)$/,
);

plan tests => 2 + @expectations;
Expand Down

0 comments on commit ef5a3cf

Please sign in to comment.