| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 31 | 1 | ▶ | if ( defined $file ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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 ""
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 ""
| |||||||||||||||||||||||||||||
| 42 | 0 | _print_usage('Missing script path'); | |||||||||||||||||||||||||||
| 43 | 0 | return 2; | |||||||||||||||||||||||||||
| 44 | } | ||||||||||||||||||||||||||||
| 45 | 4 | ▶ | if ( @{ $args } ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 70 | 1 | ▶ | open my $out_fh, '>:encoding(UTF-8)', $file | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
| |||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||
| 103 | 4 | ▶ | if ( $options->{help} ) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 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
Condition0%0/3
defined $message and $message ne ""
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 ""
| |||||||||||||||||||||||||||||
| 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; | |||||||||||||||||||||||||||