blib/lib/Getopt/Abridged.pm

filter: Statementstmt 99.1% 123 / 124 Branchbran 83.3% 45 / 54 Conditioncond 71.4% 15 / 21 MC/DCmcdc 41.6% 5 / 12 Subroutinesub 100% 12 / 12 Podpod 100% 2 / 2 totaltotal 89.7% 202 / 225 CC 41 SCAR 25.5
CC
41
Cov
92%
new
19.3
print_pod
16.2
__ANON__
3.2
CRAP 12.8
↑ Summary
1 package Getopt::Abridged;
2 2 $VERSION = v0.0.1;
3  
4 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 2 ▶use base 'Getopt::Base';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 =head1 NAME
11  
12 Getopt::Abridged - quick and simple full-featured option handling
13  
14 =head1 SYNOPSIS
15  
16 sub main {
17 my @args = @_;
18  
19 my $opt = Getopt::Abridged->new(
20 'w|world=s=world',
21 'g|greeting=s=hello',
22 'v|verbose=1',
23 'q|quiet=!verbose',
24 -positional => ['world'],
25 )->process(\@args) or return;
26  
27 print $opt->greeting, ' ', $opt->world, "\n" if($opt->verbose);
28 }
29  
30 main(@ARGV) if($0 eq __FILE__);
31  
32 =head1 About
33  
34 This module is provided as a shortcut for using Getopt::Base and to
35 support easily transitioning into Getopt::AsDocumented.
36  
37 =cut
38  
39 =head2 new
40  
41 my $opt = Getopt::Abridged->new(@opts, @args);
42  
43 =cut
44  
45 2 ▶sub new {
Subroutine new: called CC=19 SCAR=29.6
Pod documented
46 2 my $package = shift;
47 2 my (@args) = @_;
48 2 my $self = $package->SUPER::new();
49  
50 2 my $order = $self->{defined_options} = [];
51  
52 my %opt_do = (
53 1 ▶ -positional => sub {
Subroutine __ANON__: called CC=3 SCAR=11.7
54 1 my $list = shift;
55 1 ▶ ((ref($list)||'') eq 'ARRAY') or
Branch: 1 branch
BranchTrueFalseTotal
(ref $list || "") eq "ARRAY" or croak("'positional' value must be an array-ref") 1 0 1
Condition50%1/2
ref $list || ''
l!l
10
MC/DC0%0/1
ref $list || ''
ref $list
Truth table50%1/2
A
A= ref $list
Aresult
11
00
56 croak("'positional' value must be an array-ref");
57 1 $self->add_positionals(@$list);
58 },
59 2 );
60 2 my %type_map = (
61 s => 'string',
62 i => 'integer',
63 n => 'number',
64 );
65 2 ▶ while(@args) {
Branch: 1 branch
BranchTrueFalseTotal
if (@args) 13 2 15
66 13 my $opt = shift(@args);
67 13 ▶ if($opt =~ m/^-/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt =~ /^-/) { } 1 12 13
68 1 ▶ @args or croak("'$opt' must have a value");
Branch: 1 branch
BranchTrueFalseTotal
@args or croak("'${opt}' must have a value") 1 0 1
69 1 my $val = shift(@args);
70 1 ▶ my $do = $opt_do{$opt} or croak("no such option: $opt");
Branch: 1 branch
BranchTrueFalseTotal
my $do = $opt_do{$opt} or croak("no such option: $opt") 1 0 1
71 1 $do->($val);
72 }
73 else {
74 12 my ($spec, $type, @def) = split(/=/, $opt, 3);
75 12 my %setup;
76 12 ▶ if(! defined($type)) {
Branch: 2 branches
BranchTrueFalseTotal
if (not defined $type) { } 1 11 12
elsif ($type =~ s/^(\@|\%)//) { } 4 7 11
77 1 $type = '0';
78 }
79 elsif($type =~ s/^(\@|\%)//) {
80 4 ▶ $setup{form} = ($1 eq '@' ? 'ARRAY' : 'HASH');
Branch: 1 branch
BranchTrueFalseTotal
$1 eq '@' ? : 3 1 4
81 }
82  
83 12 my @spec = split(/\|/, $spec);
84 12 my @short;
85 12 ▶ push(@short, shift(@spec))
Branch: 1 branch
BranchTrueFalseTotal
@spec and length $spec[0] == 1 and do { push @short, shift @spec } 8 12 20
Condition66%2/3
@spec and length $spec[0] == 1
!ll&&!rl&&r
0128
MC/DC50%1/2
@spec and length $spec[0] == 1
@spec length $spec[0] == 1
Truth table66%2/3
A and B
A= @specB= length $spec[0] == 1
ABresult
0X0
100
111
86 while(@spec and length($spec[0]) == 1);
87  
88 12 my $long = pop(@spec);
89  
90 12 ▶ if(@def) {
Branch: 3 branches
BranchTrueFalseTotal
if (@def) { } 6 6 12
elsif ($type =~ /^[10]/) { } 2 4 6
elsif ($type =~ s/^\!//) { } 2 2 4
91 6 my $def = shift(@def);
92 $setup{default} = $setup{form} ?
93 6 ▶ ($setup{form} eq 'ARRAY' ?
Branch: 2 branches
BranchTrueFalseTotal
$setup{'form'} ? : 2 4 6
$setup{'form'} eq 'ARRAY' ? : 1 1 2
94 [split(/,/, $def)] : {split(/,|=/, $def)}
95 ) : $def;
96 }
97 elsif($type =~ m/^[10]/) {
98 2 $setup{type} = 'boolean';
99 2 $setup{default} = $type;
100 }
101 elsif($type =~ s/^\!//) {
102 2 my $name = 'no_' . $type;
103 2 push(@$order, $name);
104 2 $self->add_aliases($name => \@short, @spec, $long);
105 2 next;
106 }
107  
108 10 push(@$order, $long);
109  
110 10 ▶ $setup{type} ||= $type_map{$type} or
Branch: 1 branch
BranchTrueFalseTotal
$setup{"type"} ||= $type_map{$type} or croak("no such type '${type}'") 10 0 10
Condition66%2/3
$setup{"type"} ||= $type_map{$type}
l!l&&r!l&&!r
280
MC/DC0%0/2
$setup{"type"} ||= $type_map{$type}
$setup{"type"} $type_map{$type}
Truth table66%2/3
A ||= B
A= $setup{"type"}B= $type_map{$type}
ABresult
1X1
011
000
111 croak("no such type '$type'");
112  
113 10 ▶ $setup{short} = [@short] if(@short);
Branch: 1 branch
BranchTrueFalseTotal
if @short 6 4 10
114 10 ▶ $setup{aliases} = [@spec] if(@spec);
Branch: 1 branch
BranchTrueFalseTotal
if @spec 0 10 10
115 10 $self->add_option($long => %setup);
116 }
117 }
118  
119 2 return($self);
120 } # new ################################################################
121  
122 =head2 import
123  
124 This translates your options into pod for use with Getopt::AsDocumented.
125  
126 Getopt::Abridged->import('pod');
127  
128 =cut
129  
130 2 my %installed;
131 4 ▶sub import {
Subroutine import: called CC=3 SCAR=11.0
132 4 my $package = shift;
133 4 ▶ my @arg = @_ or return;
Branch: 1 branch
BranchTrueFalseTotal
my(@arg) = or return 2 2 4
134  
135 2 ▶ (@arg == 1) or croak("usage: import('pod')");
Branch: 1 branch
BranchTrueFalseTotal
@arg == 1 or croak("usage: import('pod')") 2 0 2
136 2 ▶ my $process = sub {
Subroutine __ANON__: called CC=2 SCAR=6.9
137 2 my $self = shift;
138 2 ▶ @_ and croak("should have no arguments!");
Branch: 1 branch
BranchTrueFalseTotal
@_ and croak("should have no arguments!") 1 1 2
139 1 $self->print_pod;
140 1 return();
141 2 };
142  
143 2 $installed{process} = 1;
144 2 ▶ { no strict 'refs'; *{$package . '::process'} = $process};
Subroutine BEGIN: called CC=1 SCAR=0.0
145  
146 } # import #############################################################
147  
148 =head2 unimport
149  
150 Getopt::Abridged->unimport;
151  
152 =cut
153  
154 1 ▶sub unimport {
Subroutine unimport: called CC=3 SCAR=11.0
155 1 my $package = shift;
156 1 foreach my $key (keys %installed) {
157 1 ▶ delete($installed{$key}) or next;
Branch: 1 branch
BranchTrueFalseTotal
delete $installed{$key} or next 1 0 1
158 2 ▶ my $st = do {no strict 'refs'; \%{$package . '::'}};
Subroutine BEGIN: called CC=1 SCAR=0.0
159 1 delete($st->{$key});
160 }
161 } # unimport ###########################################################
162  
163 2 our $PODHANDLE;
164  
165 =head2 print_pod
166  
167 This is activated via import and the call to process(). If your
168 application is written to return/exit when process() returns false, you
169 may simply do:
170  
171 perl -MGetopt::Abridged=pod -S your_program
172  
173 You will then want to see L<Getopt::AsDocumented> and change your
174 process() call to:
175  
176 Getopt::AsDocumented->process(\@args) or return;
177  
178 The builtin --version and --help options will be automatically included
179 in the pod output.
180  
181 TODO C<=for positional> directives need to be printed from here.
182  
183 =cut
184  
185 1 ▶sub print_pod {
Subroutine print_pod: called CC=16 SCAR=27.8
Pod documented
186 1 my $self = shift;
187  
188 1 ▶ my $fh = $PODHANDLE || \*STDOUT;
Condition50%1/2
$Getopt::Abridged::PODHANDLE || \*STDOUT
l!l
10
MC/DC0%0/1
$Getopt::Abridged::PODHANDLE || \*STDOUT
$Getopt::Abridged::PODHANDLE
Truth table50%1/2
A
A= $Getopt::Abridged::PODHANDLE
Aresult
11
00
189  
190 1 require File::Basename;
191 1 my $name = File::Basename::basename($0);
192 1 print $fh "=head1 Usage\n\n $name [options]\n\n";
193 1 print $fh "=head1 Options\n\n=over\n\n";
194  
195 # Best way I can think of at the moment to get them to come out in the
196 # same order:
197 my $order = $self->{defined_options} or
198 1 ▶ croak("must have defined some options for me to print pod");
Branch: 1 branch
BranchTrueFalseTotal
my $order = $self->{"defined_options"} or croak("must have defined some options for me to print pod") 1 0 1
199  
200 1 my $optd = $self->{opt_data};
201  
202 1 my $short = $self->{short};
203 1 my %shortmap;
204 1 foreach my $s (keys %$short) {
205 4 ▶ my $list = $shortmap{$short->{$s}} ||= [];
Condition50%1/2
$shortmap{$short->{$s}} ||= []
l!l
04
MC/DC0%0/1
$shortmap{$short->{$s}} ||= []
$shortmap{$short->{$s}}
Truth table50%1/2
A
A= $shortmap{$short->{$s}}
Aresult
11
00
206 4 push(@$list, $s);
207 }
208  
209 1 my $aliases = $self->{aliases};
210 1 my %aliasmap;
211 1 foreach my $n (keys %$aliases) {
212 1 ▶ my $list = $aliasmap{$aliases->{$n}} ||= [];
Condition50%1/2
$aliasmap{$aliases->{$n}} ||= []
l!l
01
MC/DC0%0/1
$aliasmap{$aliases->{$n}} ||= []
$aliasmap{$aliases->{$n}}
Truth table50%1/2
A
A= $aliasmap{$aliases->{$n}}
Aresult
11
00
213 1 push(@$list, $n);
214 }
215  
216  
217 1 foreach my $canon (@$order) {
218 5 my $item = $optd->{$canon};
219 5 ▶ my $short = $shortmap{$canon} || [];
Condition100%2/2
$shortmap{$canon} || []
l!l
41
MC/DC100%1/1
$shortmap{$canon} || []
$shortmap{$canon}
Truth table100%2/2
A
A= $shortmap{$canon}
Aresult
11
00
220 5 ▶ my $alias = $aliasmap{$canon} || [];
Condition100%2/2
$aliasmap{$canon} || []
l!l
14
MC/DC100%1/1
$aliasmap{$canon} || []
$aliasmap{$canon}
Truth table100%2/2
A
A= $aliasmap{$canon}
Aresult
11
00
221  
222 print $fh "=item ",
223 4 join(', ', map({"-$_"} @$short),
224 5 map({s/_/-/g; $_} map({"--$_"} @$alias, $canon)));
225  
226 5 ▶ if($item->{type} ne 'boolean') {
Branch: 1 branch
BranchTrueFalseTotal
if ($item->{"type"} ne "boolean") 3 2 5
227 3 my $example = uc($canon);
228 3 ▶ if(my $f = $item->{form}) {
Branch: 1 branch
BranchTrueFalseTotal
if (my $f = $item->{"form"}) 1 2 3
229 1 ▶ if($f eq 'HASH') {
Branch: 1 branch
BranchTrueFalseTotal
if ($f eq 'HASH') { } 0 1 1
230 0 $example = 'NAME=' . $example;
231 }
232 else {
233 1 $example .= ' [' . (map({s/_/-/g; $_} '--' . $canon))[0] .
234 ' ...]';
235 }
236 }
237  
238 3 ▶ $example .= ' ' . "($item->{type})" if($item->{type} ne 'string');
Branch: 1 branch
BranchTrueFalseTotal
if $item->{"type"} ne "string" 1 2 3
239  
240 3 print $fh " ", $example;
241 }
242 5 print $fh "\n\nThe $canon.\n\n";
243 5 ▶ if(defined($item->{default}) and $canon !~ m/^no_/) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $item->{"default"} and not $canon =~ /^no_/) 3 2 5
Condition100%3/3
defined $item->{"default"} and not $canon =~ /^no_/
!ll&&!rl&&r
113
MC/DC100%2/2
defined $item->{"default"} and not $canon =~ /^no_/
defined $item->{"default"} not $canon =~ /^no_/
Truth table100%3/3
A and not(B)
A= defined $item->{"default"}B= not $canon =~ /^no_/
ABresult
0X0
100
111
244 3 print $fh "DEFAULT: $item->{default}\n\n";
245 }
246 }
247  
248 1 print $fh "=item --version\n\nPrint version number and quit.\n\n";
249 1 print $fh "=item -h, --help\n\nShow help about options.\n\n";
250  
251 1 print $fh "=back\n\n=cut\n\n";
252 } # print_pod ##########################################################
253  
254 =head1 AUTHOR
255  
256 Eric Wilhelm @ <ewilhelm at cpan dot org>
257  
258 http://scratchcomputing.com/
259  
260 =head1 BUGS
261  
262 If you found this module on CPAN, please report any bugs or feature
263 requests through the web interface at L<http://rt.cpan.org>. I will be
264 notified, and then you'll automatically be notified of progress on your
265 bug as I make changes.
266  
267 If you pulled this development version from my /svn/, please contact me
268 directly.
269  
270 =head1 COPYRIGHT
271  
272 Copyright (C) 2009 Eric L. Wilhelm, All Rights Reserved.
273  
274 =head1 NO WARRANTY
275  
276 Absolutely, positively NO WARRANTY, neither express or implied, is
277 offered with this software. You use this software at your own risk. In
278 case of loss, no person or entity owes you anything whatsoever. You
279 have been warned.
280  
281 =head1 LICENSE
282  
283 This program is free software; you can redistribute it and/or modify it
284 under the same terms as Perl itself.
285  
286 =cut
287  
288 # vi:ts=2:sw=2:et:sta
289 2 1;
↑ Summary