#============================================================================= # # Module: Term::CLI::Argument::Number # # Description: Base class for numerical arguments in Term::CLI # # Author: Steven Bakker (SBAKKER), # Created: 22/01/18 # # Copyright (c) 2018 Steven Bakker # # This module is free software; you can redistribute it and/or modify # it under the same terms as Perl itself. See "perldoc perlartistic." # # This software is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. # #============================================================================= package Term::CLI::Argument::Number 0.053004 { use 5.014; use strict; use warnings; use Term::CLI::L10N; use Moo 1.000001; use namespace::clean 0.25; extends 'Term::CLI::Argument'; has min => ( is => 'rw', clearer => 1, predicate => 1 ); has max => ( is => 'rw', clearer => 1, predicate => 1 ); has inclusive => ( is => 'rw', default => sub {1} ); sub coerce_value { die "coerce_value() has not been overloaded"; } sub validate { my ($self, $value) = @_; if (!defined $value || length($value) == 0) { return $self->set_error(loc('not a valid number')); } my $num = $self->coerce_value($value); if (!defined $num) { return $self->set_error(loc('not a valid number')); } if ($self->inclusive) { if ($self->has_min && $num < $self->min) { return $self->set_error(loc('too small')); } elsif ($self->has_max && $num > $self->max) { return $self->set_error(loc('too large')); } } else { if ($self->has_min && $num <= $self->min) { return $self->set_error(loc('too small')); } elsif ($self->has_max && $num >= $self->max) { return $self->set_error(loc('too large')); } } return $num; } } 1; __END__ =pod =head1 NAME Term::CLI::Argument::Number - base class for numerical arguments in Term::CLI =head1 VERSION version 0.053004 =head1 SYNOPSIS use Term::CLI::Argument::Number; my $arg = Term::CLI::Argument::Number->new( name => 'arg1', min => 1 max => 2 inclusive => 1 ); =head1 DESCRIPTION Base class for numerical arguments in L(3p). This class cannot be used directly, but should be extended by sub-classes. =head1 CLASS STRUCTURE =head2 Inherits from: L(3p). =head2 Consumes: None. =head1 CONSTRUCTORS =over =item B ( B =E I, ... ) X Create a new Term::CLI::Argument::Number object and return a reference to it. The B attribute is required. Other attributes that are recognised: =over =item B =E I The minimum valid value (by default an I boundary, but see L below. =item B =E I The maximum valid value (by default an I boundary, but see L below. =item B =E I Default is 1 (true). Indicates whether minimum/maximum boundaries are inclusive or exclusive. =back =back =head1 ACCESSORS Inherited from L(3p). Additionally, the following are defined: =over =item B ( I ) =item B ( I ) Lower and upper boundaries, resp. =item B ( I ) Boolean indicating whether the boundaries are inclusive. =item B =item B Booleans, indicate whether C and C have been set, resp. =item B =item B Clear the C and C limits, resp. =back =head1 METHODS Inherited from L(3p). Additionally: =over =item B ( I ) The L method uses the L method to convert I to a suitable number and then checks any boundaries. =item B ( I ) This method I be overridden by sub-classes. It will be called with a single argument (the I) and is supposed to return the converted number. If the number is not valid, it should return C. =back =head1 SEE ALSO L(3p), L(3p). =head1 AUTHOR Steven Bakker Esbakker@cpan.orgE, 2018. =head1 COPYRIGHT AND LICENSE Copyright (c) 2018 Steven Bakker This module is free software; you can redistribute it and/or modify it under the same terms as Perl itself. See "perldoc perlartistic." This software is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. =cut