File Coverage

blib/lib/Dancer/GetOpt.pm
Criterion Covered Total %
statement 29 30 96.6
branch 2 6 33.3
condition n/a
subroutine 13 14 92.8
pod 0 4 0.0
total 44 54 81.4


line stmt bran cond sub pod time code
1             package Dancer::GetOpt;
2             our $AUTHORITY = 'cpan:SUKRIA';
3             # ABSTRACT: Process command-line options for Dancer scripts
4             $Dancer::GetOpt::VERSION = '1.3520';
5 167     167   72564 use strict;
  167         444  
  167         5235  
6 167     167   922 use warnings;
  167         380  
  167         5324  
7              
8 167     167   2541 use Dancer::Config 'setting';
  167         421  
  167         7998  
9 167     167   130032 use Getopt::Long;
  167         1834886  
  167         846  
10 167     167   98449 use FindBin;
  167         182560  
  167         7626  
11 167     167   1269 use File::Spec;
  167         403  
  167         54555  
12              
13             my $options = {
14             port => setting('port'),
15             daemon => setting('daemon'),
16             confdir => setting('confdir') || setting('appdir'),
17             environment => 'development',
18             };
19              
20             sub arg_to_setting {
21 8     8 0 17 my ($option, $value) = @_;
22 8         27 setting($option => $value);
23             }
24              
25             sub process_args {
26 105     105 0 3031 my $help = 0;
27             GetOptions(
28             'help' => \$help,
29 4     4   2240 'port=i' => sub { arg_to_setting(@_) },
30 2     2   917 'daemon' => sub { arg_to_setting(@_) },
31 1     1   465 'environment=s' => sub { arg_to_setting(@_) },
32 1     1   541 'confdir=s' => sub { arg_to_setting(@_) },
33 105 50       1413 ) || usage_and_exit();
34              
35 105 50       48132 usage_and_exit() if $help;
36             }
37              
38 0 0   0 0 0 sub usage_and_exit { print_usage() && exit(0) }
39              
40             sub print_usage {
41 1     1 0 308 my $app = File::Spec->catfile( $FindBin::RealBin, $FindBin::RealScript );
42 1         34 print <
43             \$ $app [options]
44              
45             Options:
46             --daemon Run in background (false)
47             --port=XXXX Port number to bind to (3000)
48             --confdir=PATH Path the config dir (appdir if not specified)
49             --environment=ENV Environment to use (development)
50             --help Display usage information
51              
52             OPTIONS
53              
54             --daemon
55              
56             When this flag is set, the Dancer script will detach from the terminal and will
57             run in background. This is perfect for production environment but is not handy
58             during the development phase.
59              
60             --port=XXXX
61              
62             This lets you change the port number to use when running the process. By
63             default, the port 3000 will be used.
64              
65             --confdir=PATH
66              
67             By default, Dancer looks in the appdir for config files (config.yml and
68             environments files). You can change this with specifying an alternate path to
69             the configdir option.
70              
71             Dancer will then look in that directory for a file config.yml and the
72             appropriate environement configuration file.
73              
74             If not specified, confdir points to appdir.
75              
76             --environment=ENV
77              
78             Which environment to use. By default this value is set to development.
79              
80              
81             EOF
82             }
83              
84             'Dancer::GetOpt';
85              
86             __END__