blib/lib/App/optex/util/argv.pm

filter: Statementstmt 35.7% 35 / 98 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 20 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 44.0% 11 / 25 Podpod 62.5% 5 / 8 totaltotal 27.5% 51 / 185 CC 23 SCAR 31.0
CC
23
Cov
25%
collect
72.0
enable
42.0
times
20.0
CRAP 22.1
« script/optex ↑ Summary blib/lib/App/optex/help.pm »
1 package App::optex::util::argv;
2  
3 2 ▶use v5.10;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 2 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 2 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 2 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 2 ▶use Encode;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 2 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 2 my($mod, $argv);
11 1 ▶sub initialize { ($mod, $argv) = @_ }
Subroutine initialize: called CC=1 SCAR=0.0
Pod undocumented
12  
13 2 binmode STDIN, ":encoding(utf8)";
14 2 binmode STDOUT, ":encoding(utf8)";
15  
16 0 ▶sub argv (&) {
Subroutine argv: not called CC=1 SCAR=6.9
Pod undocumented
17 0 my $sub = shift;
18 0 @$argv = $sub->(@$argv);
19 }
20  
21 2 my %options = (
22 "move" => "\$<move(\$<shift>)>",
23 "remove" => "\$<remove(\$<shift>)>",
24 "copy" => "\$<copy(\$<shift>)>",
25 "exch" => "\$<move(1,1)>",
26 "times" => "-M__PACKAGE__::times(count=\$<shift>) \$<move>",
27 "reverse" => "-M__PACKAGE__::reverse() \$<move>",
28 "collect" => "-M__PACKAGE__::collect(\$<shift>) \$<move>",
29 "index" => "-M__PACKAGE__::collect(index=\$<shift>) \$<move>",
30 "glob" => "-M__PACKAGE__::collect(glob=\$<shift>) \$<move>",
31 "omit" => "-M__PACKAGE__::collect(omit=\$<shift>) \$<move>",
32 "match" => "-M__PACKAGE__::collect(match=\$<shift>) \$<move>",
33 "unmatch" => "-M__PACKAGE__::collect(unmatch=\$<shift>) \$<move>",
34 "include" => "-M__PACKAGE__::collect(match=\$<shift>) \$<move>",
35 "exclude" => "-M__PACKAGE__::collect(unmatch=\$<shift>) \$<move>",
36 "filter" => "-M__PACKAGE__::filter(command=\$<shift>) \$<move>",
37 );
38  
39 0 ▶sub enable {
Subroutine enable: not called CC=6 SCAR=37.4
Pod undocumented
40 0 my %opt = @_;
41 0 ▶ if ($opt{':all'}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{":all"}) 0 0 0
42 0 ▶ map { $opt{$_} //= 1 } keys %options;
Condition0%0/2
$opt{$_} //= 1
l!l
00
MC/DC0%0/1
$opt{$_} //= 1
$opt{$_}
Truth table0%0/2
A
A= $opt{$_}
Aresult
11
00
43 }
44 0 for my $name (keys %opt) {
45 0 ▶ my $value = $options{$name} or next;
Branch: 1 branch
BranchTrueFalseTotal
my $value = $options{$name} or next 0 0 0
46 0 ▶ if ($opt{$name} ne "1") {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{$name} ne 1) 0 0 0
47 0 $name = $opt{$name};
48 }
49 0 $mod->setopt("--$name", $value);
50 }
51 }
52  
53 =head1 NAME
54  
55 util::argv - optex argument utility modules
56  
57 =head1 SYNOPSIS
58  
59 optex command -Mutil::argv
60  
61 =head1 DESCRIPTION
62  
63 This module is a collection of sample utility functions for command
64 B<optex>.
65  
66 Function can be called with option declaration. Parameters for the
67 function are passed by name and value list: I<name>=I<value>. Value 1
68 is assigned for the name without value.
69  
70 In this example,
71  
72 optex -Mutil::argv::function(debug,message=hello,count=3)
73  
74 option B<debug> has value 1, B<message> has string "hello", and
75 B<count> also has string "3".
76  
77 The above command can also be written as follows.
78  
79 optex -Mutil::argv::function=debug,message=hello,count=3
80  
81 The upper format is easier to read visually, but the lower format is
82 easier to type from the keyboard.
83  
84 =head1 FUNCTION
85  
86 =over 4
87  
88 =cut
89  
90 ######################################################################
91 ######################################################################
92  
93 0 ▶sub times {
Subroutine times: not called CC=4 SCAR=30.0
Pod documented
94 0 my %opt = @_;
95 0 ▶ my $count = $opt{count} // 2;
Condition0%0/2
$opt{"count"} // 2
l!l
00
MC/DC0%0/1
$opt{"count"} // 2
$opt{"count"}
Truth table0%0/2
A
A= $opt{"count"}
Aresult
11
00
96 0 ▶ my $suffix = $opt{suffix} // '';
Condition0%0/2
$opt{"suffix"} // ""
l!l
00
MC/DC0%0/1
$opt{"suffix"} // ""
$opt{"suffix"}
Truth table0%0/2
A
A= $opt{"suffix"}
Aresult
11
00
97 0 ▶ my $prefix = $opt{prefix} // '';
Condition0%0/2
$opt{"prefix"} // ""
l!l
00
MC/DC0%0/1
$opt{"prefix"} // ""
$opt{"prefix"}
Truth table0%0/2
A
A= $opt{"prefix"}
Aresult
11
00
98 0 ▶ argv {
Subroutine __ANON__: not called CC=1 SCAR=6.9
99 map {
100 0 ( $_, ($prefix.$_.$suffix) x ($count - 1) );
101 } @_;
102 }
103 0 }
104  
105 =item B<times>(B<count>=I<n>,B<suffix>=I<str>)
106  
107 Multiply each arguments. Default B<count> is 2.
108  
109 % optex echo -Mutil::argv::times(count=3) 1 2 3
110 1 1 1 2 2 2 3 3 3
111  
112 Put B<suffix> to duplicated arguments.
113  
114 % optex echo -Mutil::argv::times(suffix=.bak) a b c
115 a a.bak b b.bak c c.bak
116  
117 =cut
118  
119 ######################################################################
120  
121 0 ▶sub reverse {
Subroutine reverse: not called CC=1 SCAR=6.9
Pod documented
122 0 ▶ argv { reverse @_ };
Subroutine __ANON__: not called CC=1 SCAR=6.9
123 }
124  
125 =item B<reverse>()
126  
127 Reverse arguments.
128  
129 =cut
130  
131 ######################################################################
132  
133 2 ▶use Text::Glob qw(glob_to_regex);
Subroutine BEGIN: called CC=1 SCAR=0.0
134 2 ▶use File::Basename qw(basename);
Subroutine BEGIN: called CC=1 SCAR=0.0
135  
136 0 ▶sub collect {
Subroutine collect: not called CC=8 SCAR=42.8
Pod documented
137 0 my %opt = @_;
138 0 ▶ $opt{match} //= $opt{include};
Condition0%0/3
$opt{"match"} //= $opt{"include"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"match"} //= $opt{"include"}
$opt{"match"} $opt{"include"}
Truth table0%0/3
A //= B
A= $opt{"match"}B= $opt{"include"}
ABresult
1X1
011
000
139 0 ▶ $opt{unmatch} //= $opt{exclude};
Condition0%0/3
$opt{"unmatch"} //= $opt{"exclude"}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{"unmatch"} //= $opt{"exclude"}
$opt{"unmatch"} $opt{"exclude"}
Truth table0%0/3
A //= B
A= $opt{"unmatch"}B= $opt{"exclude"}
ABresult
1X1
011
000
140 0 ▶ if ($opt{index}) {
Branch: 3 branches
BranchTrueFalseTotal
if ($opt{'index'}) { } 0 0 0
elsif (my $glob = $opt{'glob'} || $opt{'omit'}) { } 0 0 0
elsif (my $re = $opt{'match'} || $opt{'unmatch'}) { } 0 0 0
141 0 my @index = $opt{index} =~ /\d+/g;
142 0 ▶ argv {
Subroutine __ANON__: not called CC=1 SCAR=6.9
143 0 @_[ grep { $_ <= $#_ } map { $_ - 1 } @index ];
144 }
145 0 }
146 ▶ elsif (my $glob = $opt{glob} || $opt{omit}) {
Condition0%0/3
$opt{'glob'} || $opt{'omit'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'glob'} || $opt{'omit'}
$opt{'glob'} $opt{'omit'}
Truth table0%0/3
A || B
A= $opt{'glob'}B= $opt{'omit'}
ABresult
1X1
011
000
147 0 my $re = glob_to_regex($glob);
148 0 ▶ argv {
Subroutine __ANON__: not called CC=2 SCAR=17.9
149 0 ▶ if ($opt{glob}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'glob'}) { } 0 0 0
150 0 grep { basename($_) =~ /$re/ } @_;
151 } else {
152 0 grep { basename($_) !~ /$re/ } @_;
153 }
154 }
155 0 }
156 ▶ elsif (my $re = $opt{match} || $opt{unmatch}) {
Condition0%0/3
$opt{'match'} || $opt{'unmatch'}
l!l&&r!l&&!r
000
MC/DC0%0/2
$opt{'match'} || $opt{'unmatch'}
$opt{'match'} $opt{'unmatch'}
Truth table0%0/3
A || B
A= $opt{'match'}B= $opt{'unmatch'}
ABresult
1X1
011
000
157 0 ▶ argv {
Subroutine __ANON__: not called CC=2 SCAR=17.9
158 0 ▶ if ($opt{match}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{'match'}) { } 0 0 0
159 0 grep { /$re/ } @_;
160 } else {
161 0 grep { !/$re/ } @_;
162 }
163 }
164 0 }
165 else {
166 0 die "collect: no valid parameter.\n";
167 }
168 }
169  
170 =item B<collect>(index=2:4:6)
171  
172 =item B<collect>(glob=*.c)
173  
174 =item B<collect>(omit=*.c)
175  
176 =item B<collect>(match=.c$)
177  
178 =item B<collect>(unmatch=.c$)
179  
180 Collect arguments based on the given parameter.
181  
182 =over 4
183  
184 =item B<index>=I<index>
185  
186 % optex echo -Mutil::argv::collect=index=2:4:6 one two three four five six
187  
188 will print:
189  
190 two four six
191  
192 =item B<glob>=I<wildcard>
193  
194 =item B<omit>=I<wildcard>
195  
196 % optex ls -Mutil::argv::collect=glob=*.c foo.h foo.c foo.1
197  
198 will print:
199  
200 foo.c
201  
202 B<omit> is reverse of B<glob>.
203  
204 =item B<match>=I<regex>
205  
206 % optex ls -Mutil::argv::collect=match=.c$ foo.h foo.c foo.1
207  
208 will print:
209  
210 foo.c
211  
212 =item B<unmatch>=I<regex>
213  
214 % optex ls -Mutil::argv::collect=unmatch=.c$ foo.h foo.c foo.1
215  
216 will print:
217  
218 foo.h foo.1
219  
220 =back
221  
222 =cut
223  
224 ######################################################################
225  
226 2 ▶use Command::Run::Tmpfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
227  
228 0 ▶sub proc {
Subroutine proc: not called CC=1 SCAR=6.9
Pod documented
229 0 state @persist;
230 0 ▶ argv {
Subroutine __ANON__: not called CC=3 SCAR=24.8
231 0 for (@_) {
232 0 ▶ my($command) = /^ \<\( (.*) \) $/x or next;
Branch: 1 branch
BranchTrueFalseTotal
my($command) = /^ \<\( (.*) \) $/x or next 0 0 0
233 0 my $tmp = Command::Run::Tmpfile->new(raw => 1);
234 0 $tmp->write(`$command`)->rewind;
235 0 push @persist, $tmp;
236 0 $_ = $tmp->path;
237 }
238 0 @_;
239 }
240 0 }
241  
242 =item B<proc>()
243  
244 Process substitution.
245  
246 % optex diff -Mutil::argv::proc= '<(date)' '<(date -u)'
247  
248 =cut
249  
250 ######################################################################
251  
252 2 ▶use Command::Run::Tmpfile;
Subroutine BEGIN: called CC=1 SCAR=0.0
253  
254 0 ▶sub filter {
Subroutine filter: not called CC=2 SCAR=17.9
Pod documented
255 0 state @persist;
256 0 my %arg = @_;
257 0 ▶ my $command = $arg{command} or die "command parameter is required.\n";
Branch: 1 branch
BranchTrueFalseTotal
my $command = $arg{"command"} or die "command parameter is required. " 0 0 0
258 0 ▶ argv {
Subroutine __ANON__: not called CC=3 SCAR=24.8
259 0 for (@_) {
260 0 ▶ -e $_ or next;
Branch: 1 branch
BranchTrueFalseTotal
-e $_ or next 0 0 0
261 0 my $tmp = Command::Run::Tmpfile->new(raw => 1);
262 0 $tmp->write(`$command < $_`)->rewind;
263 0 push @persist, $tmp;
264 0 $_ = $tmp->path;
265 }
266 0 @_;
267 }
268 0 }
269  
270 =item B<filter>(B<command>=I<command>)
271  
272 Execute filter command for each file. Specify any command which
273 should be invoked for each argument.
274  
275 % optex diff -Mutil::argv::filter=command='cat -n' foo bar
276  
277 In this example, C<foo> and C<bar> are replaced by the result output
278 of C<< cat -n < foo >> and C<< cat -n < bar >>. The replacement only
279 occurs when the file corresponding to the argument exists.
280  
281 =cut
282  
283 ######################################################################
284  
285 =back
286  
287 =head1 OPTIONS
288  
289 Several options are prepared and enabled by request. To enable
290 specific option, use B<enable> function like this to enable B<--move>
291 and B<--copy> options.
292  
293 -Mutil::argv::enable=move,copy
294  
295 Parameter B<:all> can be used to enable everything.
296  
297 -Mutil::argv::enable=:all
298  
299 You can use alternative names:
300  
301 -Mutil::argv::enable(move=mymove,copy=mycopy)
302  
303 =over 4
304  
305 =item B<--move> I<param>
306  
307 =item B<--remove> I<param>
308  
309 =item B<--copy> I<param>
310  
311 These options are converted C<< $<command(param)> >> notation, where
312 I<param> is B<offset> or B<offset>,B<length>.
313  
314 B<--move 0> moves all following arguments there, B<--remove 0> just
315 removes them, and B<--copy 0> copies them.
316  
317 B<--move 0,1> moves following argument (which does not change
318 anything), and B<--move 1,1> moves second argument (exchange following
319 two).
320  
321 B<--move -1> moves the last argument.
322  
323 B<--copy 0,1> duplicates the next.
324  
325 =item B<--exch>
326  
327 Exchanges following two arguments. This is same as B<--move 1,1>.
328  
329 optex -Mutil::argv::enable=exch echo --exch foo bar
330  
331 will print:
332  
333 bar foo
334  
335 =item B<--filter> I<command>
336  
337 Set input filter for each file argument.
338  
339 Next command passes the result of the C<expand> filter to the C<cat
340 -n> for each file in the command line.
341  
342 optex -Mutil::argv::enable=filter cat -n --filter=expand a b c
343  
344 =back
345  
346 Following options are interface for builtin functions.
347  
348 =over 4
349  
350 =item B<--times> I<count>
351  
352 =item B<--reverse>
353  
354 =item B<--index> I<index>
355  
356 =item B<--glob> I<wildcard>
357  
358 =item B<--omit> I<wildcard>
359  
360 =item B<--match> I<regex>
361  
362 =item B<--unmatch> I<regex>
363  
364 =back
365  
366 =cut
367  
368 2 1;
369  
370 __DATA__
« script/optex ↑ Summary blib/lib/App/optex/help.pm »