| 1 | package Locale::Simple::CLI; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | # ABSTRACT: Command line interface behind the locale-simple command | ||||||||||||||||||||||||||||||||||||||||||
| 3 | 11 | our $VERSION = '0.200'; | |||||||||||||||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||||||||||||||||
| 5 | 11 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 11 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | |||||||||||||||||||||||||||||||||||||||||||
| 8 | 11 | ▶ | use Getopt::Long (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | 11 | ▶ | use File::Basename (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 10 | 11 | ▶ | use Locale::Simple (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 11 | 11 | ▶ | use Locale::Simple::PO2JSON (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 12 | 11 | ▶ | use Locale::Simple::Scraper (); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 13 | |||||||||||||||||||||||||||||||||||||||||||
| 14 | 11 | my $USAGE = <<'END'; | |||||||||||||||||||||||||||||||||||||||||
| 15 | Usage: locale-simple COMMAND [ARGS] | ||||||||||||||||||||||||||||||||||||||||||
| 16 | |||||||||||||||||||||||||||||||||||||||||||
| 17 | Commands: | ||||||||||||||||||||||||||||||||||||||||||
| 18 | scrape [OPTIONS] find translation calls in the sources below the | ||||||||||||||||||||||||||||||||||||||||||
| 19 | current directory (locale-simple scrape --help) | ||||||||||||||||||||||||||||||||||||||||||
| 20 | po2json [--domain NAME] [--pretty] FILE.po | ||||||||||||||||||||||||||||||||||||||||||
| 21 | convert a PO file to JSON for the browser | ||||||||||||||||||||||||||||||||||||||||||
| 22 | help show this text | ||||||||||||||||||||||||||||||||||||||||||
| 23 | --version show the version | ||||||||||||||||||||||||||||||||||||||||||
| 24 | END | ||||||||||||||||||||||||||||||||||||||||||
| 25 | |||||||||||||||||||||||||||||||||||||||||||
| 26 | 38 | ▶ | sub run { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
run: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 27 | 38 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 28 | |||||||||||||||||||||||||||||||||||||||||||
| 29 | 38 | ▶ | my $cmd = @argv ? shift @argv : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 30 | 38 | ▶ | if ( $cmd eq 'help' or $cmd eq '--help' or $cmd eq '-h' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition100%6/6
$cmd eq "help" or $cmd eq "--help" or $cmd eq "-h"
$cmd eq "help" or $cmd eq "--help"
MC/DC100%3/3
$cmd eq "help" or $cmd eq "--help" or $cmd eq "-h"
$cmd eq "help"
$cmd eq "--help"
$cmd eq "-h"
Truth table100%4/4
A or B or C
A= $cmd eq "help"B= $cmd eq "--help"C= $cmd eq "-h"
| |||||||||||||||||||||||||||||||||||||||||||
| 31 | 3 | print STDOUT $USAGE; | |||||||||||||||||||||||||||||||||||||||||
| 32 | 3 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 33 | } | ||||||||||||||||||||||||||||||||||||||||||
| 34 | 35 | ▶ | if ( $cmd eq '--version' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 35 | 2 | print STDOUT "locale-simple $Locale::Simple::VERSION\n"; | |||||||||||||||||||||||||||||||||||||||||
| 36 | 2 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 37 | } | ||||||||||||||||||||||||||||||||||||||||||
| 38 | 33 | ▶ | return _scrape(@argv) if $cmd eq 'scrape'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 11 | ▶ | return _po2json(@argv) if $cmd eq 'po2json'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 40 | |||||||||||||||||||||||||||||||||||||||||||
| 41 | 2 | ▶ | print STDERR $cmd eq '' ? "Missing command\n" : "Unknown command: $cmd\n"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 42 | 2 | print STDERR $USAGE; | |||||||||||||||||||||||||||||||||||||||||
| 43 | 2 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||||||||||||||||
| 45 | |||||||||||||||||||||||||||||||||||||||||||
| 46 | 22 | ▶ | sub _scrape { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_scrape: called CC=5 SCAR=16.1
| |||||||||||||||||||||||||||||||||||||||||||
| 47 | 22 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 48 | 22 | ▶ | eval { Locale::Simple::Scraper::scrape(@argv); 1 } or do { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 49 | 5 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 50 | 5 | ▶ | if ( ref $err and ref $err eq 'Locale::Simple::Scraper::BadCall' ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
ref $err and ref $err eq "Locale::Simple::Scraper::BadCall"
MC/DC50%1/2
ref $err and ref $err eq "Locale::Simple::Scraper::BadCall"
ref $err
ref $err eq "Locale::Simple::Scraper::BadCall"
Truth table66%2/3
A and B
A= ref $errB= ref $err eq "Locale::Simple::Scraper::BadCall"
| |||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | print STDERR $err->{message}, Locale::Simple::Scraper::usage(); | |||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 53 | } | ||||||||||||||||||||||||||||||||||||||||||
| 54 | 1 | ▶ | $err .= "\n" unless $err =~ /\n\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 55 | 1 | print STDERR $err; | |||||||||||||||||||||||||||||||||||||||||
| 56 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 57 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 58 | 17 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||||||||||||||||
| 61 | 9 | ▶ | sub _po2json { | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
_po2json: called CC=8 SCAR=20.9
| |||||||||||||||||||||||||||||||||||||||||||
| 62 | 9 | my (@argv) = @_; | |||||||||||||||||||||||||||||||||||||||||
| 63 | 9 | my ( $domain, $pretty ); | |||||||||||||||||||||||||||||||||||||||||
| 64 | 9 | my $parser = Getopt::Long::Parser->new( config => ['no_ignore_case'] ); | |||||||||||||||||||||||||||||||||||||||||
| 65 | 9 | my $ok = do { | |||||||||||||||||||||||||||||||||||||||||
| 66 | 0 | ▶ | local $SIG{__WARN__} = sub { print STDERR @_ }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=1.2
| |||||||||||||||||||||||||||||||||||||||||||
| 67 | 9 | $parser->getoptionsfromarray( \@argv, 'domain=s' => \$domain, 'pretty' => \$pretty ); | |||||||||||||||||||||||||||||||||||||||||
| 68 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 69 | 9 | ▶ | if ( !$ok or @argv != 1 ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition66%2/3
not $ok or @argv != 1
MC/DC50%1/2
not $ok or @argv != 1
not $ok
@argv != 1
Truth table66%2/3
not(A) or B
A= not $okB= @argv != 1
| |||||||||||||||||||||||||||||||||||||||||||
| 70 | 1 | ▶ | print STDERR "po2json needs exactly one PO file\n" if $ok; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 71 | 1 | print STDERR $USAGE; | |||||||||||||||||||||||||||||||||||||||||
| 72 | 1 | return 2; | |||||||||||||||||||||||||||||||||||||||||
| 73 | } | ||||||||||||||||||||||||||||||||||||||||||
| 74 | 8 | my ($file) = @argv; | |||||||||||||||||||||||||||||||||||||||||
| 75 | 8 | ▶ | $domain = File::Basename::basename( $file, '.po', '.pot' ) unless defined $domain; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 76 | 8 | my $json = eval { | |||||||||||||||||||||||||||||||||||||||||
| 77 | 8 | ▶ | Locale::Simple::PO2JSON::data2json( | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 78 | Locale::Simple::PO2JSON::po2data( $file, domain => $domain ), | ||||||||||||||||||||||||||||||||||||||||||
| 79 | pretty => $pretty ? 1 : 0, | ||||||||||||||||||||||||||||||||||||||||||
| 80 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 81 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 82 | 8 | ▶ | if ( !defined $json ) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 83 | 1 | my $err = $@; | |||||||||||||||||||||||||||||||||||||||||
| 84 | 1 | ▶ | $err .= "\n" unless $err =~ /\n\z/; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 85 | 1 | print STDERR $err; | |||||||||||||||||||||||||||||||||||||||||
| 86 | 1 | return 1; | |||||||||||||||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||||||||||||||||
| 88 | 7 | print STDOUT $json, "\n"; | |||||||||||||||||||||||||||||||||||||||||
| 89 | 7 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 90 | } | ||||||||||||||||||||||||||||||||||||||||||
| 91 | |||||||||||||||||||||||||||||||||||||||||||
| 92 | 11 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | __END__ | ||||||||||||||||||||||||||||||||||||||||||