| 1 | package Catmandu::Cmd::help; | ||||||||||
| 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 'Catmandu::Cmd'; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 13 | ▶ | use App::Cmd::Command::help; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | 13 | ▶ | use Catmandu::Util qw(require_package pod_section); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 10 | 13 | ▶ | use namespace::clean; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | sub usage_desc { | ||||||||||
| 13 | 8 | ▶ | '%c help [ <command> | ( export | import | store | fix ) <name> ]' | ||||||||
|
Subroutine
usage_desc: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 14 | } | ||||||||||
| 15 | |||||||||||
| 16 | 40 | ▶ | sub command_names {qw/help --help -h -?/} | ||||||||
|
Subroutine
command_names: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 17 | |||||||||||
| 18 | 13 | my %MODULES = ( | |||||||||
| 19 | Exporter => { | ||||||||||
| 20 | re => qr/^export(er)?$/i, | ||||||||||
| 21 | usage => [ | ||||||||||
| 22 | "catmandu convert ... to %n [options]", | ||||||||||
| 23 | "catmandu export ... to %n [options]", | ||||||||||
| 24 | ], | ||||||||||
| 25 | }, | ||||||||||
| 26 | Importer => { | ||||||||||
| 27 | re => qr/^import(er)?$/i, | ||||||||||
| 28 | usage => [ | ||||||||||
| 29 | "catmandu convert %n [options] to ...", | ||||||||||
| 30 | "catmandu import %n [options] to ...", | ||||||||||
| 31 | ], | ||||||||||
| 32 | }, | ||||||||||
| 33 | Store => { | ||||||||||
| 34 | re => qr/^(store|copy)$/i, | ||||||||||
| 35 | usage => [ | ||||||||||
| 36 | "catmandu import ... to %n [options]", | ||||||||||
| 37 | "catmandu copy ... to %n [options]", | ||||||||||
| 38 | "catmandu export %n [options] ...", | ||||||||||
| 39 | "catmandu copy %n [options] ...", | ||||||||||
| 40 | ] | ||||||||||
| 41 | }, | ||||||||||
| 42 | Fix => {re => qr/^fix$/i, usage => ["%n( [options] )"]}, | ||||||||||
| 43 | ); | ||||||||||
| 44 | |||||||||||
| 45 | sub execute { | ||||||||||
| 46 | 8 | ▶ | my ($self, $opts, $args) = @_; | ||||||||
|
Subroutine
execute: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 47 | |||||||||||
| 48 | # TODO: list available Importer/Exporters/Stores/Fixes... | ||||||||||
| 49 | |||||||||||
| 50 | 8 | ▶ | if (@$args == 2) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 51 | |||||||||||
| 52 | # detect many forms such as: | ||||||||||
| 53 | # export JSON, exporter JSON, JSON export, JSON exporter | ||||||||||
| 54 | 3 | foreach (0, 1) { | |||||||||
| 55 | 3 | foreach my $type (keys %MODULES) { | |||||||||
| 56 | 6 | ▶ | if ($args->[$_] =~ $MODULES{$type}->{re}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 57 | 3 | $self->help_about($type, $args->[($_ + 1) % 2]); | |||||||||
| 58 | 3 | return; | |||||||||
| 59 | } | ||||||||||
| 60 | } | ||||||||||
| 61 | } | ||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 5 | App::Cmd::Command::help::execute(@_); | |||||||||
| 65 | } | ||||||||||
| 66 | |||||||||||
| 67 | sub help_about { | ||||||||||
| 68 | 3 | ▶ | my ($self, $type, $name) = @_; | ||||||||
|
Subroutine
help_about: called CC=3 SCAR=11.0
Pod
undocumented
| |||||||||||
| 69 | |||||||||||
| 70 | 3 | my $class = "Catmandu::${type}::$name"; | |||||||||
| 71 | 3 | require_package($class); | |||||||||
| 72 | |||||||||||
| 73 | 3 | my $about = pod_section($class, "name"); | |||||||||
| 74 | 3 | $about =~ s/\n/ /mg; | |||||||||
| 75 | 3 | say ucfirst($about); | |||||||||
| 76 | |||||||||||
| 77 | 3 | say "\nUsage:"; | |||||||||
| 78 | 3 | print join "", map {s/%n/$name/g; " $_\n"} @{$MODULES{$type}->{usage}}; | |||||||||
| 79 | |||||||||||
| 80 | 3 | my $descr = pod_section($class, "description"); | |||||||||
| 81 | 3 | chomp $descr; | |||||||||
| 82 | 3 | ▶ | say "\n$descr" if $descr; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 83 | |||||||||||
| 84 | # TODO: include examples? | ||||||||||
| 85 | |||||||||||
| 86 | 3 | my $options = pod_section($class, "configuration"); | |||||||||
| 87 | 3 | ▶ | if ($options) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 2 | $options =~ s/^([a-z0-9_-]+)\s*\n?/--$1, /mgi; | |||||||||
| 89 | 2 | $options | |||||||||
| 90 | 13 | =~ s/^(--[a-z0-9_-]+(,\s*--[a-z0-9_-]+)*),\s*([^-])/" $1\n $3"/emgi; | |||||||||
| 91 | 2 | print "\nOptions:\n$options"; | |||||||||
| 92 | } | ||||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 13 | 1; | |||||||||
| 96 | |||||||||||
| 97 | __END__ | ||||||||||
| 98 | |||||||||||
| 99 | =pod | ||||||||||
| 100 | |||||||||||
| 101 | =head1 NAME | ||||||||||
| 102 | |||||||||||
| 103 | Catmandu::Cmd::help - show help | ||||||||||
| 104 | |||||||||||
| 105 | =head1 EXAMPLES | ||||||||||
| 106 | |||||||||||
| 107 | catmandu help convert | ||||||||||
| 108 | catmandu help import JSON | ||||||||||
| 109 | catmandu help help | ||||||||||
| 110 | |||||||||||
| 111 | =cut | ||||||||||