File Coverage

blib/lib/Sport/Analytics/NHL/Usage.pm
Criterion Covered Total %
statement 23 68 33.8
branch 0 22 0.0
condition 0 6 0.0
subroutine 8 11 72.7
pod 3 3 100.0
total 34 110 30.9


line stmt bran cond sub pod time code
1             package Sport::Analytics::NHL::Usage;
2              
3 1     1   2945 use v5.10.1;
  1         3  
4 1     1   4 use strict;
  1         2  
  1         16  
5 1     1   4 use warnings FATAL => 'all';
  1         1  
  1         27  
6              
7 1     1   557 use Getopt::Long qw(:config no_ignore_case bundling);
  1         7498  
  1         4  
8              
9 1     1   130 use Sport::Analytics::NHL::Config;
  1         1  
  1         126  
10 1     1   5 use Sport::Analytics::NHL::LocalConfig;
  1         3  
  1         85  
11 1     1   5 use Sport::Analytics::NHL;
  1         1  
  1         31  
12              
13 1     1   4 use parent 'Exporter';
  1         1  
  1         4  
14              
15             our @EXPORT = qw(gopts);
16              
17             =head1 NAME
18              
19             Sport::Analytics::NHL::Usage - an internal utility module standardizing the usage of our applications.
20              
21             =head1 FUNCTIONS
22              
23             =over 2
24              
25             =item C
26              
27             =item C
28              
29             =item C
30              
31             this is the main wrapper for GetOptions to keep things coherent.
32              
33             =back
34              
35             =cut
36              
37             our $USAGE_MESSAGE = '';
38             our $def_db = $MONGO_DB || 'hockey';
39              
40             our %OPTS = (
41             standard => [
42             {
43             short => 'h', long => 'help',
44             action => sub { usage(); },
45             description => 'print this message and exit'
46             },
47             {
48             short => 'V', long => 'version',
49             action => sub { say hdb_version(); exit; },
50             description => 'print version and exit'
51             },
52             {
53             short => 'v', long => 'verbose',
54             action => sub { $ENV{HOCKEYDB_VERBOSE} = 1 },
55             description => 'produce verbose output to STDERR'
56             },
57             {
58             short => 'd', long => 'debug',
59             action => sub { $ENV{HOCKEYDB_DEBUG} = 1; },
60             description => 'produce debug output to STDERR'
61             },
62             ],
63             season => [
64             {
65             short => 's', long => 'start-season', arg => 'SEASON', type => 'i',
66             description => "Start at season SEASON (default $CURRENT_SEASON)",
67             },
68             {
69             short => 'S', long => 'stop-season', arg => 'SEASON', type => 'i',
70             description => "Stop at season SEASON (default $CURRENT_SEASON)",
71             },
72             {
73             short => 'T', long => 'stage', arg => 'STAGE', type => 'i',
74             description => "Scrape stage STAGE ($REGULAR: REGULAR, $PLAYOFF: PLAYOFF, default: $CURRENT_STAGE",
75             },
76             ],
77             database => [
78             {
79             long => 'no-database',
80             description => 'Do not use a MongoDB backend',
81             },
82             {
83             short => 'D', long => 'database', arg => 'DB', type => 's',
84             description => "Use Mongo database DB (default $def_db)"
85             },
86             ],
87             compile => [
88             {
89             long => 'no-compile',
90             description => 'Do not compile file even if storable is absent',
91             },
92             {
93             long => 'recompile',
94             description => 'Compile file even if storable is present',
95             }
96             ],
97             merge => [
98             {
99             long => 'no-merge',
100             description => 'Do not merge file even if storable is absent',
101             },
102             {
103             long => 'remerge',
104             description => 'Compile file even if storable is present',
105             }
106             ],
107             misc => [
108             {
109             short => 'f', long => 'force',
110             description => 'override/overwrite existing data',
111             },
112             {
113             long => 'test',
114             description => 'Test the validity of the files (use with caution)'
115             },
116             {
117             long => 'doc',
118             description => 'Only process reports of type doc (repeatable). Available types are: BS, PL, RO, GS, ES',
119             repeatable => 1, arg => 'DOC',
120             type => 's'
121             },
122             {
123             long => 'no-schedule-crawl',
124             description => 'Try to use schedule already present in the system',
125             },
126             {
127             short => 'E', long => 'data-dir', arg => 'DIR', type => 's',
128             description => "Data directory root (default $DATA_DIR)",
129             }
130             ],
131             );
132              
133             sub usage (;$) {
134              
135 0   0 0 1   my $status = shift || 0;
136              
137 0           print join("\n", <
138             $USAGE_MESSAGE
139             ENDUSAGE
140 0           exit $status;
141             }
142              
143             sub convert_opt ($) {
144              
145 0     0 1   my $opt = shift;
146              
147 0           my $c_opt = $opt;
148 0           $c_opt =~ s/\-/_/g;
149 0           $c_opt;
150             }
151              
152             sub gopts ($$$) {
153              
154 0     0 1   my $wid = shift;
155 0           my $opts = shift;
156 0           my $args = shift;
157              
158 0           my %g_opts = ();
159 0           my $u_opts = {};
160 0 0         my $u_arg = @{$args} ? ' Arguments' : '';
  0            
161 0           my $usage_message ="
162             \t\t$wid
163             \t\tUsage: $0 [Options]$u_arg
164             ";
165 0 0         unshift(@{$opts}, ':standard') unless grep {$_ eq '-standard'} @{$opts};
  0            
  0            
  0            
166 0 0         if (@{$opts}) {
  0            
167 0           $usage_message .= "\t\tOptions:\n";
168 0           for my $opt_group (@{ $opts }) {
  0            
169 0           my @opts;
170 0 0         if ($opt_group =~ /^\:(.*)/) {
171 0           @opts = @{ $OPTS{$1} };
  0            
172             }
173             else {
174 0           @opts = grep { $_->{long} eq $opt_group } @{ $OPTS{misc} };
  0            
  0            
175             }
176 0           for my $opt (@opts) {
177             $usage_message .= sprintf(
178             "\t\t\t%-20s %-10s %s\n",
179             ($opt->{short} ? "-$opt->{short}|" : '') . "--$opt->{long}",
180             $opt->{arg} || '',
181             $opt->{description},
182 0 0 0       );
183 0 0         my $is_repeatable = $opt->{repeatable} ? '@' : '';
184             $g_opts{
185             (($opt->{short} ? "$opt->{short}|" : '') . $opt->{long}) .
186             ($opt->{type} ? "=$opt->{type}$is_repeatable" : '')
187 0 0 0       } = ($opt->{action} || \$u_opts->{convert_opt($opt->{long})});
    0          
188             }
189             }
190             }
191             else {
192 0           $usage_message .= "\t\tNo Options\n";
193             }
194 0 0         if (@{$args}) {
  0            
195 0           $usage_message .= "\t\tArguments:\n";
196 0           for my $arg (@{$args}) {
  0            
197             $usage_message .= sprintf(
198             "\t\t\t%-20s %s%s",
199             $arg->{name}, $arg->{description},
200 0 0         $arg->{optional} ? ' [optional]' : ''
201             );
202             }
203             }
204 0           $USAGE_MESSAGE = $usage_message;
205 0 0         GetOptions(%g_opts) || usage();
206 0           $u_opts;
207             }
208              
209             1;
210              
211             =head1 AUTHOR
212              
213             More Hockey Stats, C<< >>
214              
215             =head1 BUGS
216              
217             Please report any bugs or feature requests to C, or through
218             the web interface at L. I will be notified, and then you'll
219             automatically be notified of progress on your bug as I make changes.
220              
221              
222             =head1 SUPPORT
223              
224             You can find documentation for this module with the perldoc command.
225              
226             perldoc Sport::Analytics::NHL::Usage
227              
228             You can also look for information at:
229              
230             =over 4
231              
232             =item * RT: CPAN's request tracker (report bugs here)
233              
234             L
235              
236             =item * AnnoCPAN: Annotated CPAN documentation
237              
238             L
239              
240             =item * CPAN Ratings
241              
242             L
243              
244             =item * Search CPAN
245              
246             L
247              
248             =back