Commandable

 view release on metacpan or  search on metacpan

lib/Commandable/Command.pm  view on Meta::CPAN

   --opt=value
   --opt value

C<multi_value> options can be supplied more than once; values are pushed into
an ARRAY reference which is passed in the options hash.

C<inc> options may be supplied more than once; each occurance will increment
the stored value by one.

=head2 default

   $val = $optspec->default;

A value to provide in the options hash if the user did not specify a different
one.

=head2 negatable

   $bool = $optspec->negatable;

If true, also accept a C<--no-OPT> option to reset the value of the option to
C<undef>.

=head2 typespec

I<Since version 0.13> no longer supported.

=head2 matches

   $re = $optspec->matches;

If defined, gives a precompiled regexp that any user-supplied value must
conform to.

A few shortcuts are provided, which are used if the provided name ends in
C<=i> (for "integer"), C<=u> (for "unsigned integer", i.e. non-negative) or
C<=f> (for "float").

=cut

my %typespecs = (
   i => [ "be an integer", qr/^-?\d+$/ ],
   u => [ "be a non-negative integer", qr/^\d+$/ ],
   f => [ "be a floating-point number", qr/^-?\d+(?:\.\d+)?$/ ],
);

sub new ( $class, %args )
{
   warn "Use of $args{name} in a Commandable command option name; should be " . $args{name} =~ s/:$/=/r
      if $args{name} =~ m/:$/;

   if( $args{name} =~ s/([=:])(.+?)$/$1/ ) {
      # Convert a type abbreviation
      my $typespec = $typespecs{$2} or
         die "Unrecognised typespec $2";

      ( $args{match_msg}, $args{matches} ) = @$typespec;
   }
   $args{mode} = "value" if $args{name} =~ s/[=:]$//;
   $args{mode} = "multi_value" if $args{multi};
   my @names = split m/\|/, delete $args{name};
   $args{mode} //= "set";
   $args{negatable} //= 1 if $args{mode} eq "bool";
   bless [ \@names, @args{qw( description mode default negatable matches match_msg )} ], $class;
}

sub name        { shift->[0]->[0] }
sub keyname     { shift->name =~ s/-/_/gr }
sub names       { shift->[0]->@* }
sub description { shift->[1] }
sub mode        { shift->[2] }
sub default     { shift->[3] }
sub negatable   { shift->[4] }
sub matches     { shift->[5] }
sub match_msg   { shift->[6] }

sub mode_expects_value { shift->mode =~ m/value$/ }

=head1 AUTHOR

Paul Evans <leonerd@leonerd.org.uk>

=cut

0x55AA;



( run in 1.204 second using v1.01-cache-2.11-cpan-483215c6ad5 )