App-Getconf
view release on metacpan or search on metacpan
lib/App/Getconf.pm view on Meta::CPAN
tie my %h, 'Tie::IxHash';
%h = @args;
return \%h;
}
#-----------------------------------------------------------------------------
=item C<opt($data)>
Generic option specification.
Possible data:
opt {
type => 'flag' | 'bool' | 'int' | 'float' | 'string',
check => qr// | sub {} | ["enum", "value", ...],
storage => undef | \$foo | [] | {},
help => "message displayed on --help",
value => "initial value",
default => "default value",
}
If type is not specified, the option is treated as a string.
Check is for verifying correctness of specified option. It may be a regexp,
callback function (it gets the value to check as a first argument and in C<$_>
variable) or list of possible string values.
Types of options:
=over
=item C<flag>
Simple option, like I<--help> or I<--version>. Flag's value tells how many
times it was encountered.
=item C<bool>
ON/OFF option. May be turned on (I<--verbose>) or off (I<--no-verbose>).
=item C<int>
Option containing an integer.
=item C<float>
Option containing a floating point number.
=item C<string>
Option containing a string. This is the default.
=back
Storage tells if the option is a single-value (default), multi-value
accumulator (e.g. may be specified in command line multiple times, and the
option arguments will be stored in an array) or multi-value hash accumulator
(similar, but option argument is specified as C<key=value>, and the value part
is validated). Note that this specify only type of storage, not the actual
container.
B<NOTE>: Don't specify option with a hash storage and that has sub-options
(see L</"Schema Definition">). Verification can't tell whether the value is
meant for the hash under this option or for one of its sub-options.
Presence of C<help> key indicates that this option should be exposed to
end-users in I<--help> message. Options lacking this key will be skipped (but
stil honoured by App::Getconf).
Except for flags (I<--help>) and bool (I<--no-verbose>) options, the rest of
types require an argument. It may be specified as I<--timeout=120> or as
I<--timeout 120>. This requirement may be loosened by providing
C<default> value. This way end-user may just provide I<--timeout> option, and
the argument to the option is taken from default. (Of course, only
I<--timeout=120> form is supported if the argument needs to be provided.)
Initial value (C<value> key) is the value set for the option just after
defining schema. It may or may not be changed with command line options (which
is different from C<default>, for which the option still needs to be
specified).
Initial and default values are both subject to check that was specified, if
any.
Help message will not retain any formatting, all whitespaces are converted to
single space (empty lines are squeezed to single empty line). On the other
hand, the message will be pretty wrapped and indented, while you don't need to
worry about formatting the string if it is longer and broken to separate lines
in your source code, so I think it's a good trade-off.
=cut
sub opt($) {
my ($data) = @_;
my $type = $data->{type} || "string";
my $check = $data->{check};
my $storage = $data->{storage};
my $help = $data->{help};
my $value = $data->{value}; # not necessary, but kept for convention
my $default = $data->{default}; # not necessary, but kept for convention
if (ref $storage) {
# make sure the store is not a reference to something outside of this
# function
if (ref $storage eq 'ARRAY') {
$storage = 'array';
} elsif (ref $storage eq 'HASH') {
$storage = 'hash';
} elsif (ref $storage eq 'SCALAR') {
$storage = 'scalar';
} # TODO: else die?
} else {
$storage = 'scalar';
}
return new App::Getconf::Node(
type => $type,
check => $check,
( run in 0.695 second using v1.01-cache-2.11-cpan-39bf76dae61 )