1 package Getopt::Long::Descriptive::Usage;
5 our $VERSION = '0.082';
7 use List::Util qw(max);
11 Getopt::Long::Descriptive::Usage - the usage description for GLD
15 use Getopt::Long::Descriptive;
16 my ($opt, $usage) = describe_options( ... );
18 $usage->text; # complete usage message
20 $usage->die; # die with usage message
24 This document only describes the methods of the Usage object. For information
25 on how to use L<Getopt::Long::Descriptive>, consult its documentation.
31 my $usage = Getopt::Long::Descriptive::Usage->new(\%arg);
33 You B<really> don't need to call this. GLD will do it for you.
37 options - an arrayref of options
38 leader_text - the text that leads the usage; this may go away!
43 my ($class, $arg) = @_;
45 my @to_copy = qw(options leader_text);
48 @copy{ @to_copy } = @$arg{ @to_copy };
50 bless \%copy => $class;
55 This returns the full text of the usage message.
62 return join qq{\n}, $self->leader_text, $self->option_text;
67 This returns the text that comes at the beginning of the usage message.
71 sub leader_text { $_[0]->{leader_text} }
75 This returns the text describing the available options.
82 my @options = @{ $self->{options} || [] };
85 # a spec can grow up to 4 characters in usage output:
86 # '-' on short option, ' ' between short and long, '--' on long
87 my @specs = map { $_->{spec} } grep { $_->{desc} ne 'spacer' } @options;
88 my $length = (max(map { length } @specs) || 0) + 4;
89 my $spec_fmt = "\t%-${length}s";
92 my $opt = shift @options;
93 my $spec = $opt->{spec};
94 my $desc = $opt->{desc};
95 if ($desc eq 'spacer') {
96 $string .= sprintf "$spec_fmt\n", $opt->{spec};
100 $spec = Getopt::Long::Descriptive->_strip_assignment($spec);
101 $spec = join " ", reverse map { length > 1 ? "--$_" : "-$_" }
103 $string .= sprintf "$spec_fmt %s\n", $spec, $desc;
111 This warns with the usage message.
115 sub warn { warn shift->text }
119 This throws the usage message as an exception.
121 $usage_obj->die(\%arg);
123 Some arguments can be provided
125 pre_text - text to be prepended to the usage message
126 post_text - text to be appended to the usage message
128 The C<pre_text> and C<post_text> arguments are concatenated with the usage
129 message with no line breaks, so supply this if you need them.
135 my $arg = shift || {};
138 join q{}, grep { defined } $arg->{pre_text}, $self->text, $arg->{post_text}
145 # This is only needed because Usage used to be a blessed coderef that worked
146 # this way. Later we can toss a warning in here. -- rjbs, 2009-08-19
149 Carp::cluck("use of __PACKAGE__ objects as a code ref is deprecated");
150 return sub { return $_[0] ? $self->text : $self->warn; };
156 Hans Dieter Pearcey, C<< <hdp@cpan.org> >>
160 Please report any bugs or feature requests through the web interface at
161 L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=Getopt-Long-Descriptive>. I
162 will be notified, and then you'll automatically be notified of progress on your
163 bug as I make changes.
165 =head1 COPYRIGHT & LICENSE
167 Copyright 2005 Hans Dieter Pearcey, all rights reserved.
169 This program is free software; you can redistribute it and/or modify it
170 under the same terms as Perl itself.