lib/Serge/Command/help.pm

filter: Statementstmt 27.5% 19 / 69 Branchbran 0.0% 0 / 28 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 46.1% 6 / 13 Podpod 0.0% 0 / 7 totaltotal 20.4% 25 / 122 SCAR 52.5
CC
18
Cov
19%
show_brief_help
42.0
show_in_browser
30.0
init
20.0
CRAP 190.2
« lib/Serge/FindFiles.pm ↑ Summary lib/Serge/Engine/Plugin/keys_language.pm »
1 package Serge::Command::help;
2 1 ▶use parent Serge::Command;
Subroutine BEGIN: called CC=1 SCAR=0.0
3  
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 1 ▶use Getopt::Long;
Subroutine BEGIN: called CC=1 SCAR=0.0
7 1 ▶use Serge::Pod;
Subroutine BEGIN: called CC=1 SCAR=0.0
8 1 ▶use Serge;
Subroutine BEGIN: called CC=1 SCAR=0.0
9 1 ▶use Serge::Util::Pager;
Subroutine BEGIN: called CC=1 SCAR=0.0
10  
11 sub get_commands {
12 return {
13 0 ▶ '' => {handler => \&show_default_help},
Subroutine get_commands: not called CC=1 SCAR=6.9
Pod undocumented
14 help => {handler => \&show_help, info => 'Show help on main application and other commands'},
15 }
16 }
17  
18 sub init {
19 0 ▶ my ($self, $command) = @_;
Subroutine init: not called CC=4 SCAR=30.0
Pod undocumented
20  
21 0 $self->SUPER::init($command);
22  
23 # OS-specific checks
24  
25 0 ▶ $self->{browser} = 1 if ($^O eq 'MSWin32');
Branch: 1 branch
BranchTrueFalseTotal
if $^O eq "MSWin32" 0 0 0
26  
27 0 my $console;
28 0 ▶ GetOptions(
Branch: 1 branch
BranchTrueFalseTotal
&GetOptions("console", \$console) or die "Failed to parse some command-line parameters." 0 0 0
29 'console' => \$console,
30 ) or die "Failed to parse some command-line parameters.";
31  
32 0 ▶ $self->{browser} = undef if $console;
Branch: 1 branch
BranchTrueFalseTotal
if $console 0 0 0
33 }
34  
35 sub show_brief_help {
36 0 ▶ my ($self, $message) = @_;
Subroutine show_brief_help: not called CC=6 SCAR=37.4
Pod undocumented
37  
38 0 my @commands = sort keys %{$self->{parent}->{commands}};
39  
40 0 ▶ print $message ? $message."\n" : "Serge $Serge::VERSION - Free, Open-Source Solution for Continuous Localization\n";
Branch: 1 branch
BranchTrueFalseTotal
$message ? : 0 0 0
41 0 $self->{parent}->show_synopsis;
42  
43 # determine the max size of commands to render second column
44 # with their information nicely aligned
45 0 my $max_command_len;
46 0 foreach (@commands) {
47 0 ▶ $max_command_len = length($_) if length($_) > $max_command_len;
Branch: 1 branch
BranchTrueFalseTotal
if length $_ > $max_command_len 0 0 0
48 }
49  
50 0 print "Available commands:\n";
51 0 foreach (@commands) {
52 0 my $padding = ' ' x ($max_command_len - length($_));
53 0 my $info = $self->{parent}->{commands}->{$_}->{info};
54 0 ▶ print " $_$padding $info\n" if $_ ne '';
Branch: 1 branch
BranchTrueFalseTotal
if $_ ne "" 0 0 0
55 }
56 0 print "\n";
57 }
58  
59 sub show_default_help {
60 0 ▶ my ($self, $command) = @_;
Subroutine show_default_help: not called CC=1 SCAR=6.9
Pod undocumented
61 0 $self->show_brief_help; # with no message
62  
63 0 return 0;
64 }
65  
66 sub show_help {
67 0 ▶ my ($self, $command) = @_;
Subroutine show_help: not called CC=3 SCAR=24.8
Pod undocumented
68  
69 0 my $help_on_command = shift @ARGV;
70 0 ▶ if ($self->{parent}->known_command($help_on_command)) {
Branch: 1 branch
BranchTrueFalseTotal
if ($self->{'parent'}->known_command($help_on_command)) { } 0 0 0
71 0 ▶ $self->show_help_on_topic($help_on_command eq '' ? 'serge' : "serge-$help_on_command");
Branch: 1 branch
BranchTrueFalseTotal
$help_on_command eq '' ? : 0 0 0
72 } else {
73 0 $self->show_brief_help("Unknown command: '$help_on_command'");
74 0 return 1;
75 }
76  
77 0 return 0;
78 }
79  
80 sub show_help_on_topic {
81 0 ▶ my ($self, $command) = @_;
Subroutine show_help_on_topic: not called CC=4 SCAR=30.0
Pod undocumented
82  
83 0 my $pod = Serge::Pod->new();
84  
85 0 ▶ if (!$self->{browser} || !$self->show_in_browser($pod->get_html_path($command))) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $self->{"browser"} && $self->show_in_browser($pod->get_html_path($command))) 0 0 0
Condition0%0/3
$self->{'browser'} && $self->show_in_browser($pod->get_html_path($command))
!ll&&!rl&&r
000
MC/DC0%0/2
$self->{'browser'} && $self->show_in_browser($pod->get_html_path($command))
$self->{'browser'} $self->show_in_browser($pod->get_html_path($command))
Truth table0%0/3
A && B
A= $self->{'browser'}B= $self->show_in_browser($pod->get_html_path($command))
ABresult
0X0
100
111
86  
87 0 my $podfile = $pod->get_pod_path($command);
88 0 ▶ if (!-f $podfile) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $podfile) 0 0 0
89 0 print "Sorry, but there's no help available for '$command' in '$pod->{root}'\n";
90 0 exit(2);
91 }
92  
93 0 my $fh = Serge::Util::Pager::init;
94 0 $pod->print_as_text($podfile, $fh);
95 0 Serge::Util::Pager::close;
96 }
97 }
98  
99 sub show_in_browser {
100 0 ▶ my ($self, $html_file) = @_;
Subroutine show_in_browser: not called CC=5 SCAR=34.0
Pod undocumented
101  
102 0 ▶ return undef unless -f $html_file;
Branch: 1 branch
BranchTrueFalseTotal
unless -f $html_file 0 0 0
103  
104 # Windows
105 0 ▶ if ($^O eq 'MSWin32') {
Branch: 1 branch
BranchTrueFalseTotal
if ($^O eq "MSWin32") 0 0 0
106 0 ▶ print "MSWin32: executing `$html_file`\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
107 0 `$html_file`; # this should open the .html file in the default associated application (typically, browser)
108 0 my $exitcode = $? >> 8;
109  
110 0 ▶ print "Exit code: $exitcode\n" if $self->{parent}->{debug};
Branch: 1 branch
BranchTrueFalseTotal
if $self->{"parent"}{"debug"} 0 0 0
111 0 return $exitcode == 0;
112 }
113  
114 0 return undef;
115 }
116  
117  
118 1 1;
« lib/Serge/FindFiles.pm ↑ Summary lib/Serge/Engine/Plugin/keys_language.pm »