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 SCAR 53.4
CC
30
Cov
42%
import
74.8
show_help
72.0
GetOptions
19.9
CRAP 207.7
↑ Summary Build »
1 package Getopt::Lazy;
2  
3 2 require 5.006;
4  
5 2 ▶use version;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 our $VERSION = qv('0.0.6');
7  
8 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 2 ▶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 getopt;
27 print usage and exit 1 unless @ARGV;
28  
29 =head1 DESCRIPTION
30  
31 Getting tired of the digging the same tedious "getopt" things for every script
32 that you write? This module works for you!
33  
34 =head2 Without Getopt::Lazy
35  
36 Normally we started a script this way:
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 ...or, being a little bit "lazier" than usual:
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 getopt;
83 print usage and exit 1 unless @ARGV;
84  
85 =head2 What We've Got?
86  
87 The usage() will display the following help screen for you:
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 Basically the Getopt::Lazy does two things for you: 1) spawning a variable for
101 every given option, and 2) generating GNU-style help message. (Guess I was too
102 lazy to put this down in details. See for yourself!) If you have other even
103 lazier ways of doing the same thing, be sure to let me know!
104  
105 =head1 INTERFACE
106  
107 =cut
108  
109 2 ▶use Carp;
Subroutine BEGIN: called CC=1 SCAR=0.0
110  
111 2 our @ISA = qw/Exporter/;
112 2 our @EXPORT = qw/GetOptions/;
113  
114 2 our %opt = ();
115 2 our %usage = ();
116 2 our %conf = ();
117  
118 =over 2
119  
120 =item show_help
121  
122 Show the help screen
123  
124 =back
125  
126 =cut
127  
128 sub show_help {
129 2 ▶ use File::Basename;
Subroutine BEGIN: called CC=1 SCAR=0.0
130 2 ▶ use Text::Wrap;
Subroutine BEGIN: called CC=1 SCAR=0.0
131  
132 0 ▶ my $msg = shift;
Subroutine show_help: not called CC=8 SCAR=42.8
Pod documented
133 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
134 0 my $summary = $conf{summary};
135 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
136 0 $usage =~ s/\%c\b/$cmd/g;
137 0 $usage =~ s/\%o\b/[options..]/g;
138  
139 0 ▶ print $msg, "\n" if defined $msg;
Branch: 1 branch
BranchTrueFalseTotal
if defined $msg 0 0 0
140 0 ▶ print "$cmd - $summary\n" if defined $summary;
Branch: 1 branch
BranchTrueFalseTotal
if defined $summary 0 0 0
141 0 ▶ print "usage: $usage\n" if defined $usage;
Branch: 1 branch
BranchTrueFalseTotal
if defined $usage 0 0 0
142 0 ▶ return unless keys %usage;
Branch: 1 branch
BranchTrueFalseTotal
unless keys %Getopt::Lazy::usage 0 0 0
143  
144 0 print "options:\n";
145 0 my $size = 8 * int (((reverse sort { $a <=> $b } map length $_, keys %usage)[0] + 8) / 8);
146 0 for (sort keys %usage) {
147 0 printf "\t%-${size}s%s\n", $_, $usage{$_};
148 }
149  
150 0 1;
151 }
152  
153 sub import {
154 1 ▶ my $pkg = shift;
Subroutine import: called CC=18 SCAR=43.1
155 1 my %o = @_;
156  
157 1 ▶ $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
158  
159 1 for (keys %o) {
160 1 ▶ m/^-(\w+)$/ and do { $conf{$1} = $o{$_}; next; };
Branch: 1 branch
BranchTrueFalseTotal
/^-(\w+)$/ and do { $conf{$1} = $o{$_}; next } 0 1 1
161  
162 1 my ($type, $spec, $name) = m/^([\&\@\%\$])?((.+?)(?:\|.*)?(?:\=.*)?)$/;
163 1 my $guess = undef;
164 1 (my $var = $name) =~ s/-/_/g;
165 1 ▶ push @EXPORT, ($type || $guess || '$') . $var;
Condition40%2/5
$type || $guess || '$'
l!l
10
$type or $guess
l!l&&r!l&&!r
100
MC/DC0%0/2
$type || $guess || '$'
$type $guess
Truth table33%1/3
A or B
A= $typeB= $guess
ABresult
1X1
011
000
166  
167 1 my $item = "--$name";
168 1 ▶ my ($desc, @def) = ref $o{$_} eq 'ARRAY' ? @{$o{$_}}: $o{$_};
Branch: 1 branch
BranchTrueFalseTotal
ref $o{$_} eq 'ARRAY' ? : 1 0 1
169 1 ▶ $spec =~ /\|(\w+)=/ and $item .= ", -$1";
Branch: 1 branch
BranchTrueFalseTotal
$spec =~ /\|(\w+)=/ and $item .= ", -$1" 0 1 1
170 1 ▶ $desc =~ s/^\[([A-Z_-]+)\]\s*// and $item .= " $1";
Branch: 1 branch
BranchTrueFalseTotal
$desc =~ s/^\[([A-Z_-]+)\]\s*// and $item .= " $1" 0 1 1
171 1 $usage{$item} = $desc;
172  
173 2 ▶ no strict 'refs';
Subroutine BEGIN: called CC=1 SCAR=0.0
174 (not defined $type or $type eq '$') and do {
175 0 ▶ $usage{$item} .= " (default: $def[0])" if $def[0];
Branch: 1 branch
BranchTrueFalseTotal
if $def[0] 0 0 0
176 0 ${"$var"} = shift @def;
177 0 ▶ $opt{$spec} = *{"$var"}{SCALAR};
Condition33%1/3
$type eq "\@" and do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} }
!ll&&!rl&&r
100
MC/DC0%0/2
$type eq "\@" and do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} }
$type eq "\@" do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} }
Truth table33%1/3
A and B
A= $type eq "\@"B= do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} }
ABresult
0X0
100
111
178 } or $type eq '@' and do {
179 0 ▶ $usage{$item} .= " (default: ".join(',', @def).")" if @def > 0;
Branch: 1 branch
BranchTrueFalseTotal
if @def > 0 0 0 0
180 0 @{"$var"} = (@def);
181 0 ▶ $opt{$spec} = *{"$var"}{ARRAY};
Condition33%1/3
$type eq "%" and do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{"HASH"} }
!ll&&!rl&&r
100
MC/DC0%0/2
$type eq "%" and do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{"HASH"} }
$type eq "%" do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{"HASH"} }
Truth table33%1/3
A and B
A= $type eq "%"B= do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{"HASH"} }
ABresult
0X0
100
111
182 } or $type eq '%' and do {
183 0 %{"$var"} = (@def);
184 0 ▶ $opt{$spec} = *{"$var"}{HASH};
Condition33%1/3
$type eq "&" and do { my $code = shift @def; $opt{$spec} = $code }
!ll
010
MC/DC0%0/2
$type eq "&" and do { my $code = shift @def; $opt{$spec} = $code }
$type eq "&" do { my $code = shift @def; $opt{$spec} = $code }
Truth table33%1/3
A and B
A= $type eq "&"B= do { my $code = shift @def; $opt{$spec} = $code }
ABresult
0X0
100
111
185 1 ▶ } or $type eq '&' and do {
Branch: 1 branch
BranchTrueFalseTotal
!defined($type) || $type eq "\$" and do { $usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $opt{$spec} = *{"$var";}{"SCALAR"} } or $type eq "\@" and do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} } or $type eq "%" and do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{"HASH"} } or $type eq "&" and do { my $code = shift @def; $opt{$spec} = $code } 1 0 1
Condition33%5/15
(!defined($type) || $type eq '$') && do { $usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $opt{$spec} = *{"$var";}{'SCALAR'} } || $type eq '@' && do { $usage{$item} .= ' (default: ' . join(',', @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{'ARRAY'} } || $type eq '%' && do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{'HASH'} } || $type eq '&' && do { my $code = shift @def; $opt{$spec} = $code }
l!l&&r!l&&!r
010
!defined($type) || $type eq "\$" and do { $usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $opt{$spec} = *{"$var";}{"SCALAR"} } or $type eq "\@" and do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} } or $type eq "%" and do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{"HASH"} }
l!l&&r!l&&!r
001
!defined($type) || $type eq "\$" and do { $usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $opt{$spec} = *{"$var";}{"SCALAR"} } or $type eq "\@" and do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} }
l!l&&r!l&&!r
001
!defined($type) || $type eq "\$" and do { $usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $opt{$spec} = *{"$var";}{"SCALAR"} }
!ll&&!rl&&r
100
not defined $type or $type eq "\$"
l!l&&r!l&&!r
001
MC/DC0%0/6
(!defined($type) || $type eq '$') && do { $usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $opt{$spec} = *{"$var";}{'SCALAR'} } || $type eq '@' && do { $usage{$item} .= ' (default: ' . join(',', @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{'ARRAY'} } || $type eq '%' && do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{'HASH'} } || $type eq '&' && do { my $code = shift @def; $opt{$spec} = $code }
not defined $type $type eq "\$" do { $usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $opt{$spec} = *{"$var";}{"SCALAR"} } $type eq "\@" and do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} } $type eq "%" and do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{"HASH"} } $type eq '&' && do { my $code = shift @def; $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 { $usage{$item} .= " (default: $def[0])" if $def[0]; ${"$var";} = shift @def; $opt{$spec} = *{"$var";}{"SCALAR"} }D= $type eq "\@" and do { $usage{$item} .= ' (default: ' . join(",", @def) . ')' if @def > 0; @{"$var";} = @def; $opt{$spec} = *{"$var";}{"ARRAY"} }E= $type eq "%" and do { %{"$var";} = @def; $opt{$spec} = *{"$var";}{"HASH"} }F= $type eq '&' && do { my $code = shift @def; $opt{$spec} = $code }
ABCDEFresult
1X1XXX1
011XXX1
00X1XX1
1X01XX1
0101XX1
00X01X1
1X001X1
01001X1
00X0011
1X00011
0100011
00X0000
1X00000
0100000
186 1 my $code = shift @def;
187 1 $opt{$spec} = $code;
188 };
189 }
190  
191 1 $pkg->export_to_level(1, undef, @EXPORT);
192 }
193  
194 =over 2
195  
196 =item GetOptions
197  
198 Make Getopt::Long work!
199  
200 =back
201  
202 =cut
203  
204 sub GetOptions {
205 0 ▶ my %o = @_;
Subroutine GetOptions: not called CC=5 SCAR=29.9
Pod documented
206  
207 2 ▶ use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
208 0 Getopt::Long::GetOptions %opt;
209  
210 0 ▶ return unless defined $o{-autohelp};
Branch: 1 branch
BranchTrueFalseTotal
unless defined $o{"-autohelp"} 0 0 0
211 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
212 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
213 }
214  
215  
216 2 1; # Magic true value required at end of module
217 __END__
↑ Summary Build »