File Coverage

blib/lib/App/Base/Script/Option.pm
Criterion Covered Total %
statement 20 20 100.0
branch 4 4 100.0
condition n/a
subroutine 6 6 100.0
pod 2 2 100.0
total 32 32 100.0


line stmt bran cond sub pod time code
1             use Moose;
2 13     13   122464  
  13         479303  
  13         89  
3             our $VERSION = '0.08'; ## VERSION
4              
5             =head1 NAME
6              
7             App::Base::Script::Option - OO interface for command-line options
8              
9             =head1 SYNOPSIS
10              
11             my $option = App::Base::Script::Option->new(
12             {
13             name => 'foo',
14             display => '--foo=<f>',
15             documentation => 'Controls the foo behavior of my script.',
16             default => 4,
17             option_type => 'integer',
18             }
19             );
20              
21             =head1 DESCRIPTION
22              
23             App::Base::Script::Option is used by App::Base::Script::Common and its
24             descendents to implement the standard definition of command-
25             line options. Typically an object of this class will be
26             constructed anonymously as part of the anonymous arrayref
27             return value of the options() method:
28              
29             sub options {
30             return [
31             App::Base::Script::Option->new(
32             name => 'foo',
33             documentation => 'The foo option',
34             option_type => 'integer',
35             ),
36             App::Base::Script::Option->new(
37             name => 'bar',
38             documentation => 'The bar option',
39             ),
40             ];
41             }
42              
43             =head1 ATTRIBUTES
44              
45             =head2 name
46              
47             The name of the attribute that must be specified on the command line.
48             This name follows Getopt::Long rules, so its usage can be reduced to
49             the shortest unambiguous specification. In other words, if the options
50             'fibonacci' and 'fortune' are options to the same script, then --fi
51             and --fo are valid options but -f (or --f) are not because of the
52             ambiguity between the two options.
53              
54             =head2 display
55              
56             The name as it is displayed in a usage (--help) option (switch) table.
57             By default, it is the same as the name; this method is provided for
58             cases in which it may be helpful to have a usage statement that shows
59             a sample value such as '--max-timeout=<timeout>' rather than simply
60             saying '--max-timeout', because the meaning of --max-timeout is then
61             explained in terms of <timeout> in the documentation for the option.
62              
63             =head2 documentation
64              
65             A scalar (string) which documents the behavior of the option. REQUIRED.
66              
67             =head2 default
68              
69             The default value of the option, if any.
70              
71             =head2 option_type
72              
73             One of: 'integer', 'float', 'string', or 'switch'.
74              
75             The content of an option field is verified against the provided value
76             during option parsing. For example, --foo=Fred will cause a failure
77             if the 'foo' option was declared to have option_type 'integer'.
78              
79             =cut
80              
81             use MooseX::Types -declare => [qw(script_option_type)];
82 13     13   97684 use MooseX::Types::Moose qw( Str );
  13         510795  
  13         76  
83 13     13   76532  
  13         220832  
  13         107  
84             subtype script_option_type, as Str, where {
85             $_ =~ /^(integer|float|string|switch)$/;
86             }, message {
87             "Invalid option type $_";
88             };
89              
90             has [qw(name documentation)] => (
91             is => 'ro',
92             isa => 'Str',
93             required => 1,
94             );
95              
96             has [qw(default display)] => (
97             is => 'ro',
98             );
99              
100             has [qw(option_type)] => (
101             is => 'ro',
102             isa => script_option_type,
103             default => 'switch',
104             );
105              
106             =head1 METHODS
107              
108             =head2 display_name
109              
110             Returns the display name of the option, which is either $self->display or
111             (if $self->display is not defined) $self->name. This value is used to
112             generate the switch table documentation.
113              
114             =cut
115              
116             my $self = shift;
117             if ($self->display) {
118 58     58 1 237 return $self->display;
119 58 100       1652 } else {
120 30         1157 return $self->name;
121             }
122 28         662 }
123              
124             =head2 show_documentation
125              
126             Returns documentation string for the option
127              
128             =cut
129              
130             my $self = shift;
131             if ($self->default) {
132             return $self->documentation . ' (default: ' . $self->default . ')';
133 29     29 1 54 } else {
134 29 100       710 return $self->documentation;
135 15         394 }
136             }
137 14         355  
138             no Moose;
139             __PACKAGE__->meta->make_immutable;
140              
141 13     13   65001 1;
  13         36  
  13         99  
142