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