blib/lib/Getopt/Lazy.pm

filter: Statementstmt 57.3% 51 / 89 Branchbran 16.6% 5 / 30 Conditioncond 25.6% 10 / 39 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 75.0% 9 / 12 Podpod 100% 2 / 2 totaltotal 40.3% 77 / 191 CC 30 SCAR 37.9
CC
30
Cov
42%
import
74.8
show_help
44.4
GetOptions
19.9
CRAP 44.3
↑ Summary
1 package Getopt::Lazy;
2  
3 3 require 5.006;
4  
5 3 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 3 our $VERSION = qv('0.0.7');
7  
8 3 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 3 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 =head1 NAME
12  
13 Getopt::Lazy - Yet another lazy, minimal way of using Getopt::Long
14  
15 =head1 SYNOPSIS
16  
17 use Getopt::Lazy
18 'help|h' => 'Show this help screen',
19 'verbose|v' => 'Show verbose output',
20 'output|o=s' => ["[FILE] Send the output to FILE", 'getopt.out'],
21 'output-encoding=s' => ['[ENCODING] Specify the output encoding', 'utf8'],
22 -summary => 'a simple example usage of Getopt::Lazy',
23 -usage => '%c %o file1 [file2 ..]',
24 ;
25  
26 GetOptions;
27 show_help and exit 1 unless @ARGV;
28  
29 =head1 DESCRIPTION
30  
31 Got tired of the same tedious "getopt" things in every script
32 you wrote? This module works for you!
33  
34 =head2 Without Getopt::Lazy
35  
36 Normally your script would go like this:
37  
38 use File::Basename;
39 use Getopt::Long;
40  
41 sub usage {
42 my $msg = shift;
43 my $cmd = basename $0;
44 print $msg, "\n" if defined $msg;
45 print <<__USAGE__;
46 $cmd - Yet another tool for whatever you like
47 Usage: $cmd [options...] file [more-file]
48 Options:
49 --boolean,-b Turn on the function A
50 --string, -s STRING Specify the name of blahblah (defaults 'blahblah')
51 --another-string, -as STRING Specify an alias for blahblah (defaults 'blahblah')
52 ...
53 __USAGE__
54 }
55  
56 my $boolean = 0;
57 my $string = 'blahblah';
58 my $another_string = 'blahblah';
59 ...
60  
61 GetOptions(
62 'boolean|b' => \$boolean,
63 'string|s=s' => \$string,
64 'another-string|as=s' => \$string,
65 ...
66 );
67  
68 usage and exit unless @ARGV;
69  
70 =head2 With Getopt::Lazy
71  
72 The same thing using Getopt::Lazy:
73  
74 use Getopt::Lazy
75 'boolean|b' => 'Turn on the function A',
76 'string|s=s' => ['[STRING] Specify the name of blahblah' => 'blahblah'],
77 'another-string|as=s' => ['[STRING] Specify an alias for blahblah' => 'blahblah'],
78 -summary => 'Yet another tool for whatever you like',
79 -usage => '%c %o file1 [more-file]',
80 ;
81  
82 GetOptions;
83 show_help and exit 1 unless @ARGV;
84  
85 =head2 What We've Got?
86  
87 show_help() simply prints out this help screen:
88  
89 $ lazy
90 lazy - Yet another tool for whatever you like
91 Usage: lazy [options..] file1 [more-file]
92 Options:
93 --another-string, -as STRING Specify an alias for blahblah (default: blahblah)
94 --boolean Turn on the function A
95 --string, -s STRING Specify the name of blahblah (default: blahblah)
96  
97  
98 =head2 Detail Usage
99  
100 Getopt::Lazy does two things for you: 1) spawning a variable for
101 every given option, and 2) generating GNU-style help messages.
102  
103 =head1 INTERFACE
104  
105 =cut
106  
107 3 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
108  
109 3 our @ISA = qw/Exporter/;
110 3 our @EXPORT = qw/GetOptions show_help/;
111  
112 3 our %opt = ();
113 3 our %usage = ();
114 3 our %conf = ();
115  
116 =over 2
117  
118 =item show_help
119  
120 Show the help screen
121  
122 =back
123  
124 =cut
125  
126 0 ▶sub show_help {
Subroutine show_help: not called CC=8 SCAR=37.9
Pod documented
127 3 ▶ use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
128 3 ▶ use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
129  
130 0 my $msg = shift;
131 0 ▶ my $cmd = $conf{cmd} || basename $0;
Condition0%0/3
$Getopt::Lazy::conf{'cmd'} || basename($0)
l!l&&r!l&&!r
000
MC/DC0%0/2
$Getopt::Lazy::conf{'cmd'} || basename($0)
$Getopt::Lazy::conf{'cmd'} basename($0)
Truth table0%0/3
A || B
A= $Getopt::Lazy::conf{'cmd'}B= basename($0)
ABresult
1X1
011
000
132 0 my $summary = $conf{summary};
133 0 ▶ my $usage = $conf{usage} || '%c %o';
Condition0%0/2
$Getopt::Lazy::conf{'usage'} || '%c %o'
l!l
00
MC/DC0%0/1
$Getopt::Lazy::conf{'usage'} || '%c %o'
$Getopt::Lazy::conf{'usage'}
Truth table0%0/2
A
A= $Getopt::Lazy::conf{'usage'}
Aresult
11
00
134 0 $usage =~ s/\%c\b/$cmd/g;
135 0 $usage =~ s/\%o\b/[options..]/g;
136  
137 0 ▶ print $msg, "\n" if defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 0 0 0
138 0 ▶ print "$cmd - $summary\n" if defined $summary;
Branch: 1 branch
BranchTrueFalseTotal
if defined $summary 0 0 0
139 0 ▶ print "usage: $usage\n" if defined $usage;
Branch: 1 branch
BranchTrueFalseTotal
if defined $usage 0 0 0
140 0 ▶ return unless keys %usage;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %Getopt::Lazy::usage 0 0 0
141  
142 0 print "options:\n";
143 0 my $size = 8 * int (((reverse sort { $a <=> $b } map length $_, keys %usage)[0] + 8) / 8);
144 0 for (sort keys %usage) {
145 0 printf "\t%-${size}s%s\n", $_, $usage{$_};
146 }
147  
148 0 1;
149 }
150  
151 2 ▶sub import {
Subroutine import: called CC=18 SCAR=43.1
152 2 my $pkg = shift;
153 2 my %o = @_;
154  
155 0 ▶ $o{"&help"} = ["Show this help screen", sub { show_help and exit }];
Subroutine __ANON__: not called CC=2 SCAR=13.0
Branch: 1 branch
BranchTrueFalseTotal
show_help() and exit 0 0 0
156  
157 2 for (keys %o) {
158 0 ▶ m/^-(\w+)$/ and do { $conf{$1} = $o{$_}; next; };
Branch: 1 branch
BranchTrueFalseTotal
/^-(\w+)$/ and do { $Getopt::Lazy::conf{$1} = $o{$_}; next } 0 2 2
159  
160 2 my ($type, $spec, $name) = m/^([\&\@\%\$])?((.+?)(?:\|.*)?(?:\=.*)?)$/;
161 2 my $guess = undef;
162 2 (my $var = $name) =~ s/-/_/g;
163 2 ▶ push @EXPORT, ($type || $guess || '$') . $var;
Condition40%2/5
$type || $guess || '$'
l!l
20
$type or $guess
l!l&&r!l&&!r
200
MC/DC0%0/2
$type || $guess || '$'
$type $guess
Truth table33%1/3
A or B
A= $typeB= $guess
ABresult
1X1
011
000
164  
165 2 my $item = "--$name";
166 2 ▶ my ($desc, @def) = ref $o{$_} eq 'ARRAY' ? @{$o{$_}}: $o{$_};
Branch: 1 branch
BranchTrueFalseTotal
ref $o{$_} eq 'ARRAY' ? : 2 0 2
167 2 ▶ $spec =~ /\|(\w+)=/ and $item .= ", -$1";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ /\|(\w+)=/ and $item .= ", -$1" 0 2 2
168 2 ▶ $desc =~ s/^\[([A-Z_-]+)\]\s*// and $item .= " $1";
Branch: 1 branch
BranchTrueFalseTotal
$desc =~ s/^\[([A-Z_-]+)\]\s*// and $item .= " $1" 0 2 2
169 2 $usage{$item} = $desc;
170  
171 3 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
172 (not defined $type or $type eq '$') and do {
173 0 ▶ $usage{$item} .= " (default: $def[0])" if $def[0];
Branch: 1 branch
BranchTrueFalseTotal
if $def[0] 0 0 0
174 0 ${"$var"} = shift @def;
175 0 ▶ $opt{$spec} = *{"$var"}{SCALAR};
Condition33%1/3
$type eq "\@" and do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} }
!ll&&!rl&&r
200
MC/DC0%0/2
$type eq "\@" and do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} }
$type eq "\@" do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} }
Truth table33%1/3
A and B
A= $type eq "\@"B= do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} }
ABresult
0X0
100
111
176 } or $type eq '@' and do {
177 0 ▶ $usage{$item} .= " (default: ".join(',', @def).")" if @def > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @def > 0 0 0 0
178 0 @{"$var"} = (@def);
179 0 ▶ $opt{$spec} = *{"$var"}{ARRAY};
Condition33%1/3
$type eq "%" and do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"HASH"} }
!ll&&!rl&&r
200
MC/DC0%0/2
$type eq "%" and do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"HASH"} }
$type eq "%" do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"HASH"} }
Truth table33%1/3
A and B
A= $type eq "%"B= do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"HASH"} }
ABresult
0X0
100
111
180 } or $type eq '%' and do {
181 0 %{"$var"} = (@def);
182 0 ▶ $opt{$spec} = *{"$var"}{HASH};
Condition33%1/3
$type eq "&" and do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code }
!ll
020
MC/DC0%0/2
$type eq "&" and do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code }
$type eq "&" do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code }
Truth table33%1/3
A and B
A= $type eq "&"B= do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code }
ABresult
0X0
100
111
183 2 ▶ } or $type eq '&' and do {
Branch: 1 branch
BranchTrueFalseTotal
!defined($type) || $type eq "\$" and do { $Getopt::Lazy::usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"SCALAR"} } or $type eq "\@" and do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} } or $type eq "%" and do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"HASH"} } or $type eq "&" and do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code } 0 2 2
Condition33%5/15
(!defined($type) || $type eq '$') && do { $Getopt::Lazy::usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{'SCALAR'} } || $type eq '@' && do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(',', @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{'ARRAY'} } || $type eq '%' && do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{'HASH'} } || $type eq '&' && do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code }
l!l&&r!l&&!r
020
!defined($type) || $type eq "\$" and do { $Getopt::Lazy::usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"SCALAR"} } or $type eq "\@" and do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} } or $type eq "%" and do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"HASH"} }
l!l&&r!l&&!r
002
!defined($type) || $type eq "\$" and do { $Getopt::Lazy::usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"SCALAR"} } or $type eq "\@" and do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} }
l!l&&r!l&&!r
002
!defined($type) || $type eq "\$" and do { $Getopt::Lazy::usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"SCALAR"} }
!ll&&!rl&&r
200
not defined $type or $type eq "\$"
l!l&&r!l&&!r
002
MC/DC0%0/6
(!defined($type) || $type eq '$') && do { $Getopt::Lazy::usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{'SCALAR'} } || $type eq '@' && do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(',', @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{'ARRAY'} } || $type eq '%' && do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{'HASH'} } || $type eq '&' && do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code }
not defined $type $type eq "\$" do { $Getopt::Lazy::usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"SCALAR"} } $type eq "\@" and do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} } $type eq "%" and do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"HASH"} } $type eq '&' && do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code }
Truth table0%0/14
not(A) or B and C or D or E || F
A= not defined $typeB= $type eq "\$"C= do { $Getopt::Lazy::usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"SCALAR"} }D= $type eq "\@" and do { $Getopt::Lazy::usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"ARRAY"} }E= $type eq "%" and do { %{"$var";} = @def; $Getopt::Lazy::opt{$spec} = *{"$var";}{"HASH"} }F= $type eq '&' && do { my $code = shift @def; $Getopt::Lazy::opt{$spec} = $code }
ABCDEFresult
1X1XXX1
011XXX1
00X1XX1
1X01XX1
0101XX1
00X01X1
1X001X1
01001X1
00X0011
1X00011
0100011
00X0000
1X00000
0100000
184 2 my $code = shift @def;
185 2 $opt{$spec} = $code;
186 };
187 }
188  
189 2 $pkg->export_to_level(1, undef, @EXPORT);
190 }
191  
192 =over 2
193  
194 =item GetOptions
195  
196 Make Getopt::Long work!
197  
198 =back
199  
200 =cut
201  
202 0 ▶sub GetOptions {
Subroutine GetOptions: not called CC=5 SCAR=29.9
Pod documented
203 0 my %o = @_;
204  
205 3 ▶ use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
206 0 Getopt::Long::GetOptions %opt;
207  
208 0 ▶ return unless defined $o{-autohelp};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o{"-autohelp"} 0 0 0
209 0 ▶ my $show_help = ref $o{-autohelp} eq "CODE"? $o{-autohelp}->(): scalar $o{-autohelp};
Branch: 1 branch
BranchTrueFalseTotal
ref $o{'-autohelp'} eq 'CODE' ? : 0 0 0
210 0 ▶ show_help and exit if $show_help;
Branch: 1 branch
BranchTrueFalseTotal
if $show_help 0 0 0
Condition0%0/5
$show_help && (show_help() && exit)
!ll&&!rl&&r
000
show_help() and exit
!ll
00
MC/DC0%0/2
$show_help && (show_help() && exit)
$show_help show_help()
Truth table0%0/3
A && B
A= $show_helpB= show_help()
ABresult
0X0
100
111
211 }
212  
213  
214 3 1; # Magic true value required at end of module
215 __END__
↑ Summary