| 1 | #! /bin/false | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # Copyright (C) 2016-2018 Guido Flohr <guido.flohr@cantanea.com>, | ||||||||||||||
| 4 | # all rights reserved. | ||||||||||||||
| 5 | |||||||||||||||
| 6 | # This program is free software: you can redistribute it and/or modify | ||||||||||||||
| 7 | # it under the terms of the GNU General Public License as published by | ||||||||||||||
| 8 | # the Free Software Foundation; either version 3 of the License, or | ||||||||||||||
| 9 | # (at your option) any later version. | ||||||||||||||
| 10 | |||||||||||||||
| 11 | # This program is distributed in the hope that it will be useful, | ||||||||||||||
| 12 | # but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||||||||||||
| 13 | # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||||||||||||||
| 14 | # GNU General Public License for more details. | ||||||||||||||
| 15 | |||||||||||||||
| 16 | # You should have received a copy of the GNU General Public License | ||||||||||||||
| 17 | # along with this program. If not, see <http://www.gnu.org/licenses/>. | ||||||||||||||
| 18 | |||||||||||||||
| 19 | package Qgoda::CLI; | ||||||||||||||
| 20 | 2 | $Qgoda::CLI::VERSION = 'v0.9.2'; | |||||||||||||
| 21 | 2 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 22 | |||||||||||||||
| 23 | 2 | ▶ | use IO::Handle; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 24 | 2 | ▶ | use Locale::TextDomain qw(qgoda); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 25 | 2 | ▶ | use Getopt::Long 2.36 qw(GetOptionsFromArray); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 26 | |||||||||||||||
| 27 | 2 | ▶ | use Qgoda; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 28 | 2 | ▶ | use Qgoda::Util qw(perl_class class2module); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 29 | |||||||||||||||
| 30 | sub new { | ||||||||||||||
| 31 | 2 | ▶ | my ($class, $argv) = @_; | ||||||||||||
|
Subroutine
new: called CC=4 SCAR=14.5
Pod
undocumented
| |||||||||||||||
| 32 | |||||||||||||||
| 33 | 2 | ▶ | $argv ||= [@ARGV]; | ||||||||||||
|
Condition50%1/2
$argv ||= [@ARGV]
MC/DC0%0/1
$argv ||= [@ARGV]
$argv
Truth table50%1/2
A
A= $argv
| |||||||||||||||
| 34 | |||||||||||||||
| 35 | 2 | my (@args, $cmd); | |||||||||||||
| 36 | |||||||||||||||
| 37 | # Split arguments into global options like '--verbose', a command, | ||||||||||||||
| 38 | # and command-specific options. We simplify this by stipulating that | ||||||||||||||
| 39 | # global options cannot take any arguments. So the first command-line | ||||||||||||||
| 40 | # argument that does not start with a hyphen is the command, the rest | ||||||||||||||
| 41 | # are options and arguments for that command. | ||||||||||||||
| 42 | 2 | ▶ | while (@$argv) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | 2 | my $arg = shift @$argv; | |||||||||||||
| 44 | 2 | ▶ | if ($arg =~ /^-[-a-zA-Z0-9]/) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 45 | 0 | push @args, $arg; | |||||||||||||
| 46 | } else { | ||||||||||||||
| 47 | 2 | $cmd = $arg; | |||||||||||||
| 48 | 2 | last; | |||||||||||||
| 49 | } | ||||||||||||||
| 50 | } | ||||||||||||||
| 51 | |||||||||||||||
| 52 | bless { | ||||||||||||||
| 53 | 2 | __global_options => \@args, | |||||||||||||
| 54 | __cmd => $cmd, | ||||||||||||||
| 55 | __cmd_args => [@$argv], | ||||||||||||||
| 56 | }, $class; | ||||||||||||||
| 57 | } | ||||||||||||||
| 58 | |||||||||||||||
| 59 | sub dispatch { | ||||||||||||||
| 60 | 2 | ▶ | my ($self) = @_; | ||||||||||||
|
Subroutine
dispatch: called CC=7 SCAR=22.1
Pod
undocumented
| |||||||||||||||
| 61 | |||||||||||||||
| 62 | 2 | autoflush STDOUT, 1; | |||||||||||||
| 63 | 2 | autoflush STDERR, 1; | |||||||||||||
| 64 | |||||||||||||||
| 65 | 2 | my %options; | |||||||||||||
| 66 | 2 | Getopt::Long::Configure('bundling'); | |||||||||||||
| 67 | { | ||||||||||||||
| 68 | 2 | local $SIG{__WARN__} = sub { | |||||||||||||
| 69 | 0 | ▶ | $SIG{__WARN__} = 'DEFAULT'; | ||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||||||
| 70 | 0 | $self->usageError(shift); | |||||||||||||
| 71 | 2 | }; | |||||||||||||
| 72 | |||||||||||||||
| 73 | GetOptionsFromArray($self->{__global_options}, | ||||||||||||||
| 74 | 'log-stderr' => \$options{log_stderr}, | ||||||||||||||
| 75 | 'q|quiet' => \$options{quiet}, | ||||||||||||||
| 76 | 'h|help' => \$options{help}, | ||||||||||||||
| 77 | 'v|verbose' => \$options{verbose}, | ||||||||||||||
| 78 | 'V|version' => \$options{version}, | ||||||||||||||
| 79 | 2 | ); | |||||||||||||
| 80 | } | ||||||||||||||
| 81 | |||||||||||||||
| 82 | 2 | ▶ | $self->displayUsage if $options{help}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 2 | ▶ | $self->displayVersion if $options{version}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | |||||||||||||||
| 85 | my $cmd = $self->{__cmd} | ||||||||||||||
| 86 | 2 | ▶ | or $self->usageError(__"no command given!"); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 87 | 2 | $cmd =~ s/-/::/g; | |||||||||||||
| 88 | $self->usageError(__x("invalid command name '{command}'", | ||||||||||||||
| 89 | 2 | ▶ | command => $self->{__cmd})) | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 90 | if !perl_class $cmd; | ||||||||||||||
| 91 | |||||||||||||||
| 92 | $cmd = join '::', map { | ||||||||||||||
| 93 | 2 | ucfirst $_; | |||||||||||||
| 94 | } split /::/, $cmd; | ||||||||||||||
| 95 | |||||||||||||||
| 96 | 2 | my $class = 'Qgoda::Command::' . $cmd; | |||||||||||||
| 97 | 2 | my $module = class2module $class; | |||||||||||||
| 98 | |||||||||||||||
| 99 | 2 | eval { require $module }; | |||||||||||||
| 100 | 2 | ▶ | if ($@) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 101 | 0 | ▶ | if ($@ =~ m{^Can't locate $module in \@INC}) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 102 | $self->usageError(__x("unknown command '{command}'", | ||||||||||||||
| 103 | 0 | command => $self->{__cmd})); | |||||||||||||
| 104 | } else { | ||||||||||||||
| 105 | 0 | my $msg = $@; | |||||||||||||
| 106 | 0 | chomp $msg; | |||||||||||||
| 107 | die __x("{program}: {command}: {error}\n", | ||||||||||||||
| 108 | program => $0, | ||||||||||||||
| 109 | command => $self->{__cmd}, | ||||||||||||||
| 110 | 0 | error => $msg); | |||||||||||||
| 111 | } | ||||||||||||||
| 112 | } | ||||||||||||||
| 113 | |||||||||||||||
| 114 | 2 | return $class->new->run($self->{__cmd_args}, \%options); | |||||||||||||
| 115 | } | ||||||||||||||
| 116 | |||||||||||||||
| 117 | sub displayUsage { | ||||||||||||||
| 118 | 0 | ▶ | my $msg = __x(<<EOF, program => $0); | ||||||||||||
|
Subroutine
displayUsage: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 119 | Usage: {program} COMMAND [OPTIONS] | ||||||||||||||
| 120 | EOF | ||||||||||||||
| 121 | |||||||||||||||
| 122 | 0 | $msg .= "\n"; | |||||||||||||
| 123 | |||||||||||||||
| 124 | 0 | $msg .= __<<EOF; | |||||||||||||
| 125 | Mandatory arguments to long options, are mandatory to short options, too. | ||||||||||||||
| 126 | EOF | ||||||||||||||
| 127 | |||||||||||||||
| 128 | 0 | $msg .= "\n"; | |||||||||||||
| 129 | |||||||||||||||
| 130 | 0 | $msg .= __<<EOF; | |||||||||||||
| 131 | The following commands are currently supported: | ||||||||||||||
| 132 | EOF | ||||||||||||||
| 133 | |||||||||||||||
| 134 | 0 | $msg .= "\n"; | |||||||||||||
| 135 | |||||||||||||||
| 136 | 0 | $msg .= __<<EOF; | |||||||||||||
| 137 | build build site and exit | ||||||||||||||
| 138 | watch build, then watch for changes and build on demand | ||||||||||||||
| 139 | config dump the current configuration and exit | ||||||||||||||
| 140 | init initialize a new qgoda site | ||||||||||||||
| 141 | dump dump the site structure as JSON (implies --quiet) | ||||||||||||||
| 142 | markdown process Markdown | ||||||||||||||
| 143 | xgettext extract translatable strings from Markdown | ||||||||||||||
| 144 | files | ||||||||||||||
| 145 | po various commands for processing translations | ||||||||||||||
| 146 | EOF | ||||||||||||||
| 147 | |||||||||||||||
| 148 | 0 | $msg .= "\n"; | |||||||||||||
| 149 | |||||||||||||||
| 150 | 0 | $msg .= __<<EOF; | |||||||||||||
| 151 | Operation mode: | ||||||||||||||
| 152 | -q, --quiet quiet mode | ||||||||||||||
| 153 | -v, --verbose verbosely log what is going on | ||||||||||||||
| 154 | --log-stderr log to standard error instead of standard out | ||||||||||||||
| 155 | EOF | ||||||||||||||
| 156 | |||||||||||||||
| 157 | 0 | $msg .= "\n"; | |||||||||||||
| 158 | |||||||||||||||
| 159 | 0 | $msg .= __<<EOF; | |||||||||||||
| 160 | Informative output: | ||||||||||||||
| 161 | -h, --help display this help and exit | ||||||||||||||
| 162 | -V, --version output version information and exit | ||||||||||||||
| 163 | EOF | ||||||||||||||
| 164 | |||||||||||||||
| 165 | 0 | $msg .= "\n"; | |||||||||||||
| 166 | |||||||||||||||
| 167 | 0 | $msg .= __x(<<EOF, program => $0); | |||||||||||||
| 168 | Try '{program} --help' for more information or visit | ||||||||||||||
| 169 | http://www.qgoda.net/en/docs/ for extensive documentation. | ||||||||||||||
| 170 | EOF | ||||||||||||||
| 171 | |||||||||||||||
| 172 | 0 | print $msg; | |||||||||||||
| 173 | |||||||||||||||
| 174 | 0 | exit 0; | |||||||||||||
| 175 | } | ||||||||||||||
| 176 | |||||||||||||||
| 177 | sub commandUsageError { | ||||||||||||||
| 178 | 0 | ▶ | my ($class, $cmd, $message, $usage) = @_; | ||||||||||||
|
Subroutine
commandUsageError: not called CC=5 SCAR=34.0
Pod
undocumented
| |||||||||||||||
| 179 | |||||||||||||||
| 180 | 0 | ▶ | if ($message) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 181 | 0 | $message =~ s/\s+$//; | |||||||||||||
| 182 | 0 | ▶ | if (defined $cmd) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 183 | 0 | $message = "$0 $cmd: $message\n"; | |||||||||||||
| 184 | } else { | ||||||||||||||
| 185 | 0 | $message = "$0: $message\n"; | |||||||||||||
| 186 | } | ||||||||||||||
| 187 | } else { | ||||||||||||||
| 188 | 0 | $message = ''; | |||||||||||||
| 189 | } | ||||||||||||||
| 190 | |||||||||||||||
| 191 | 0 | ▶ | if (defined $usage) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 192 | 0 | $message .= __x(<<EOF, program => $0, command => $cmd, usage => $usage); | |||||||||||||
| 193 | Usage: {program} [GLOBAL_OPTIONS] {usage} | ||||||||||||||
| 194 | Try '{program} {command} --help' for more information! | ||||||||||||||
| 195 | EOF | ||||||||||||||
| 196 | } elsif (defined $cmd) { | ||||||||||||||
| 197 | 0 | $message .= __x(<<EOF, program => $0, command => $cmd); | |||||||||||||
| 198 | Usage: {program} [GLOBAL_OPTIONS] {command} [OPTIONS] | ||||||||||||||
| 199 | Try '{program} {command} --help' for more information! | ||||||||||||||
| 200 | EOF | ||||||||||||||
| 201 | } else { | ||||||||||||||
| 202 | 0 | $message .= __x(<<EOF, program => $0); | |||||||||||||
| 203 | Usage: {program} [GLOBAL_OPTIONS] COMMAND [OPTIONS] | ||||||||||||||
| 204 | Try '{program} --help' for more information! | ||||||||||||||
| 205 | EOF | ||||||||||||||
| 206 | } | ||||||||||||||
| 207 | |||||||||||||||
| 208 | 0 | die $message; | |||||||||||||
| 209 | } | ||||||||||||||
| 210 | |||||||||||||||
| 211 | sub usageError { | ||||||||||||||
| 212 | 0 | ▶ | my ($class, $message) = @_; | ||||||||||||
|
Subroutine
usageError: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 213 | |||||||||||||||
| 214 | 0 | return $class->commandUsageError(undef, $message); | |||||||||||||
| 215 | } | ||||||||||||||
| 216 | |||||||||||||||
| 217 | sub displayVersion { | ||||||||||||||
| 218 | 0 | ▶ | my $msg = __x('{program} (Qgoda static site generator) {version} | ||||||||||||
|
Subroutine
displayVersion: not called CC=1 SCAR=6.9
Pod
undocumented
| |||||||||||||||
| 219 | Copyright (C) {years} Cantanea EOOD (http://www.cantanea.com/). | ||||||||||||||
| 220 | License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html> | ||||||||||||||
| 221 | This is free software: you are free to change and redistribute it. | ||||||||||||||
| 222 | There is NO WARRANTY, to the extent permitted by law. | ||||||||||||||
| 223 | Written by Guido Flohr (http://www.guido-flohr.net/). | ||||||||||||||
| 224 | ', program => $0, years => 2016-2018, version => $Qgoda::VERSION); | ||||||||||||||
| 225 | |||||||||||||||
| 226 | 0 | print $msg; | |||||||||||||
| 227 | |||||||||||||||
| 228 | 0 | exit 0; | |||||||||||||
| 229 | } | ||||||||||||||
| 230 | |||||||||||||||
| 231 | 2 | 1; | |||||||||||||