bin/game

filter: Statementstmt 86.7% 59 / 68 Branchbran 70.5% 24 / 34 Conditioncond 58.3% 7 / 12 MC/DCmcdc 28.5% 2 / 7 Subroutinesub 88.8% 8 / 9 Podpod n/a totaltotal 76.9% 100 / 130 CC 22 SCAR 29.0
CC
22
Cov
79%
main
26.5
list
3.0
__ANON__
1.1
CRAP 18.2
↑ Summary blib/lib/Game/Terminal/Menu.pm »
1 #!/usr/bin/env perl
2 9 ▶use 5.014;
Subroutine BEGIN: called CC=1 SCAR=0.0
3 9 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 9 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
5  
6 9 ▶use Getopt::Long ();
Subroutine BEGIN: called CC=1 SCAR=0.0
7 9 ▶use Game::Terminal;
Subroutine BEGIN: called CC=1 SCAR=0.0
8  
9 9 our $VERSION = '0.04';
10  
11 9 ▶exit main(@ARGV) unless caller;
Branch: 1 branch
BranchTrueFalseTotal
unless caller 9 0 9
12  
13 2 ▶sub usage {
Subroutine usage: called CC=1 SCAR=0.0
14 my $rows = join '', map {
15 2 sprintf " %-11s %-6s %-4s %s\n", $_->{key}, $_->{family}, $_->{players}, $_->{blurb}
16 48 } Game::Terminal::games();
17 2 return <<"USAGE";
18 game - one command for every terminal game
19  
20 usage: game the menu: choose a game, set its options, play
21 game <name> [options] start that game; the options are the game's own
22 game list every game, with its version or "not installed"
23 game help <name> that game's own --help
24 game --help | --version
25  
26 options, before the name:
27 --ascii plain characters, in whatever words the game uses for that
28 --no-colour no colour, by setting NO_COLOR for the game
29 --no-pick type moves instead of choosing them with the arrow keys
30  
31 the games:
32 $rows
33 exit status:
34 the game's own: 0 won, drew or quit, 1 lost, 2 a bad option
35 2 a bad option to game itself, or a name that is no game
36 3 the game died
37  
38 A name is matched after lowercasing and dropping punctuation, and the
39 aliases work too: draughts, othello, ur, morris, tafl, weiqi, baduk,
40 chinesechess, awale, rummy, crib, 66. NO_COLOR in the environment turns
41 colour off everywhere.
42 USAGE
43 }
44  
45 1 ▶sub list {
Subroutine list: called CC=3 SCAR=11.1
46 1 for my $row (Game::Terminal::games()) {
47 24 my $version = Game::Terminal::version_of($row);
48 printf "%-11s %-6s %-4s %-46s %s\n", $row->{key}, $row->{family}, $row->{players},
49 24 ▶ $row->{blurb}, defined $version ? "$row->{class} $version" : 'not installed';
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 24 0 24
50 }
51 1 return 0;
52 }
53  
54 9 ▶sub main {
Subroutine main: called CC=20 SCAR=32.8
55 9 my @argv = @_;
56 9 my %opt;
57 9 my $saved = Getopt::Long::Configure(qw(bundling no_ignore_case pass_through require_order));
58 9 my $ok = eval {
59 0 ▶ local $SIG{__WARN__} = sub { print STDERR "game: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
60 9 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
61 'ascii', 'colour|color!', 'pick!', 'version|v', 'help|h');
62 };
63 9 Getopt::Long::Configure($saved);
64 9 ▶ if (!$ok || (@argv && $argv[0] =~ m/^-./)) {
Branch: 1 branch
BranchTrueFalseTotal
if (not $ok or @argv and $argv[0] =~ /^-./u) 1 8 9
Condition83%5/6
not $ok or @argv and $argv[0] =~ /^-./u
l!l&&r!l&&!r
018
@argv and $argv[0] =~ /^-./u
!ll&&!rl&&r
261
MC/DC66%2/3
not $ok or @argv and $argv[0] =~ /^-./u
not $ok @argv $argv[0] =~ /^-./u
Truth table75%3/4
not(A) or B and C
A= not $okB= @argvC= $argv[0] =~ /^-./u
ABCresult
1XX1
0111
00X0
0100
65 1 ▶ print STDERR "game: not an option: $argv[0]\n" if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 0 1
66 1 print STDERR usage();
67 1 return 2;
68 }
69 8 ▶ if ($opt{version}) { print "game, Game::Terminal $Game::Terminal::VERSION\n"; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 1 7 8
70 7 ▶ if ($opt{help}) { print usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 1 6 7
71  
72 6 ▶ my %common = (ascii => $opt{ascii} ? 1 : 0, colour => $opt{colour}, pick => $opt{pick});
Branch: 1 branch
BranchTrueFalseTotal
$opt{'ascii'} ? : 1 5 6
73 6 my $word = shift @argv;
74  
75 6 ▶ if (!defined $word) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $word) 0 6 6
76 0 require Game::Terminal::Menu;
77 0 return Game::Terminal::Menu->new(common => \%common)->start;
78 }
79 6 ▶ if ($word eq 'list') { return list() }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "list") 1 5 6
80 5 ▶ if ($word eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "help") 1 4 5
81 1 $word = shift @argv;
82 0 ▶ if (!defined $word) { print usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $word) 0 1 1
83 1 @argv = ('--help');
84 }
85  
86 5 my $row = Game::Terminal::find($word);
87 5 ▶ if (!$row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 1 4 5
88 1 my @near = Game::Terminal::nearest($word);
89 1 ▶ print STDERR "game: no such game '$word'." . (@near ? " Did you mean $near[0]?" : '') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
@near ? : 1 0 1
90 1 return 2;
91 }
92 4 ▶ if (!Game::Terminal::installed($row)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Game::Terminal::installed($row)) 0 4 4
93 0 print STDERR "game: $row->{key} needs $row->{module}: cpanm $row->{module}\n";
94 0 return 2;
95 }
96  
97 4 my @flags = Game::Terminal::flags_for($row, \%common);
98 my ($status, $error) = Game::Terminal::launch($row, [ @flags, @argv ],
99 4 ▶ no_colour => (defined $common{colour} && !$common{colour}) ? 1 : 0);
Branch: 1 branch
BranchTrueFalseTotal
defined $common{'colour'} && !$common{'colour'} ? : 0 4 4
Condition33%1/3
defined $common{"colour"} and not $common{"colour"}
!ll&&!rl&&r
400
MC/DC0%0/2
defined $common{"colour"} and not $common{"colour"}
defined $common{"colour"} not $common{"colour"}
Truth table33%1/3
A and not(B)
A= defined $common{"colour"}B= not $common{"colour"}
ABresult
0X0
100
111
100 4 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 4 4
101 0 print STDERR "game: $row->{key} died: $error\n";
102 0 return 3;
103 }
104 4 ▶ return defined $status && $status =~ m/^\d+$/ ? $status : 0;
Branch: 1 branch
BranchTrueFalseTotal
defined $status && $status =~ /^\d+$/u ? : 4 0 4
Condition33%1/3
defined $status and $status =~ /^\d+$/u
!ll&&!rl&&r
004
MC/DC0%0/2
defined $status and $status =~ /^\d+$/u
defined $status $status =~ /^\d+$/u
Truth table33%1/3
A and B
A= defined $statusB= $status =~ /^\d+$/u
ABresult
0X0
100
111
105 }
106  
107 1;
108  
109 __END__
↑ Summary blib/lib/Game/Terminal/Menu.pm »