Ordo/Command/Base.pm untested

filter: Statementstmt 0.0% 0 / 52 Branchbran 0.0% 0 / 6 Conditioncond 0.0% 0 / 11 MC/DCmcdc 0.0% 0 / 7 Subroutinesub 0.0% 0 / 13 Podpod 0.0% 0 / 7 totaltotal 0.0% 0 / 96 CC 4 SCAR 14.8
CC
4
Cov
0%
run
12.0
show_help
6.0
name
2.0
CRAP 4.4
1 package App::Ordo::Command::Base;
2 use Moo;
3 use feature qw(say);
4 use utf8;
5 use open ':std', ':utf8';
6 use Getopt::Long qw(GetOptionsFromArray);
7 use Term::ANSIColor qw(colored);
8  
9 has 'api' => (is => 'ro', required => 1);
10  
11 # Override in subclasses
12 sub name { die "override name()" }
13 sub summary { "no summary" }
14 sub usage { "override usage" }
15 sub option_spec { {} } # MUST return hashref: 'opt=s' => 'description'
16  
17 sub run {
18 my ($self, @args) = @_;
19  
20 if (@args && ($args[0] eq '--help' || $args[0] eq '-h')) {
21 $self->show_help;
22 return;
23 }
24  
25 my %opt;
26 my $ok = GetOptionsFromArray(
27 \@args,
28 \%opt,
29 keys %{$self->option_spec},
30 );
31  
32 unless ($ok) {
33 say colored(["bold red"], "Invalid options — use --help");
34 return;
35 }
36  
37 $self->execute(\%opt, @args);
38 }
39  
40 sub execute {
41 my ($self, $opt, @args) = @_;
42 die "execute() not implemented in " . ref($self);
43 }
44  
45 sub show_help {
46 my ($self) = @_;
47 say colored(["bold cyan"], "ordo @{[lc $self->name]} " . $self->usage);
48 say $self->summary;
49  
50 my $spec = $self->option_spec;
51 return unless $spec && keys %$spec;
52  
53 say "\nOptions:";
54 for my $key (sort keys %$spec) {
55 my $desc = $spec->{$key} || '';
56 (my $clean = $key) =~ s/[=:!+].*//;
57 $clean =~ s/\|/ | /g;
58 printf " %-30s %s\n", $clean, $desc;
59 }
60 }
61  
62 1;