Bash.pm

filter: Statementstmt 8.3% 19 / 227 Branchbran 0.0% 0 / 52 Conditioncond 0.0% 0 / 19 MC/DCmcdc 0.0% 0 / 9 Subroutinesub 40.0% 6 / 15 Podpod 0.0% 0 / 9 totaltotal 7.5% 25 / 331 CC 41 SCAR 44.7
CC
41
Cov
6%
generate_subcommands
210.0
cmd_new
90.0
generate_options
90.0
CRAP 87.5
↑ Summary Bash/Spec.pm »
1 # ABSTRACT: App Module and utilities for appspec-bash
2 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use 5.010;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 package App::Spec::Bash;
6  
7 1 our $VERSION = '0.002'; # VERSION
8  
9 1 ▶use base 'App::Spec::Run::Cmd';
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 1 ▶use File::Path qw/ remove_tree make_path /;
Subroutine BEGIN: called CC=1 SCAR=0.0
12 1 ▶use Data::Dumper;
Subroutine BEGIN: called CC=1 SCAR=0.0
13  
14 0 ▶sub parser {
Subroutine parser: not called CC=2 SCAR=17.9
Pod undocumented
15 0 my ($self, $run) = @_;
16 0 my $params = $run->parameters;
17 0 my $bashfile = $params->{output};
18 0 my $specfile = $params->{spec};
19 0 my $spec = App::Spec->read($specfile);
20  
21 0 my $bash = $self->generate_app($spec);
22 0 ▶ open my $fh, '>', $bashfile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $bashfile or die $! 0 0 0
23 0 print $fh $bash;
24 0 close $fh;
25 }
26  
27 0 ▶sub cmd_new {
Subroutine cmd_new: not called CC=9 SCAR=45.0
Pod undocumented
28 0 my ($self, $run) = @_;
29 0 my $params = $run->parameters;
30 0 my $options = $run->options;
31 0 my $name = $options->{name};
32 0 my $class = $options->{class};
33 0 ▶ my $dir = $params->{path} // $class;
Condition0%0/3
$params->{"path"} // $class
l!l&&r!l&&!r
000
MC/DC0%0/2
$params->{"path"} // $class
$params->{"path"} $class
Truth table0%0/3
A // B
A= $params->{"path"}B= $class
ABresult
1X1
011
000
34 0 ▶ my $overwrite = $options->{overwrite} || 0;
Condition0%0/2
$options->{'overwrite'} || 0
l!l
00
MC/DC0%0/1
$options->{'overwrite'} || 0
$options->{'overwrite'}
Truth table0%0/2
A
A= $options->{'overwrite'}
Aresult
11
00
35  
36 0 my $spec = <<"EOM";
37 name: $name
38 appspec: { version: '0.001' }
39 plugins: [-Meta] # not supported in bash
40 class: $class
41 title: 'app title'
42 description: 'app description'
43  
44 subcommands:
45  
46 cmd1:
47 op: cmd1
48 summary: Command1
49 options:
50 - flag-a|a --Some flag a
51 - flag-b|b --Some flag b
52 - opt-x|x=s --Some option x
53 - opt-y|y=s@ --Some (multi) option y
54  
55 cmd2:
56 op: cmd2
57 summary: Command2
58 options:
59 - verbose|v+ --Verbose output (can be used multiple times)
60 parameters:
61 - +name --Some required parameter
62 EOM
63  
64 0 my $script = sprintf <<'EOM', $name;
65 #!/usr/bin/env bash
66  
67 DIR="$( dirname $BASH_SOURCE )"
68  
69 APPSPECDIR="$DIR/.."
70 source "$APPSPECDIR/lib/appspec"
71 source "$APPSPECDIR/lib/%s"
72  
73 APPSPEC.run $@
74 EOM
75  
76 0 my $module = <<"EOM";
77 #!/usr/bin/env bash
78  
79 $class.cmd1() {
80 APPSPEC.say "Running command 1" BOLD
81 APPSPEC.say "flag-a: \$OPT_FLAG_A"
82 APPSPEC.say "flag-b: \$OPT_FLAG_B"
83 APPSPEC.say "opt-x: '\$OPT_OPT_X'"
84 APPSPEC.say "opt-y: (\${OPT_OPT_Y[*]})"
85 }
86  
87 $class.cmd2() {
88 APPSPEC.say "Running command 2" MAGENTA
89 APPSPEC.say "verbose: \$OPT_VERBOSE"
90 APPSPEC.say "name: \$PARAM_NAME"
91 }
92 EOM
93  
94 0 ▶ if (-e $dir) {
Branch: 1 branch
BranchTrueFalseTotal
if (-e $dir) 0 0 0
95 0 ▶ unless ($overwrite) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($overwrite) 0 0 0
96 0 say "Remove directory '$dir' first or use --overwrite...";
97 0 return;
98 }
99 0 say "Removing directory '$dir' first...";
100 0 remove_tree $dir, { verbose => 1 };
101 }
102 make_path(
103 0 "$dir/bin", "$dir/lib", "$dir/share/completion/zsh",
104 "$dir/share/completion/bash", { verbose => 1 });
105  
106 0 say "Creating $dir/share/$name.yaml ...";
107 0 ▶ open my $fh, '>', "$dir/share/$name.yaml" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", "$dir/share/$name.yaml" or die $! 0 0 0
108 0 print $fh $spec;
109 0 close $fh;
110  
111 0 say "Creating $dir/bin/$name ...";
112 0 ▶ open $fh, '>', "$dir/bin/$name" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", "$dir/bin/$name" or die $! 0 0 0
113 0 print $fh $script;
114 0 close $fh;
115 0 ▶ chmod 0755, "$dir/bin/$name" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
chmod 0755, "$dir/bin/$name" or die $! 0 0 0
116  
117 0 say "Creating $dir/lib/$name ...";
118 0 ▶ open $fh, '>', "$dir/lib/$name" or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open $fh, ">", "$dir/lib/$name" or die $! 0 0 0
119 0 print $fh $module;
120 0 close $fh;
121  
122 0 say <<"EOM";
123 Created skeleton in $dir.
124  
125 To generate the parser and help, do:
126  
127 % cd $dir
128 % appspec-bash generate parser share/$name.yaml lib/appspec
129 % appspec-bash generate help share/$name.yaml lib/help
130  
131 Try it out:
132 % bin/$name cmd1 -ab --opt-x x -yfoo --opt-y bar
133 % bin/$name cmd2 -vvv
134 % bin/$name cmd2 -vvv foo
135  
136 EOM
137 }
138  
139 0 ▶sub genhelp {
Subroutine genhelp: not called CC=3 SCAR=24.8
Pod undocumented
140 0 my ($self, $run) = @_;
141 0 my $params = $run->parameters;
142 0 my $bashfile = $params->{output};
143 0 my $specfile = $params->{spec};
144  
145 0 my $spec = App::Spec->read($specfile);
146 0 my $appspec_version = App::Spec->VERSION;
147 0 my $appspec_bash_version = App::Spec::Bash->VERSION;
148 0 my %help;
149 0 my $help = $self->generate_help(
150 help => \%help,
151 spec => $spec,
152 cmdspec => $spec,
153 commands => [],
154 );
155 0 my $functions = '';
156 0 for my $func (sort keys %help) {
157 0 my $code = <<"EOM";
158 APPSPEC.help-$func() {
159 cat <<EOHELP
160 $help{ $func }
161 EOHELP
162 }
163  
164 EOM
165 0 $functions .= $code;
166 }
167 0 my $bash = <<"EOM";
168 #!/bin/bash
169  
170 # Generated by perl modules
171 # App::Spec v$appspec_version
172 # App::Spec::Bash v$appspec_bash_version
173  
174 APPSPEC.help() {
175 cat <<EOHELP
176 Usage
177 EOHELP
178 }
179 $functions
180  
181 EOM
182 0 ▶ open my $fh, '>', $bashfile or die $!;
Branch: 1 branch
BranchTrueFalseTotal
open my $fh, ">", $bashfile or die $! 0 0 0
183 0 print $fh $bash;
184 0 close $fh;
185 }
186  
187 0 ▶sub generate_help {
Subroutine generate_help: not called CC=4 SCAR=30.0
Pod undocumented
188 0 my ($self, %args) = @_;
189 0 my $spec = $args{spec};
190 0 my $cmdspec = $args{cmdspec};
191 0 my $help = $args{help};
192 0 my $cmds = $args{commands};
193 0 ▶ my $cmdstring = @$cmds ? join '-', @$cmds : 'ROOT';
Branch: 1 branch
BranchTrueFalseTotal
@$cmds ? : 0 0 0
194 0 my $usage = $spec->usage(
195 commands => [ @$cmds ],
196 colored => 0,
197 );
198 0 $help->{ $cmdstring } = $usage;
199  
200 0 ▶ my $subcommands = $cmdspec->subcommands || {};
Condition0%0/2
$cmdspec->subcommands || {}
l!l
00
MC/DC0%0/1
$cmdspec->subcommands || {}
$cmdspec->subcommands
Truth table0%0/2
A
A= $cmdspec->subcommands
Aresult
11
00
201 0 for my $key (sort keys %$subcommands) {
202 0 my $cmd = $subcommands->{ $key };
203 0 my $cmdstring = join '-', @$cmds, $key;
204 0 my $usage = $spec->usage(
205 commands => [ @$cmds, $key ],
206 colored => 0,
207 );
208 0 $help->{ $cmdstring } = $usage;
209 0 my $subhelp = $self->generate_help(
210 help => $help,
211 spec => $spec,
212 cmdspec => $cmd,
213 commands => [@$cmds, $key],
214 );
215 }
216 }
217  
218 0 ▶sub generate_app {
Subroutine generate_app: not called CC=3 SCAR=24.8
Pod undocumented
219 0 my ($self, $spec) = @_;
220 0 my $appspec_version = App::Spec->VERSION;
221 0 my $appspec_bash_version = App::Spec::Bash->VERSION;
222 0 my $name = $spec->name;
223 0 my $title = $spec->title;
224 0 my $class = $spec->class;
225  
226 0 my @all_options;
227 0 my $options = $spec->options;
228 0 my $params = $spec->parameters;
229 0 my $local_declare = '';
230 0 my $global_opt_long = '';
231 0 my $global_opt_short = '';
232 0 ▶ if (@$options) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$options) 0 0 0
233 0 ($local_declare, $global_opt_long, $global_opt_short)
234 = $self->generate_options($options);
235 }
236 0 my $parse_params = '';
237 0 ▶ if (@$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$params) 0 0 0
238 0 my $funcname = '';
239 0 my ($params_declare, $parse_params) = $self->generate_params($funcname, $params);
240 0 $local_declare .= $params_declare;
241 }
242 0 push @all_options, @$options;
243  
244 0 my @functions;
245 my @commands;
246  
247 0 my $subcommands = $spec->subcommands;
248 0 my $subcmds = $self->generate_subcommands(
249 subcommands => $subcommands,
250 functions => \@functions,
251 commands => \@commands,
252 );
253 0 my $functions = join "\n", @functions;
254 0 my $colors = $self->ansi_colors;
255  
256 0 my $run = '';
257 0 my $bash = <<"EOM";
258 #!/bin/bash
259  
260 # Generated by perl modules
261 # App::Spec v$appspec_version
262 # App::Spec::Bash v$appspec_bash_version
263 # $name - $title
264  
265 set -e
266  
267 declare OP=
268 declare -a ERRORS=()
269 declare -a COMMANDS=()
270 declare stdout_terminal=false stderr_terminal=false
271 declare END_OF_OPTIONS=false
272 $colors
273  
274 DEBUG=false
275 [[ -n "\$APPSPEC_BASH_DEBUG" ]] && DEBUG=true
276  
277 APPSPEC.run() {
278 APPSPEC.init-terminal
279 APPSPEC.parse "\$@"
280 }
281  
282 APPSPEC.run-op() {
283 if (( \${#ERRORS[*]} > 0 )); then
284 APPSPEC.show_help
285 for i in "\${ERRORS[@]}"; do
286 APPSPEC.error "\$i"
287 done
288 exit 1
289 else
290 debug "OP: \$OP"
291 fi
292 if [[ \${#argv[@]} -gt 0 ]]; then
293 debug "leftover args in ARGV: (\${argv[*]})"
294 ARGV=("\${argv[@]}")
295 fi
296 debug "Running $class.\$OP"
297 if [[ -n "\$OP" ]]; then
298 $class.\$OP
299 else
300 echo "No operation found"
301 fi
302 }
303  
304 APPSPEC.parse() {
305 local argv=("\$@")
306 debug "ARGV: \${argv[*]}"
307 $local_declare
308 if [[ \${#argv[@]} -eq 0 ]]; then
309 APPSPEC.add-error "Missing subcommand"
310 APPSPEC.run-op
311 return
312 fi
313 while [[ \${#argv[@]} -gt 0 ]]; do
314 debug "processing \${argv[0]}..."
315 case "\${argv[0]}" in
316 $global_opt_long
317 -*)
318 $global_opt_short
319 ;;
320  
321 # SUBCOMMANDS
322 $subcmds
323 esac
324 done
325  
326 debug "ARGV: \${argv[*]}"
327 }
328  
329 $functions
330  
331  
332 EOM
333 0 $bash .= <<'EOM';
334  
335 APPSPEC.colored() {
336 local fh="$1"
337 local message="$2"
338 if [[ "$fh" == "stdout" ]] && ! $stdout_terminal; then
339 echo "$message"
340 return
341 fi
342 if [[ "$fh" == stderr ]] && ! $stderr_terminal; then
343 echo "$message"
344 return
345 fi
346 shift
347 shift
348 local colornames=($@)
349 local varname value colored=
350  
351 for i in "${colornames[@]}"; do
352 varname="APPSPEC_COLOR_$i"
353 value="${!varname}"
354 colored+="$value"
355 done
356  
357 colored+="$message"
358 colored+="$APPSPEC_NO_COLOR"
359 echo "$colored"
360  
361 }
362  
363 APPSPEC.colorize() {
364 local fh="$1"
365 local message="$2"
366 shift
367 shift
368 [[ "$fh" == stdout ]] && echo -e "$(APPSPEC.colored "$fh" "$message" $@)"
369 [[ "$fh" == stderr ]] && echo -e "$(APPSPEC.colored "$fh" "$message" $@)" >&2
370 }
371  
372 APPSPEC.say() {
373 local message="$1"
374 shift
375 if [[ $# -gt 0 ]]; then
376 echo -e "$(APPSPEC.colored stdout "$message" $@)"
377 else
378 echo "$message"
379 fi
380 }
381  
382 APPSPEC.error() {
383 local message="$1"
384 echo -e "$(APPSPEC.colored stderr "$message" BOLD RED)" >&2
385 }
386  
387 APPSPEC.add-error() {
388 debug "$1"
389 ERRORS+=("$1")
390 }
391  
392 APPSPEC.init-terminal() {
393 if [[ -t 1 ]]; then
394 stdout_terminal=true
395 fi
396 if [[ -t 2 ]]; then
397 stderr_terminal=true
398 fi
399 }
400  
401  
402 APPSPEC.cmd_help() {
403 debug "======== APPSPEC.cmd_help"
404 COMMANDS=("${COMMANDS[@]:1}")
405 APPSPEC.show_help
406 }
407  
408 APPSPEC.show_help() {
409 source "$APPSPECDIR/lib/help"
410 debug "======== APPSPEC.show_help"
411 if [[ -n "$COMMANDS" ]]; then
412 local func="${COMMANDS[*]}"
413 func="${func/ /-}"
414 debug "func $func"
415 APPSPEC.help-$func
416 else
417 APPSPEC.help-ROOT
418 fi
419 }
420  
421 shift_arg() {
422 argv=("${argv[@]:1}")
423 }
424  
425 debug() {
426 $DEBUG && APPSPEC.colorize stderr "$@" DARKGRAY || true
427 }
428 EOM
429  
430  
431 }
432  
433 0 ▶sub generate_subcommands {
Subroutine generate_subcommands: not called CC=14 SCAR=53.5
Pod undocumented
434 0 my ($self, %args) = @_;
435 0 ▶ my $subcommands = $args{subcommands} || {};
Condition0%0/2
$args{'subcommands'} || {}
l!l
00
MC/DC0%0/1
$args{'subcommands'} || {}
$args{'subcommands'}
Truth table0%0/2
A
A= $args{'subcommands'}
Aresult
11
00
436 0 my $functions = $args{functions};
437 0 my $commands = $args{commands};
438 0 my $code;
439 0 ▶ if (%$subcommands) {
Branch: 1 branch
BranchTrueFalseTotal
if (%$subcommands) { } 0 0 0
440 0 my $case = <<"EOM";
441 EOM
442 0 for my $name (sort keys %$subcommands) {
443 # no support for _meta functions
444 0 ▶ next if $name eq '_meta';
Branch: 1 branch
BranchTrueFalseTotal
if $name eq "_meta" 0 0 0
445  
446 0 my $spec = $subcommands->{ $name };
447 0 my $sub = $spec->subcommands;
448 0 ▶ my $op = $spec->op || '';
Condition0%0/2
$spec->op || ''
l!l
00
MC/DC0%0/1
$spec->op || ''
$spec->op
Truth table0%0/2
A
A= $spec->op
Aresult
11
00
449  
450 0 ▶ my $sreq = ($spec->subcommand_required // 1) && $sub && ! $op ? 1 : 0;
Branch: 1 branch
BranchTrueFalseTotal
($spec->subcommand_required // 1) && $sub && !$op ? : 0 0 0
Condition0%0/8
$spec->subcommand_required // 1 and $sub and not $op
!ll&&!rl&&r
000
$spec->subcommand_required // 1 and $sub
!ll&&!rl&&r
000
$spec->subcommand_required // 1
l!l
00
MC/DC0%0/3
$spec->subcommand_required // 1 and $sub and not $op
$spec->subcommand_required $sub not $op
Truth table0%0/4
A and B and not(C)
A= $spec->subcommand_requiredB= $subC= not $op
ABCresult
0XX0
10X0
1100
1111
451  
452 0 my $subcmds = $self->generate_subcommands(
453 subcommands => $sub,
454 functions => $functions,
455 commands => [@$commands, $name],
456 );
457 0 ▶ if ($op) {
Branch: 2 branches
BranchTrueFalseTotal
if ($op) { } 0 0 0
elsif ($sreq) { } 0 0 0
458 0 $op = <<"EOM";
459 if [[ -z "\$OP" ]]; then
460 OP=$op
461 APPSPEC.run-op
462 fi
463 EOM
464 }
465 elsif ($sreq) {
466 0 $op = <<"EOM";
467 if [[ -z "\$OP" ]]; then
468 APPSPEC.add-error "Missing subcommand"
469 APPSPEC.run-op
470 fi
471 EOM
472 }
473 0 my $options = $spec->options;
474 0 my ($local_declare, $global_opt_long, $global_opt_short)
475 = $self->generate_options($options);
476 0 my $params = $spec->parameters;
477 0 my $parse_params = '';
478 0 my $funcname = join '-', (@$commands, $name);
479 0 ▶ if (@$params) {
Branch: 1 branch
BranchTrueFalseTotal
if (@$params) 0 0 0
480 0 (my $params_declare, $parse_params) = $self->generate_params($funcname, $params);
481 0 $local_declare .= $params_declare;
482 }
483  
484 0 my $parse_params_call = '';
485 0 ▶ if ($parse_params) {
Branch: 1 branch
BranchTrueFalseTotal
if ($parse_params) 0 0 0
486 0 my $parse_params_func = <<"EOM";
487 APPSPEC.params-$funcname() {
488 debug "APPSPEC.params-$funcname()"
489 $parse_params
490 }
491 EOM
492 0 push @$functions, $parse_params_func;
493 0 $parse_params_call = " APPSPEC.params-$funcname";
494 }
495  
496  
497 0 $case .= <<"EOM";
498 $name)
499 COMMANDS+=("$name")
500 $local_declare
501 shift_arg
502 APPSPEC.parse-$funcname
503 $parse_params_call
504 $op
505 return
506 ;;
507 EOM
508  
509 0 my $function = <<"EOM";
510 APPSPEC.parse-$funcname() {
511 debug "APPSPEC.parse-$funcname()"
512  
513 while [[ \${#argv[@]} -gt 0 ]]; do
514 if APPSPEC.parse-options-$funcname; then
515 break
516 fi
517  
518 case "\${argv[0]}" in
519 # SUBCOMMANDS
520 $subcmds
521 esac
522 done
523  
524 }
525 EOM
526 0 my $function2 = <<"EOM";
527 APPSPEC.parse-options-$funcname() {
528 debug "APPSPEC.parse-options-$funcname()"
529 \$END_OF_OPTIONS && return 1
530 while [[ \${#argv[@]} -gt 0 ]]; do
531 case "\${argv[0]}" in
532 $global_opt_long
533 -*)
534 $global_opt_short
535 ;;
536  
537 *) return 1
538 ;;
539 esac
540 done
541 return 0
542 }
543 EOM
544 0 push @$functions, $function;
545 0 push @$functions, $function2;
546  
547 }
548 0 $code .= $case;
549  
550 0 $code .= <<"EOM";
551 *)
552 APPSPEC.add-error "Unknown subcommand '\${argv[0]}'"
553 shift_arg
554 APPSPEC.run-op
555 return
556 ;;
557  
558 EOM
559 }
560 else {
561 0 $code .= <<"EOM";
562 *) break
563 ;;
564 EOM
565 }
566 0 return $code;
567 }
568  
569 0 ▶sub generate_options {
Subroutine generate_options: not called CC=9 SCAR=45.0
Pod undocumented
570 0 my ($self, $options) = @_;
571  
572 0 my $local_declare = '';
573 0 my $long = <<"EOM";
574 EOM
575 0 my $short_preface = <<'EOM';
576 local i arg=${argv[0]/-/}
577 for (( i=0; i < "${#arg}"; i++ )); do
578 local char="${arg:$i:1}"
579 value="${arg:$i+1}"
580 if (( $i+1 == ${#arg} )); then
581 shift_arg
582 value="${argv[0]}"
583 fi
584 debug "processing short $char. arg=$arg value=$value"
585 case "$char" in
586 EOM
587  
588 0 my $short = '';
589 0 for my $option (@$options) {
590 0 my $name = $option->name;
591 0 my $bashname = "OPT_" . uc($name);
592 0 my $long_action = '';
593 0 my $short_action = '';
594 0 $bashname =~ tr/0-9a-zA-Z_/_/c;
595  
596 0 ▶ if ($option->type eq 'flag') {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->type eq 'flag') { } 0 0 0
597 0 ▶ if ($option->multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->multiple) { } 0 0 0
598 0 $local_declare .= sprintf " declare -i %s=0\n", $bashname;
599 0 $long_action = sprintf " %s+=1\n", $bashname;
600 0 $short_action = sprintf " %s+=1\n", $bashname;
601 }
602 else {
603 0 $local_declare .= sprintf " declare %s=false\n", $bashname;
604 0 $long_action = sprintf " %s=true\n", $bashname;
605 0 $short_action = sprintf " %s=true\n", $bashname;
606 }
607 }
608 else {
609 0 ▶ if ($option->multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($option->multiple) { } 0 0 0
610 0 $local_declare .= sprintf " declare -a %s=()\n", $bashname;
611 0 $long_action = <<"EOM";
612 shift_arg
613 $bashname+=("\${argv[0]}")
614 EOM
615 0 $short_action = <<"EOM";
616 $bashname+=("\$value")
617 EOM
618 }
619 else {
620 0 $local_declare .= sprintf " declare %s=\n", $bashname;
621 0 $long_action = <<"EOM";
622 shift_arg
623 $bashname="\${argv[0]}"
624 EOM
625 0 $short_action = <<"EOM";
626 $bashname="\$value"
627 EOM
628 }
629 0 $short_action .= <<"EOM";
630 shift_arg
631 break
632 EOM
633 }
634 0 $long_action .= " shift_arg\n";
635  
636 0 my @names = ($name, @{ $option->aliases });
637 0 my @case_long;
638 my @case_short;
639 0 for my $n (@names) {
640 0 ▶ if (length($n) > 1) {
Branch: 1 branch
BranchTrueFalseTotal
if (length $n > 1) { } 0 0 0
641 0 push @case_long, "--$n";
642 }
643 else {
644 0 push @case_short, $n;
645 }
646 }
647 0 my $case_long = join '|', @case_long;
648 0 my $case_short = join '|', @case_short;
649 0 $long .= <<"EOM";
650 $case_long)
651 debug "LONG OPTION $name"
652 $long_action
653 ;;
654 EOM
655 0 ▶ if (@case_short) {
Branch: 1 branch
BranchTrueFalseTotal
if (@case_short) 0 0 0
656 0 $short .= <<"EOM";
657 $case_short)
658 debug "SHORT OPTION $name"
659 $short_action
660 ;;
661 EOM
662 }
663 }
664  
665 0 $long .= <<"EOM";
666 --) shift_arg; END_OF_OPTIONS=true; break
667 ;;
668 --*)
669 APPSPEC.add-error "Unknown option '\${argv[0]}'"
670 shift_arg
671 break
672 ;;
673 EOM
674 0 ▶ if ($short) {
Branch: 1 branch
BranchTrueFalseTotal
if ($short) { } 0 0 0
675 0 $short .= <<"EOM";
676 *)
677 APPSPEC.add-error "Unknown option '-\$char'"
678 shift_arg
679 break
680 ;;
681 esac
682 done
683 EOM
684 0 $short = $short_preface . $short;
685 }
686 else {
687 0 $short = <<'EOM';
688 APPSPEC.add-error "Unknown option '${argv[0]}'"
689 shift_arg
690 break
691 EOM
692 }
693  
694 0 return ($local_declare, $long), $short;
695 }
696  
697 0 ▶sub generate_params {
Subroutine generate_params: not called CC=4 SCAR=30.0
Pod undocumented
698 0 my ($self, $funcname, $params) = @_;
699  
700 0 my $declare = '';
701 0 my $check = '';
702 0 my $parse = <<'EOM';
703 while [[ ${#argv[@]} -gt 0 ]]; do
704 EOM
705 0 for my $p (@$params) {
706 0 my $name = $p->name;
707 0 my $bashname = "PARAM_" . uc($name);
708 0 $bashname =~ tr/0-9a-zA-Z_/_/c;
709 0 ▶ if ($p->multiple) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->multiple) { } 0 0 0
710 0 $declare .= " declare -a $bashname=()";
711 0 $parse .= <<"EOM";
712 if APPSPEC.parse-options-$funcname; then
713 break
714 fi
715 $bashname+=(\${argv[@]})
716 argv=()
717 EOM
718 }
719 else {
720 0 $declare .= " declare $bashname";
721 0 $parse .= <<"EOM";
722 if APPSPEC.parse-options-$funcname; then
723 break
724 fi
725 $bashname="\${argv[0]}"
726 shift_arg
727 EOM
728 }
729 0 ▶ if ($p->required) {
Branch: 1 branch
BranchTrueFalseTotal
if ($p->required) 0 0 0
730 0 $declare .= " # REQUIRED";
731 0 $check .= <<"EOM";
732 if [[ -z "\$$bashname" ]]; then
733 APPSPEC.add-error "Missing required parameter '$name'"
734 fi
735 EOM
736 }
737 0 $declare .= "\n";
738  
739 }
740 0 $parse .= " break\n";
741 0 $parse .= " done\n";
742 0 $parse .= <<"EOM";
743 if APPSPEC.parse-options-$funcname; then
744 true
745 fi
746 EOM
747 0 $parse .= $check;
748 0 return ($declare, $parse);
749 }
750  
751 0 ▶sub ansi_colors {
Subroutine ansi_colors: not called CC=1 SCAR=6.9
Pod undocumented
752 0 return <<'EOM';
753 declare APPSPEC_COLOR_BOLD='\x1b[1m'
754 declare APPSPEC_COLOR_RED='\x1b[31m'
755 declare APPSPEC_COLOR_ERROR="$GIT_HUB_COLOR_RED$GIT_HUB_COLOR_BOLD"
756 declare APPSPEC_COLOR_GREEN='\x1b[32m'
757 declare APPSPEC_COLOR_CYAN='\x1b[36m'
758 declare APPSPEC_COLOR_MAGENTA='\x1b[35m'
759 declare APPSPEC_COLOR_YELLOW='\x1b[33m'
760 declare APPSPEC_COLOR_DARKGRAY='\x1b[90m'
761 declare APPSPEC_NO_COLOR='\x1b[0m'
762  
763 EOM
764 }
765  
766 1 1;
767  
768 =pod
769  
770 =encoding utf-8
771  
772 =head1 NAME
773  
774 App::Spec::Bash - Use Perl to generate advanced bash apps
775  
776 =head1 SYNOPSIS
777  
778 # Generate skeleton for new app
779 appspec-bash new --class MyTool --name mytool
780  
781 =head1 DESCRIPTION
782  
783 This app can create advanced bash scripts by generating bash code from a
784 specification written in YAML. The specification is the same that L<App::Spec>
785 uses.
786  
787 This is the first release. Be prepared for API changes. Sorry!
788  
789 See the documentation for the L<appspec-bash> commandline tool.
790  
791 =head1 SEE ALSO
792  
793 =over
794  
795 =item L<App::Spec>
796  
797 =back
798  
799 =head1 COPYRIGHT AND LICENSE
800  
801 Copyright 2019 by Tina Müller
802  
803 This library is free software and may be distributed under the same terms
804 as perl itself.
805  
806 =cut
↑ Summary Bash/Spec.pm »