blib/lib/App/SeqPericmd.pm

filter: Statementstmt 34.7% 16 / 46 Branchbran 0.0% 0 / 34 Conditioncond 0.0% 0 / 24 MC/DCmcdc 0.0% 0 / 12 Subroutinesub 66.6% 4 / 6 Podpod 100% 1 / 1 totaltotal 17.0% 21 / 123 CC 26 SCAR 59.0
CC
26
Cov
15%
seq
506.0
__ANON__
30.0
BEGIN
1.0
CRAP 364.1
↑ Summary
1 package App::SeqPericmd;
2  
3 1 our $DATE = '2021-07-17'; # DATE
4 1 our $VERSION = '0.050'; # VERSION
5  
6 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 ▶use List::Util qw(max);
Subroutine BEGIN: called CC=1 SCAR=0.0
11  
12 1 our %SPEC;
13  
14 $SPEC{seq} = {
15 1 v => 1.1,
16 summary => 'Rinci-/Perinci::CmdLine-based "seq"-like CLI utility',
17 description => <<'_',
18  
19 This utility is similar to Unix `seq` command, with a few differences: some
20 differences in option names, JSON output, allow infinite stream (when `to` is
21 not specified).
22  
23 _
24 args_rels => {
25 dep_any => ['equal_width', ['to']], # can't specify equal_width without to
26 },
27 args => {
28 from => {
29 schema => 'float*',
30 req => 1,
31 pos => 0,
32 },
33 to => {
34 schema => 'float*',
35 pos => 1,
36 },
37 increment => {
38 schema => 'float*',
39 default => 1,
40 cmdline_aliases => {i=>{}},
41 pos => 2,
42 },
43 header => {
44 summary => 'Add a header row',
45 schema => 'str*',
46 },
47 equal_width => {
48 summary => 'Equalize width by padding with leading zeros',
49 schema => ['bool*', is=>1],
50 cmdline_aliases => {w=>{}},
51 },
52 limit => {
53 summary => 'Only generate a certain amount of numbers',
54 schema => ['int*', min=>1],
55 cmdline_aliases => {n=>{}},
56 },
57 number_format => {
58 summary => 'sprintf() format for each number',
59 schema => ['str*'],
60 cmdline_aliases => {f=>{}},
61 },
62 },
63 result => {
64 schema => ['array*', of=>'float*'],
65 },
66 examples => [
67 {
68 summary => 'Generate whole numbers from 1 to 10 (1, 2, ..., 10)',
69 src => '[[prog]] 1 10',
70 src_plang => 'bash',
71 'x.doc.show_result' => 0,
72 },
73 {
74 summary => 'Generate odd numbers from 1 to 10 (1, 3, 5, 7, 9)',
75 src => '[[prog]] 1 10 2',
76 src_plang => 'bash',
77 'x.doc.show_result' => 0,
78 },
79 {
80 summary => 'Generate 1, 1.5, 2, 2.5, ..., 10',
81 src => '[[prog]] 1 10 -i 0.5',
82 src_plang => 'bash',
83 'x.doc.show_result' => 0,
84 },
85 {
86 summary => 'Generate stream 1, 1.5, 2, 2.5, ...',
87 src => '[[prog]] 1 -i 0.5',
88 src_plang => 'bash',
89 'x.doc.show_result' => 0,
90 },
91 {
92 summary => 'Generate 01, 02, ..., 10',
93 src => '[[prog]] 1 10 -w',
94 src_plang => 'bash',
95 'x.doc.show_result' => 0,
96 },
97 {
98 summary => 'Generate 0001, 0002, ..., 0010',
99 src => '[[prog]] 1 10 -f "%04s"',
100 src_plang => 'bash',
101 'x.doc.show_result' => 0,
102 },
103 {
104 summary => 'Generate -10, -9, -8, -7, -6 (limit 5 numbers)',
105 src => '[[prog]] --from -10 --to 0 -n 5',
106 src_plang => 'bash',
107 'x.doc.show_result' => 0,
108 },
109 {
110 summary => 'Use with fsql',
111 src => q{[[prog]] 1 100 --header num | fsql --add-tsv - --add-csv data.csv 'SELECT num, data1 FROM stdin LEFT JOIN data ON stdin.num=data.num'},
112 src_plang => 'bash',
113 'x.doc.show_result' => 0,
114 },
115 ],
116 links => [
117 {url=>'prog:seq'},
118 {url=>'prog:seq-pl'},
119 {url=>'prog:seq-intrange'},
120 {url=>'prog:seq-numseq'},
121 {url=>'prog:numseq'},
122 {url=>'prog:primes'},
123 {url=>'prog:primes.pl'},
124 {url=>'prog:primes-pericmd'},
125 ],
126 };
127 0 ▶sub seq {
Subroutine seq: not called CC=22 SCAR=62.3
Pod documented
128 0 my %args = @_;
129  
130 0 my $fmt = $args{number_format};
131 0 ▶ if (!defined($fmt)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $fmt) 0 0 0
132 0 ▶ if ($args{equal_width}) {
Branch: 2 branches
BranchTrueFalseTotal
if ($args{'equal_width'}) { } 0 0 0
elsif ($args{'from'} != int $args{'from'} or defined $args{'to'} and $args{'to'} != int $args{'to'} or $args{'increment'} or int $args{'increment'}) { } 0 0 0
133 0 ▶ my $neg = $args{from}<0 || $args{to}<0 || $args{increment}<0 ? 1:0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'from'} < 0 || $args{'to'} < 0 || $args{'increment'} < 0 ? : 0 0 0
Condition0%0/6
$args{"from"} < 0 or $args{"to"} < 0 or $args{"increment"} < 0
l!l&&r!l&&!r
000
$args{"from"} < 0 or $args{"to"} < 0
l!l&&r!l&&!r
000
MC/DC0%0/3
$args{"from"} < 0 or $args{"to"} < 0 or $args{"increment"} < 0
$args{"from"} < 0 $args{"to"} < 0 $args{"increment"} < 0
Truth table0%0/4
A or B or C
A= $args{"from"} < 0B= $args{"to"} < 0C= $args{"increment"} < 0
ABCresult
1XX1
01X1
0011
0000
134 my $width_whole = max(
135 length(int($args{from} )),
136 length(int($args{to} )),
137 0 length(int($args{increment})),
138 );
139 my $width_frac = max(
140 length($args{from} - int($args{from} )),
141 length($args{to} - int($args{to} )),
142 0 length($args{increment} - int($args{increment})),
143 ) - 2;
144 0 ▶ $width_frac = 0 if $width_frac < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $width_frac < 0 0 0 0
145 0 ▶ $fmt = sprintf("%%0%d.%df",
Branch: 1 branch
BranchTrueFalseTotal
$width_frac ? : 0 0 0
146 $width_whole+$width_frac+($width_frac ? 1:0) + $neg,
147 $width_frac,
148 );
149 #say "D:fmt=$fmt";
150 ▶ } elsif ($args{from} != int($args{from}) ||
Condition0%0/12
$args{"from"} != int $args{"from"} or defined $args{"to"} and $args{"to"} != int $args{"to"} or $args{"increment"} or int $args{"increment"}
l!l&&r!l&&!r
000
$args{"from"} != int $args{"from"} or defined $args{"to"} and $args{"to"} != int $args{"to"} or $args{"increment"}
l!l&&r!l&&!r
000
$args{"from"} != int $args{"from"} or defined $args{"to"} and $args{"to"} != int $args{"to"}
l!l&&r!l&&!r
000
defined $args{"to"} and $args{"to"} != int $args{"to"}
!ll&&!rl&&r
000
MC/DC0%0/5
$args{"from"} != int $args{"from"} or defined $args{"to"} and $args{"to"} != int $args{"to"} or $args{"increment"} or int $args{"increment"}
$args{"from"} != int $args{"from"} defined $args{"to"} $args{"to"} != int $args{"to"} $args{"increment"} int $args{"increment"}
Truth table0%0/8
A or B and C or D or E
A= $args{"from"} != int $args{"from"}B= defined $args{"to"}C= $args{"to"} != int $args{"to"}D= $args{"increment"}E= int $args{"increment"}
ABCDEresult
1XXXX1
011XX1
00X1X1
0101X1
00X011
010011
00X000
010000
151 defined($args{to}) && $args{to} != int($args{to}) ||
152 $args{increment} || int($args{increment})) {
153 # use fixed floating point to avoid showing round-off errors
154 my $width_frac = max(
155 length($args{from} - int($args{from} )),
156 length($args{increment} - int($args{increment})),
157 (defined($args{to}) ?
158 0 ▶ (length($args{to}-int($args{to}))) : ()),
Branch: 1 branch
BranchTrueFalseTotal
defined $args{'to'} ? : 0 0 0
159 ) - 2;
160 0 ▶ $width_frac = 0 if $width_frac < 0;
Branch: 1 branch
BranchTrueFalseTotal
if $width_frac < 0 0 0 0
161 0 $fmt = sprintf("%%.%df", $width_frac);
162 }
163 }
164  
165 0 ▶ if (defined $args{to}) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $args{'to'}) { } 0 0 0
166 0 my @res;
167 0 ▶ push @res, $args{header} if $args{header};
Branch: 1 branch
BranchTrueFalseTotal
if $args{"header"} 0 0 0
168 0 my $i = $args{from}+0;
169 0 ▶ while ($i <= $args{to}) {
Branch: 1 branch
BranchTrueFalseTotal
if ($i <= $args{"to"}) 0 0 0
170 0 ▶ push @res, defined($fmt) ? sprintf($fmt, $i) : $i;
Branch: 1 branch
BranchTrueFalseTotal
defined $fmt ? : 0 0 0
171 0 ▶ last if defined($args{limit}) && @res >= $args{limit};
Branch: 1 branch
BranchTrueFalseTotal
if defined $args{"limit"} and @res >= $args{"limit"} 0 0 0
Condition0%0/3
defined $args{"limit"} and @res >= $args{"limit"}
!ll&&!rl&&r
000
MC/DC0%0/2
defined $args{"limit"} and @res >= $args{"limit"}
defined $args{"limit"} @res >= $args{"limit"}
Truth table0%0/3
A and B
A= defined $args{"limit"}B= @res >= $args{"limit"}
ABresult
0X0
100
111
172 0 $i += $args{increment};
173 }
174 0 return [200, "OK", \@res];
175 } else {
176 # stream
177 0 my $i = $args{from}+0;
178 0 ▶ my $j = $args{header} ? -1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
$args{'header'} ? : 0 0 0
179 0 my $next_i;
180 #my $finish;
181 0 ▶ my $func = sub {
Subroutine __ANON__: not called CC=5 SCAR=34.0
182 #return undef if $finish;
183 0 ▶ $i = $next_i if $j++ > 0;
Branch: 1 branch
BranchTrueFalseTotal
if $j++ > 0 0 0 0
184 0 ▶ return $args{header} if $j == 0 && $args{header};
Branch: 1 branch
BranchTrueFalseTotal
if $j == 0 and $args{"header"} 0 0 0
Condition0%0/3
$j == 0 and $args{"header"}
!ll&&!rl&&r
000
MC/DC0%0/2
$j == 0 and $args{"header"}
$j == 0 $args{"header"}
Truth table0%0/3
A and B
A= $j == 0B= $args{"header"}
ABresult
0X0
100
111
185 0 $next_i = $i + $args{increment};
186 #$finish = 1 if ...
187 0 ▶ return defined($fmt) ? sprintf($fmt, $i) : $i;
Branch: 1 branch
BranchTrueFalseTotal
defined $fmt ? : 0 0 0
188 0 };
189 0 return [200, "OK", $func, {stream=>1}];
190 }
191 }
192  
193 1 1;
194 # ABSTRACT: Rinci-/Perinci::CmdLine-based "seq"-like CLI utility
195  
196 __END__
↑ Summary