Actions Status MetaCPAN Release

NAME

Getopt::EX::Hashed - Hash store object automation for Getopt::Long

VERSION

Version 1.0502

SYNOPSIS

# script/foo
use App::foo;
App::foo->new->run();

# lib/App/foo.pm
package App::foo;

use Getopt::EX::Hashed; {
    Getopt::EX::Hashed->configure( DEFAULT => [ is => 'rw' ] );
    has start    => ' =i  s begin ' , default => 1;
    has end      => ' =i  e       ' ;
    has file     => ' =s@ f       ' , any => qr/^(?!\.)/;
    has score    => ' =i          ' , min => 0, max => 100;
    has answer   => ' =i          ' , must => sub { $_[1] == 42 };
    has mouse    => ' =s          ' , any => [ 'Frankie', 'Benjy' ];
    has question => ' =s          ' , any => qr/^(life|universe|everything)$/i;
} no Getopt::EX::Hashed;

sub run {
    my $app = shift;
    use Getopt::Long;
    $app->getopt or pod2usage();
    if ($app->answer == 42) {
        $app->question //= 'life';
        ...

DESCRIPTION

Getopt::EX::Hashed is a module to automate a hash object to store command line option values for Getopt::Long and compatible modules including Getopt::EX::Long.

Major objective of this module is integrating initialization and specification into single place.

Module name shares Getopt::EX, but it works independently from other modules in Getopt::EX, so far.

Accessor methods are automatically generated when appropriate parameter is given.

FUNCTION

has

Declare option parameters in a form of:

has option_name => ( param => value, ... );

If array reference is given, multiple names can be declared at once.

has [ 'left', 'right' ] => ( spec => "=i" );

If the name start with plus (+), given parameter updates values.

has '+left' => ( default => 1 );

As for spec parameter, label can be omitted if it is the first parameter.

has left => "=i", default => 1;

If the number of parameter is not even, default label is assumed to be exist at the head: action if the first parameter is code reference, spec otherwise.

Following parameters are available.

Following parameters are all for data validation. First must is a generic validator and can implement anything. Others are shortcut for common rules.

METHOD

new

Class method to get initialized hash object.

optspec

Return option specification list which can be given to GetOptions function.

GetOptions($obj->optspec)

GetOptions has a capability of storing values in a hash, by giving the hash reference as a first argument, but it is not necessary.

getopt [ arrayref ]

Call appropriate function defined in caller's context to process options.

$obj->getopt

$obj->getopt(\@argv);

are shortcut for:

GetOptions($obj->optspec)

GetOptionsFromArray(\@argv, $obj->optspec)

use_keys keys

Because hash keys are protected by Hash::Util::lock_keys, accessing non-existent member causes an error. Use this function to declare new member key before use.

$obj->use_keys( qw(foo bar) );

If you want to access arbitrary keys, unlock the object.

use Hash::Util 'unlock_keys';
unlock_keys %{$obj};

You can change this behavior by configure with LOCK_KEYS parameter.

configure label => value, ...

Use class method Getopt::EX::Hashed->configure() before creating an object; this information is stored in the area unique for calling package. After calling new(), package unique configuration is copied in the object, and it is used for further operation. Use $obj->configure() to update object unique configuration.

There are following configuration parameters.

reset

Reset the class to the original state.

SEE ALSO

Getopt::Long

Getopt::EX, Getopt::EX::Long

AUTHOR

Kazumasa Utashiro

COPYRIGHT

The following copyright notice applies to all the files provided in this distribution, including binary files, unless explicitly noted otherwise.

Copyright 2021-2022 Kazumasa Utashiro

LICENSE

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.