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.05';
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 <name> --resume FILE carry on a saved game; --replay FILE watches one
23 game list every game, with its version or "not installed"
24 game help <name> that game's own --help
25 game --help | --version
26  
27 options, before the name:
28 --ascii plain characters, in whatever words the game uses for that
29 --no-colour no colour, by setting NO_COLOR for the game
30 --no-pick type moves instead of choosing them with the arrow keys
31  
32 the games:
33 $rows
34 exit status:
35 the game's own: 0 won, drew or quit, 1 lost, 2 a bad option
36 2 a bad option to game itself, or a name that is no game
37 3 the game died
38  
39 A name is matched after lowercasing and dropping punctuation, and the
40 aliases work too: draughts, othello, ur, morris, tafl, weiqi, baduk,
41 chinesechess, awale, rummy, crib, 66. NO_COLOR in the environment turns
42 colour off everywhere.
43 USAGE
44 }
45  
46 1 ▶sub list {
Subroutine list: called CC=3 SCAR=11.1
47 1 for my $row (Game::Terminal::games()) {
48 24 my $version = Game::Terminal::version_of($row);
49 printf "%-11s %-6s %-4s %-46s %s\n", $row->{key}, $row->{family}, $row->{players},
50 24 ▶ $row->{blurb}, defined $version ? "$row->{class} $version" : 'not installed';
Branch: 1 branch
BranchTrueFalseTotal
defined $version ? : 24 0 24
51 }
52 1 return 0;
53 }
54  
55 9 ▶sub main {
Subroutine main: called CC=20 SCAR=32.8
56 9 my @argv = @_;
57 9 my %opt;
58 9 my $saved = Getopt::Long::Configure(qw(bundling no_ignore_case pass_through require_order));
59 9 my $ok = eval {
60 0 ▶ local $SIG{__WARN__} = sub { print STDERR "game: $_[0]" };
Subroutine __ANON__: not called CC=1 SCAR=1.2
61 9 Getopt::Long::GetOptionsFromArray(\@argv, \%opt,
62 'ascii', 'colour|color!', 'pick!', 'version|v', 'help|h');
63 };
64 9 Getopt::Long::Configure($saved);
65 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
66 1 ▶ print STDERR "game: not an option: $argv[0]\n" if $ok;
Branch: 1 branch
BranchTrueFalseTotal
if $ok 1 0 1
67 1 print STDERR usage();
68 1 return 2;
69 }
70 8 ▶ if ($opt{version}) { print "game, Game::Terminal $Game::Terminal::VERSION\n"; return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"version"}) 1 7 8
71 7 ▶ if ($opt{help}) { print usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
if ($opt{"help"}) 1 6 7
72  
73 6 ▶ my %common = (ascii => $opt{ascii} ? 1 : 0, colour => $opt{colour}, pick => $opt{pick});
Branch: 1 branch
BranchTrueFalseTotal
$opt{'ascii'} ? : 1 5 6
74 6 my $word = shift @argv;
75  
76 6 ▶ if (!defined $word) {
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $word) 0 6 6
77 0 require Game::Terminal::Menu;
78 0 return Game::Terminal::Menu->new(common => \%common)->start;
79 }
80 6 ▶ if ($word eq 'list') { return list() }
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "list") 1 5 6
81 5 ▶ if ($word eq 'help') {
Branch: 1 branch
BranchTrueFalseTotal
if ($word eq "help") 1 4 5
82 1 $word = shift @argv;
83 0 ▶ if (!defined $word) { print usage(); return 0 }
Branch: 1 branch
BranchTrueFalseTotal
unless (defined $word) 0 1 1
84 1 @argv = ('--help');
85 }
86  
87 5 my $row = Game::Terminal::find($word);
88 5 ▶ if (!$row) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($row) 1 4 5
89 1 my @near = Game::Terminal::nearest($word);
90 1 ▶ print STDERR "game: no such game '$word'." . (@near ? " Did you mean $near[0]?" : '') . "\n";
Branch: 1 branch
BranchTrueFalseTotal
@near ? : 1 0 1
91 1 return 2;
92 }
93 4 ▶ if (!Game::Terminal::installed($row)) {
Branch: 1 branch
BranchTrueFalseTotal
unless (Game::Terminal::installed($row)) 0 4 4
94 0 print STDERR "game: $row->{key} needs $row->{module}: cpanm $row->{module}\n";
95 0 return 2;
96 }
97  
98 4 my @flags = Game::Terminal::flags_for($row, \%common);
99 my ($status, $error) = Game::Terminal::launch($row, [ @flags, @argv ],
100 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
101 4 ▶ if (defined $error) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined $error) 0 4 4
102 0 print STDERR "game: $row->{key} died: $error\n";
103 0 return 3;
104 }
105 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
106 }
107  
108 1;
109  
110 __END__
↑ Summary blib/lib/Game/Terminal/Menu.pm »