Login
Apply the unmodified patch from Schwern (modulo dealing with
[gknop/Perl-Critic.git] / lib / Perl / Critic / OptionsProcessor.pm
CommitLineData
dc93df4f
JRT
1##############################################################################
2# $URL$
3# $Date$
4# $Author$
5# $Revision$
dc93df4f
JRT
6##############################################################################
7
894d344a 8package Perl::Critic::OptionsProcessor;
dc93df4f 9
df6dee2b 10use 5.006001;
dc93df4f
JRT
11use strict;
12use warnings;
ee5a2bbd 13
dc93df4f 14use English qw(-no_match_vars);
ee5a2bbd 15
ee5a2bbd
ES
16use Perl::Critic::Exception::AggregateConfiguration;
17use Perl::Critic::Exception::Configuration::Option::Global::ExtraParameter;
e424327a
ES
18use Perl::Critic::Utils qw<
19 :booleans :characters :severities :data_conversion $DEFAULT_VERBOSITY
20>;
21use Perl::Critic::Utils::Constants qw< $PROFILE_STRICTNESS_DEFAULT >;
22use Perl::Critic::Utils::DataConversion qw< dor >;
dc93df4f 23
be4331b3 24our $VERSION = '1.090';
dc93df4f
JRT
25
26#-----------------------------------------------------------------------------
27
28sub new {
dc93df4f
JRT
29 my ($class, %args) = @_;
30 my $self = bless {}, $class;
31 $self->_init( %args );
32 return $self;
33}
34
35#-----------------------------------------------------------------------------
36
37sub _init {
dc93df4f
JRT
38 my ( $self, %args ) = @_;
39
40 # Multi-value defaults
e424327a 41 my $exclude = dor(delete $args{exclude}, $EMPTY);
6a51d540 42 $self->{_exclude} = [ words_from_string( $exclude ) ];
e424327a 43 my $include = dor(delete $args{include}, $EMPTY);
6a51d540 44 $self->{_include} = [ words_from_string( $include ) ];
410cf90b 45
dc93df4f 46 # Single-value defaults
e424327a
ES
47 $self->{_force} = dor(delete $args{force}, $FALSE);
48 $self->{_only} = dor(delete $args{only}, $FALSE);
9f12283e 49 $self->{_profile_strictness} =
e424327a
ES
50 dor(delete $args{'profile-strictness'}, $PROFILE_STRICTNESS_DEFAULT);
51 $self->{_single_policy} = dor(delete $args{'single-policy'}, $EMPTY);
52 $self->{_severity} = dor(delete $args{severity}, $SEVERITY_HIGHEST);
53 $self->{_theme} = dor(delete $args{theme}, $EMPTY);
54 $self->{_top} = dor(delete $args{top}, $FALSE);
55 $self->{_verbose} = dor(delete $args{verbose}, $DEFAULT_VERBOSITY);
e424327a 56 $self->{_criticism_fatal} = dor(delete $args{'criticism-fatal'}, $FALSE);
89b50090 57 $self->{_pager} = dor(delete $args{pager}, $EMPTY);
0d63e03a 58
89b50090
ES
59 # If we're using a pager or not outputing to a tty don't use colors.
60 my $default_color = ($self->pager or !-t *STDOUT) ? 0 : 1;
61 $self->{_color} = dor(delete $args{color}, dor(delete $args{colour}, $default_color));
ae9b3404 62
ee5a2bbd
ES
63 # If there's anything left, complain.
64 _check_for_extra_options(%args);
65
66 return $self;
67}
68
69#-----------------------------------------------------------------------------
70
71sub _check_for_extra_options {
72 my %args = @_;
73
0d63e03a 74 if ( my @remaining = sort keys %args ){
ee5a2bbd
ES
75 my $errors = Perl::Critic::Exception::AggregateConfiguration->new();
76
77 foreach my $option_name (@remaining) {
78 $errors->add_exception(
79 Perl::Critic::Exception::Configuration::Option::Global::ExtraParameter->new(
80 option_name => $option_name,
81 )
82 )
83 }
84
85 $errors->rethrow();
0d63e03a 86 }
dc93df4f 87
ee5a2bbd 88 return;
dc93df4f
JRT
89}
90
91#-----------------------------------------------------------------------------
58247edc 92# Public ACCESSOR methods
410cf90b 93
dc93df4f
JRT
94sub severity {
95 my ($self) = @_;
96 return $self->{_severity};
97}
98
99#-----------------------------------------------------------------------------
100
101sub theme {
102 my ($self) = @_;
103 return $self->{_theme};
104}
105
106#-----------------------------------------------------------------------------
107
108sub exclude {
109 my ($self) = @_;
110 return $self->{_exclude};
111}
112
113#-----------------------------------------------------------------------------
114
115sub include {
116 my ($self) = @_;
117 return $self->{_include};
118}
119
120#-----------------------------------------------------------------------------
121
122sub only {
123 my ($self) = @_;
124 return $self->{_only};
125}
126
127#-----------------------------------------------------------------------------
128
9f12283e 129sub profile_strictness {
66186ba3 130 my ($self) = @_;
9f12283e 131 return $self->{_profile_strictness};
66186ba3
ES
132}
133
134#-----------------------------------------------------------------------------
135
738830ba 136sub single_policy {
585ddee1 137 my ($self) = @_;
738830ba 138 return $self->{_single_policy};
585ddee1
ES
139}
140
141#-----------------------------------------------------------------------------
142
dc93df4f
JRT
143sub verbose {
144 my ($self) = @_;
145 return $self->{_verbose};
146}
147
148#-----------------------------------------------------------------------------
149
51ae9d9b 150sub color {
25792f52 151 my ($self) = @_;
51ae9d9b 152 return $self->{_color};
25792f52
ES
153}
154
155#-----------------------------------------------------------------------------
156
89b50090
ES
157sub pager {
158 my ($self) = @_;
159 return $self->{_pager};
160}
161
162#-----------------------------------------------------------------------------
163
badbf753
JRT
164sub criticism_fatal {
165 my ($self) = @_;
166 return $self->{_criticism_fatal};
167}
168
169#-----------------------------------------------------------------------------
170
dc93df4f
JRT
171sub force {
172 my ($self) = @_;
173 return $self->{_force};
174}
175
176#-----------------------------------------------------------------------------
177
178sub top {
179 my ($self) = @_;
180 return $self->{_top};
181}
182
183
1841;
185
186__END__
187
188#-----------------------------------------------------------------------------
189
190=pod
191
192=head1 NAME
193
894d344a 194Perl::Critic::OptionsProcessor - The global configuration default values, combined with command-line values.
dc93df4f 195
11f53956 196
dc93df4f
JRT
197=head1 DESCRIPTION
198
199This is a helper class that encapsulates the default parameters for
11f53956
ES
200constructing a L<Perl::Critic::Config|Perl::Critic::Config> object.
201There are no user-serviceable parts here.
202
dc93df4f
JRT
203
204=head1 CONSTRUCTOR
205
206=over 8
207
208=item C< new( %DEFAULT_PARAMS ) >
209
11f53956
ES
210Returns a reference to a new C<Perl::Critic::OptionsProcessor> object.
211You can override the coded defaults by passing in name-value pairs
212that correspond to the methods listed below.
9f12283e 213
11f53956
ES
214This is usually only invoked by
215L<Perl::Critic::UserProfile|Perl::Critic::UserProfile>, which passes
216in the global values from a F<.perlcriticrc> file. This object
9f12283e 217contains no information for individual Policies.
1ce891fa 218
dc93df4f
JRT
219=back
220
221=head1 METHODS
222
223=over 8
224
225=item C< exclude() >
226
1ce891fa 227Returns a reference to a list of the default exclusion patterns. If
11f53956
ES
228onto by
229L<Perl::Critic::PolicyParameter|Perl::Critic::PolicyParameter>. there
230are no default exclusion patterns, then the list will be empty.
231
1ce891fa 232
dc93df4f
JRT
233=item C< force() >
234
1ce891fa
JRT
235Returns the default value of the C<force> flag (Either 1 or 0).
236
11f53956 237
dc93df4f
JRT
238=item C< include() >
239
1ce891fa
JRT
240Returns a reference to a list of the default inclusion patterns. If
241there are no default exclusion patterns, then the list will be empty.
242
11f53956 243
dc93df4f
JRT
244=item C< only() >
245
1ce891fa
JRT
246Returns the default value of the C<only> flag (Either 1 or 0).
247
11f53956 248
9f12283e 249=item C< profile_strictness() >
66186ba3 250
9f12283e
ES
251Returns the default value of C<profile_strictness> as an unvalidated
252string.
66186ba3 253
11f53956 254
738830ba 255=item C< single_policy() >
585ddee1 256
badbf753 257Returns the default C<single-policy> pattern. (As a string.)
585ddee1 258
11f53956 259
dc93df4f
JRT
260=item C< severity() >
261
1ce891fa
JRT
262Returns the default C<severity> setting. (1..5).
263
11f53956 264
dc93df4f
JRT
265=item C< theme() >
266
1ce891fa
JRT
267Returns the default C<theme> setting. (As a string).
268
11f53956 269
dc93df4f
JRT
270=item C< top() >
271
1ce891fa
JRT
272Returns the default C<top> setting. (Either 0 or a positive integer).
273
11f53956 274
dc93df4f
JRT
275=item C< verbose() >
276
1ce891fa
JRT
277Returns the default C<verbose> setting. (Either a number or format
278string).
279
11f53956 280
51ae9d9b 281=item C< color() >
25792f52 282
51ae9d9b 283Returns the default C<color> setting. (Either 1 or 0).
25792f52 284
11f53956 285
89b50090
ES
286=item C< pager() >
287
288Returns the default C<pager> setting. (Either empty string or the pager
289command string).
290
291
badbf753
JRT
292=item C< criticism_fatal() >
293
294Returns the default C<criticism-fatal> setting (Either 1 or 0).
295
11f53956 296
dc93df4f
JRT
297=back
298
11f53956 299
f7392d70
ES
300=head1 SEE ALSO
301
11f53956
ES
302L<Perl::Critic::Config|Perl::Critic::Config>,
303L<Perl::Critic::UserProfile|Perl::Critic::UserProfile>
304
f7392d70 305
dc93df4f
JRT
306=head1 AUTHOR
307
308Jeffrey Ryan Thalhammer <thaljef@cpan.org>
309
11f53956 310
dc93df4f
JRT
311=head1 COPYRIGHT
312
20dfddeb 313Copyright (c) 2005-2008 Jeffrey Ryan Thalhammer. All rights reserved.
dc93df4f
JRT
314
315This program is free software; you can redistribute it and/or modify
316it under the same terms as Perl itself. The full text of this license
317can be found in the LICENSE file included with this module.
318
319=cut
737d3b65
CD
320
321# Local Variables:
322# mode: cperl
323# cperl-indent-level: 4
324# fill-column: 78
325# indent-tabs-mode: nil
326# c-indentation-style: bsd
327# End:
96fed375 328# ex: set ts=8 sts=4 sw=4 tw=78 ft=perl expandtab shiftround :