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   51160 use Moose;
  13         302694  
  13         74  
3              
4             =head1 NAME
5              
6             App::Base::Script::Option - OO interface for command-line options
7              
8             =head1 VERSION
9              
10             This document describes App::Base version 0.05
11              
12             =head1 SYNOPSIS
13              
14             my $option = App::Base::Script::Option->new(
15             {
16             name => 'foo',
17             display => '--foo=<f>',
18             documentation => 'Controls the foo behavior of my script.',
19             default => 4,
20             option_type => 'integer',
21             }
22             );
23              
24             =head1 DESCRIPTION
25              
26             App::Base::Script::Option is used by App::Base::Script::Common and its
27             descendents to implement the standard definition of command-
28             line options. Typically an object of this class will be
29             constructed anonymously as part of the anonymous arrayref
30             return value of the options() method:
31              
32             sub options {
33             return [
34             App::Base::Script::Option->new(
35             name => 'foo',
36             documentation => 'The foo option',
37             option_type => 'integer',
38             ),
39             App::Base::Script::Option->new(
40             name => 'bar',
41             documentation => 'The bar option',
42             ),
43             ];
44             }
45              
46             =head1 ATTRIBUTES
47              
48             =head2 name
49              
50             The name of the attribute that must be specified on the command line.
51             This name follows Getopt::Long rules, so its usage can be reduced to
52             the shortest unambiguous specification. In other words, if the options
53             'fibonacci' and 'fortune' are options to the same script, then --fi
54             and --fo are valid options but -f (or --f) are not because of the
55             ambiguity between the two options.
56              
57             =head2 display
58              
59             The name as it is displayed in a usage (--help) option (switch) table.
60             By default, it is the same as the name; this method is provided for
61             cases in which it may be helpful to have a usage statement that shows
62             a sample value such as '--max-timeout=<timeout>' rather than simply
63             saying '--max-timeout', because the meaning of --max-timeout is then
64             explained in terms of <timeout> in the documentation for the option.
65              
66             =head2 documentation
67              
68             A scalar (string) which documents the behavior of the option. REQUIRED.
69              
70             =head2 default
71              
72             The default value of the option, if any.
73              
74             =head2 option_type
75              
76             One of: 'integer', 'float', 'string', or 'switch'.
77              
78             The content of an option field is verified against the provided value
79             during option parsing. For example, --foo=Fred will cause a failure
80             if the 'foo' option was declared to have option_type 'integer'.
81              
82             =cut
83              
84 13     13   62698 use MooseX::Types -declare => [qw(script_option_type)];
  13         298224  
  13         59  
85 13     13   43868 use MooseX::Types::Moose qw( Str );
  13         126099  
  13         80  
86              
87             subtype script_option_type, as Str, where {
88             $_ =~ /^(integer|float|string|switch)$/;
89             }, message {
90             "Invalid option type $_";
91             };
92              
93             has [qw(name documentation)] => (
94             is => 'ro',
95             isa => 'Str',
96             required => 1,
97             );
98              
99             has [qw(default display)] => (is => 'ro',);
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 170 my $self = shift;
119 58 100       1459 if ($self->display) {
120 30         608 return $self->display;
121             } else {
122 28         666 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 26 my $self = shift;
134 29 100       1059 if ($self->default) {
135 15         322 return $self->documentation . ' (default: ' . $self->default . ')';
136             } else {
137 14         352 return $self->documentation;
138             }
139             }
140              
141 13     13   42454 no Moose;
  13         14  
  13         89  
142             __PACKAGE__->meta->make_immutable;
143              
144             1;
145              
146             __END__
147              
148             =head1 LICENSE AND COPYRIGHT
149              
150             Copyright (C) 2010-2014 Binary.com
151              
152             This program is free software; you can redistribute it and/or modify it
153             under the terms of either: the GNU General Public License as published
154             by the Free Software Foundation; or the Artistic License.
155              
156             See http://dev.perl.org/licenses/ for more information.
157              
158             =cut