lib/Zuzu/Tidy/CLI.pm

filter: Statementstmt 78.6% 59 / 75 Branchbran 57.1% 16 / 28 Conditioncond 16.6% 1 / 6 MC/DCmcdc 0.0% 0 / 4 Subroutinesub 87.5% 7 / 8 Podpod 100% 1 / 1 totaltotal 68.8% 84 / 122 CC 17 SCAR 24.2
CC
17
Cov
70%
run
16.9
_print_usage
12.0
_parse_options
3.1
CRAP 11.3
« blib/lib/Zuzu/Module/Eval.pm ↑ Summary blib/lib/Zuzu/Module/IO.pm »
1 package Zuzu::Tidy::CLI;
2  
3 4 ▶use utf8;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 4 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 4 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6  
7 4 our $VERSION = '0.007002';
8  
9 4 ▶use Getopt::Long qw(
Subroutine BEGIN: called CC=1 SCAR=0.0
10 Configure
11 GetOptionsFromArray
12 4 );
13  
14 4 ▶use Zuzu::Tidy;
Subroutine BEGIN: called CC=1 SCAR=0.0
15  
16 4 ▶sub run {
Subroutine run: called CC=13 SCAR=28.3
Pod documented
17 4 my ( @argv ) = @_;
18 4 ▶ @argv = @ARGV if not @argv;
Branch: 1 branch
BranchTrueFalseTotal
unless @argv 0 4 4
19  
20 4 binmode *STDOUT, ':utf8';
21 4 binmode *STDERR, ':utf8';
22  
23 4 my ( $options, $args, $error ) = _parse_options( \@argv );
24 4 ▶ if ( defined $error ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 4 4
25 0 _print_usage($error);
26 0 return 2;
27 }
28  
29 4 my $file = shift @{ $args };
30 4 ▶ if ( $options->{stdin} ) {
Branch: 2 branches
BranchTrueFalseTotal
if ($options->{'stdin'}) { } 1 3 4
elsif (not defined $file or $file eq '') { } 0 3 3
31 1 ▶ if ( defined $file ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $file) 0 1 1
32 0 _print_usage('--stdin does not accept a script path');
33 0 return 2;
34 }
35 1 ▶ if ( $options->{in_place} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"in_place"}) 0 1 1
36 0 _print_usage('--stdin cannot be combined with --in-place');
37 0 return 2;
38 }
39 1 binmode *STDIN, ':encoding(UTF-8)';
40 }
41 ▶ elsif ( not defined $file or $file eq '' ) {
Condition33%1/3
not defined $file or $file eq ""
l!l&&r!l&&!r
003
MC/DC0%0/2
not defined $file or $file eq ""
not defined $file $file eq ""
Truth table33%1/3
not(A) or B
A= not defined $fileB= $file eq ""
ABresult
1X1
011
000
42 0 _print_usage('Missing script path');
43 0 return 2;
44 }
45 4 ▶ if ( @{ $args } ) {
Branch: 1 branch
BranchTrueFalseTotal
if (@{$args;}) 0 4 4
46 0 _print_usage('Too many arguments');
47 0 return 2;
48 }
49  
50 4 my $source;
51 4 my $filename = $file;
52 4 ▶ if ( $options->{stdin} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{'stdin'}) { } 1 3 4
53 1 $source = do { local $/; <STDIN> };
54 1 $filename = '<stdin>';
55 }
56 else {
57 3 ▶ open my $in_fh, '<:encoding(UTF-8)', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $in_fh, "<:encoding(UTF-8)", $file or die "Could not open '${file}': $!\n" 3 0 3
58 or die "Could not open '$file': $!\n";
59 3 $source = do { local $/; <$in_fh> };
60 3 close $in_fh;
61 }
62  
63 my $tidied = Zuzu::Tidy->tidy(
64 $source,
65 filename => $filename,
66 canonical_operators => $options->{canonical_operators},
67 4 );
68  
69 4 ▶ if ( $options->{in_place} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"in_place"}) 1 3 4
70 1 ▶ open my $out_fh, '>:encoding(UTF-8)', $file
Branch: 1 branch
BranchTrueFalseTotal
open my $out_fh, ">:encoding(UTF-8)", $file or die "Could not write '${file}': $!\n" 1 0 1
71 or die "Could not write '$file': $!\n";
72 1 print {$out_fh} $tidied;
73 1 close $out_fh;
74 1 return 0;
75 }
76  
77 3 print $tidied;
78 3 return 0;
79 }
80  
81 4 ▶sub _parse_options {
Subroutine _parse_options: called CC=3 SCAR=11.4
82 4 my ( $argv ) = @_;
83  
84 4 my $options = {
85 in_place => 0,
86 stdin => 0,
87 canonical_operators => 0,
88 };
89  
90 4 Configure(
91 'no_ignore_case',
92 'bundling',
93 );
94 my $ok = GetOptionsFromArray(
95 $argv,
96 'i|in-place' => \$options->{in_place},
97 'stdin' => \$options->{stdin},
98 'canonical-operators!' => \$options->{canonical_operators},
99 'h|help' => \$options->{help},
100 4 );
101 4 ▶ return ( undef, undef, undef ) if not $ok;
Branch: 1 branch
BranchTrueFalseTotal
unless $ok 0 4 4
102  
103 4 ▶ if ( $options->{help} ) {
Branch: 1 branch
BranchTrueFalseTotal
if ($options->{"help"}) 0 4 4
104 0 return ( undef, undef, '' );
105 }
106  
107 4 return ( $options, $argv, undef );
108 }
109  
110 0 ▶sub _print_usage {
Subroutine _print_usage: not called CC=3 SCAR=24.8
111 0 my ( $message ) = @_;
112  
113 0 ▶ if ( defined $message and $message ne '' ) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $message and $message ne "") 0 0 0
Condition0%0/3
defined $message and $message ne ""
!ll&&!rl&&r
000
MC/DC0%0/2
defined $message and $message ne ""
defined $message $message ne ""
Truth table0%0/3
A and B
A= defined $messageB= $message ne ""
ABresult
0X0
100
111
114 0 print STDERR $message, "\n";
115 }
116 0 print STDERR "Usage: zuzu-tidy.pl [--stdin] [--in-place] [--canonical-operators] path/to/script.zzs\n";
117  
118 0 return;
119 }
120  
121 =pod
122  
123 =head1 NAME
124  
125 Zuzu::Tidy::CLI - command-line wrapper for Zuzu::Tidy
126  
127 =head1 SYNOPSIS
128  
129 zuzu-tidy.pl path/to/script.zzs
130 zuzu-tidy.pl --stdin < path/to/script.zzs
131 zuzu-tidy.pl --canonical-operators path/to/script.zzs
132 zuzu-tidy.pl --in-place path/to/script.zzs
133  
134 =head1 DESCRIPTION
135  
136 Provides the executable interface for C<bin/zuzu-tidy.pl>. The command reads a
137 ZuzuScript file, formats it via C<Zuzu::Tidy>, and writes to STDOUT by
138 default or updates the file in-place with C<--in-place>.
139  
140 C<--stdin> reads source from STDIN and writes tidied output to STDOUT. It
141 cannot be combined with C<--in-place>.
142  
143 C<--canonical-operators> rewrites non-canonical operator spellings such as
144 C<*> and C<< |> >> to canonical forms such as C<×> and C<▷>. This is
145 opt-in; aliases are preserved by default.
146  
147 =head1 METHODS
148  
149 =head2 run
150  
151 Parses CLI options, formats the target file, and returns a process exit code.
152  
153 =head1 COPYRIGHT AND LICENCE
154  
155 B<< Zuzu::Tidy::CLI >> is copyright Toby Inkster.
156  
157 It is free software; you may redistribute it and/or modify it under
158 the terms of either the Artistic License 1.0 or the GNU General Public
159 License version 2.
160  
161 =cut
162  
163 4 1;
« blib/lib/Zuzu/Module/Eval.pm ↑ Summary blib/lib/Zuzu/Module/IO.pm »