| 1 | package Catmandu::Cmd; | ||||||||||||||
| 2 | |||||||||||||||
| 3 | 13 | ▶ | use Catmandu::Sane; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 4 | |||||||||||||||
| 5 | 13 | our $VERSION = '1.0201_02'; | |||||||||||||
| 6 | |||||||||||||||
| 7 | 13 | ▶ | use parent qw(App::Cmd::Command); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 8 | 13 | ▶ | use Catmandu::Util qw(pod_section); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 9 | 13 | ▶ | use Catmandu::Fix; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 13 | ▶ | use namespace::clean; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | |||||||||||||||
| 12 | # Internal required by App::Cmd | ||||||||||||||
| 13 | sub opt_spec { | ||||||||||||||
| 14 | 37 | ▶ | my ($class, $cli) = @_; | ||||||||||||
|
Subroutine
opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 15 | ( | ||||||||||||||
| 16 | 37 | ['help|h|?', "print this usage screen"], | |||||||||||||
| 17 | $cli->global_opt_spec, $class->command_opt_spec($cli), | ||||||||||||||
| 18 | ); | ||||||||||||||
| 19 | } | ||||||||||||||
| 20 | |||||||||||||||
| 21 | # Internal required by App::Cmd | ||||||||||||||
| 22 | sub execute { | ||||||||||||||
| 23 | 22 | ▶ | my ($self, $opts, $args) = @_; | ||||||||||||
|
Subroutine
execute: called CC=3 SCAR=14.2
Pod
documented
| |||||||||||||||
| 24 | |||||||||||||||
| 25 | 22 | ▶ | if ($opts->{version}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 26 | 0 | print $Catmandu::VERSION; | |||||||||||||
| 27 | 0 | exit; | |||||||||||||
| 28 | } | ||||||||||||||
| 29 | 22 | ▶ | if ($opts->{help}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 30 | 0 | print $self->usage->text; | |||||||||||||
| 31 | 0 | exit; | |||||||||||||
| 32 | } | ||||||||||||||
| 33 | |||||||||||||||
| 34 | 22 | $self->command($opts, $args); | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | # show examples, if available in POD | ||||||||||||||
| 38 | sub description { | ||||||||||||||
| 39 | 0 | ▶ | my $class = ref shift; | ||||||||||||
|
Subroutine
description: not called CC=2 SCAR=17.9
Pod
documented
| |||||||||||||||
| 40 | |||||||||||||||
| 41 | 0 | my $s = pod_section($class, "name"); | |||||||||||||
| 42 | 0 | $s =~ s/.*\s+-\s+//; | |||||||||||||
| 43 | 0 | $s = ucfirst($s); | |||||||||||||
| 44 | 0 | $s .= "\n"; | |||||||||||||
| 45 | |||||||||||||||
| 46 | 0 | for (pod_section($class, "examples")) { | |||||||||||||
| 47 | 0 | $s .= "Examples:\n\n$_"; | |||||||||||||
| 48 | } | ||||||||||||||
| 49 | |||||||||||||||
| 50 | 0 | "$s\nOptions:"; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | # These should be implemented by the Catmandu::Cmd's | ||||||||||||||
| 54 | ▶ | sub command_opt_spec { } | |||||||||||||
|
Subroutine
command_opt_spec: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 55 | ▶ | sub command { } | |||||||||||||
|
Subroutine
command: not called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||||
| 56 | |||||||||||||||
| 57 | # helpers | ||||||||||||||
| 58 | sub _parse_options { | ||||||||||||||
| 59 | 12 | ▶ | my ($self, $args) = @_; | ||||||||||||
|
Subroutine
_parse_options: called CC=6 SCAR=21.5
| |||||||||||||||
| 60 | |||||||||||||||
| 61 | 12 | my $a = my $from_args = []; | |||||||||||||
| 62 | 12 | my $o = my $from_opts = {}; | |||||||||||||
| 63 | 12 | my $into_args = []; | |||||||||||||
| 64 | 12 | my $into_opts = {}; | |||||||||||||
| 65 | |||||||||||||||
| 66 | 12 | ▶ | for (my $i = 0; $i < @$args; $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 67 | 30 | my $arg = $args->[$i]; | |||||||||||||
| 68 | 30 | ▶ | if ($arg eq 'to') { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 69 | 9 | $a = $into_args; | |||||||||||||
| 70 | 9 | $o = $into_opts; | |||||||||||||
| 71 | } | ||||||||||||||
| 72 | elsif ($arg =~ s/^-+//) { | ||||||||||||||
| 73 | 0 | $arg =~ s/-/_/g; | |||||||||||||
| 74 | 0 | ▶ | if (exists $o->{$arg}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 75 | 0 | ▶ | if (is_array_ref($o->{$arg})) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 76 | 0 | push @{$o->{$arg}}, $args->[++$i]; | |||||||||||||
| 77 | } | ||||||||||||||
| 78 | else { | ||||||||||||||
| 79 | 0 | $o->{$arg} = [$o->{$arg}, $args->[++$i]]; | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | } | ||||||||||||||
| 82 | else { | ||||||||||||||
| 83 | 0 | $o->{$arg} = $args->[++$i]; | |||||||||||||
| 84 | } | ||||||||||||||
| 85 | } | ||||||||||||||
| 86 | else { | ||||||||||||||
| 87 | 21 | push @$a, $arg; | |||||||||||||
| 88 | } | ||||||||||||||
| 89 | } | ||||||||||||||
| 90 | |||||||||||||||
| 91 | 12 | return $from_args, $from_opts, $into_args, $into_opts; | |||||||||||||
| 92 | } | ||||||||||||||
| 93 | |||||||||||||||
| 94 | sub _build_fixer { | ||||||||||||||
| 95 | 5 | ▶ | my ($self, $opts) = @_; | ||||||||||||
|
Subroutine
_build_fixer: called CC=3 SCAR=12.5
| |||||||||||||||
| 96 | 5 | ▶ | if ($opts->var) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 97 | 0 | return Catmandu::Fix->new( | |||||||||||||
| 98 | preprocess => 1, | ||||||||||||||
| 99 | fixes => $opts->fix, | ||||||||||||||
| 100 | variables => $opts->var, | ||||||||||||||
| 101 | ); | ||||||||||||||
| 102 | } | ||||||||||||||
| 103 | Catmandu::Fix->new( | ||||||||||||||
| 104 | 5 | ▶ | preprocess => $opts->preprocess ? 1 : 0, | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 105 | fixes => $opts->fix, | ||||||||||||||
| 106 | ); | ||||||||||||||
| 107 | } | ||||||||||||||
| 108 | |||||||||||||||
| 109 | 13 | 1; | |||||||||||||
| 110 | |||||||||||||||
| 111 | __END__ | ||||||||||||||
| 112 | |||||||||||||||
| 113 | =pod | ||||||||||||||
| 114 | |||||||||||||||
| 115 | =head1 NAME | ||||||||||||||
| 116 | |||||||||||||||
| 117 | Catmandu::Cmd - A base class for extending the Catmandu command line | ||||||||||||||
| 118 | |||||||||||||||
| 119 | =head1 SYNOPSIS | ||||||||||||||
| 120 | |||||||||||||||
| 121 | # to create a command: | ||||||||||||||
| 122 | $ catmandu hello_world | ||||||||||||||
| 123 | |||||||||||||||
| 124 | # you need a package: | ||||||||||||||
| 125 | package Catmandu::Cmd::hello_world; | ||||||||||||||
| 126 | use parent 'Catmandu::Cmd'; | ||||||||||||||
| 127 | |||||||||||||||
| 128 | sub command_opt_spec { | ||||||||||||||
| 129 | ( | ||||||||||||||
| 130 | [ "greeting|g=s", "provide a greeting text" ], | ||||||||||||||
| 131 | ); | ||||||||||||||
| 132 | } | ||||||||||||||
| 133 | |||||||||||||||
| 134 | sub description { | ||||||||||||||
| 135 | <<EOS; | ||||||||||||||
| 136 | examples: | ||||||||||||||
| 137 | |||||||||||||||
| 138 | catmandu hello_world --greeting "Hoi" | ||||||||||||||
| 139 | |||||||||||||||
| 140 | options: | ||||||||||||||
| 141 | EOS | ||||||||||||||
| 142 | } | ||||||||||||||
| 143 | |||||||||||||||
| 144 | sub command { | ||||||||||||||
| 145 | my ($self, $opts, $args) = @_; | ||||||||||||||
| 146 | my $greeting = $opts->greeting // 'Hello'; | ||||||||||||||
| 147 | print "$greeting, World!\n" | ||||||||||||||
| 148 | } | ||||||||||||||
| 149 | |||||||||||||||
| 150 | =head1 NAME | ||||||||||||||
| 151 | |||||||||||||||
| 152 | Catmandu::Cmd::hello_world - prints a funny line | ||||||||||||||
| 153 | |||||||||||||||
| 154 | =cut | ||||||||||||||
| 155 | |||||||||||||||
| 156 | |||||||||||||||
| 157 | |||||||||||||||
| 158 | =head1 DESCRIPTION | ||||||||||||||
| 159 | |||||||||||||||
| 160 | Catmandu:Cmd is a base class to extend the commands that can be provided for | ||||||||||||||
| 161 | the 'catmandu' command line tools. New catmandu commands should be defined in | ||||||||||||||
| 162 | the Catmandu::Cmd namespace and extend Catmandu::Cmd. | ||||||||||||||
| 163 | |||||||||||||||
| 164 | Every command needs to implement 4 things: | ||||||||||||||
| 165 | |||||||||||||||
| 166 | * command_opt_spec - which should return an array of command options with documentation | ||||||||||||||
| 167 | * description - a long description of the command | ||||||||||||||
| 168 | * command - the body which is executed | ||||||||||||||
| 169 | * head1 NAME - a short description of the command | ||||||||||||||
| 170 | |||||||||||||||
| 171 | =head1 METHODS | ||||||||||||||
| 172 | |||||||||||||||
| 173 | =head2 command_opt_spec() | ||||||||||||||
| 174 | |||||||||||||||
| 175 | This method should be overridden to provide option specifications. (This is list of arguments passed to describe_options from Getopt::Long::Descriptive, after the first.) | ||||||||||||||
| 176 | |||||||||||||||
| 177 | If not overridden, it returns an empty list. | ||||||||||||||
| 178 | |||||||||||||||
| 179 | =head2 description() | ||||||||||||||
| 180 | |||||||||||||||
| 181 | This method should return a string containing the long documentation of the command | ||||||||||||||
| 182 | |||||||||||||||
| 183 | =head2 command() | ||||||||||||||
| 184 | |||||||||||||||
| 185 | This method does whatever it is the command should do! It is passed a hash reference of the parsed command-line options and an array reference of left over arguments. | ||||||||||||||
| 186 | |||||||||||||||
| 187 | =head1 DOCUMENTATION | ||||||||||||||
| 188 | |||||||||||||||
| 189 | At least provide for every command a NAME documentation | ||||||||||||||
| 190 | |||||||||||||||
| 191 | =head1 SEE ALSO | ||||||||||||||
| 192 | |||||||||||||||
| 193 | L<Catmandu::Cmd::config> , L<Catmandu::Cmd::convert> , L<Catmandu::Cmd::count> , | ||||||||||||||
| 194 | L<Catmandu::Cmd::data> , L<Catmandu::Cmd::delete> , L<Catmandu::Cmd::export>, | ||||||||||||||
| 195 | L<Catmandu::Cmd::import> , L<Catmandu::Cmd::move> , L<Catmandu::Cmd::run> | ||||||||||||||
| 196 | |||||||||||||||
| 197 | =cut | ||||||||||||||