App-Getconf

 view release on metacpan or  search on metacpan

lib/App/Getconf.pm  view on Meta::CPAN


  App::Getconf->options(YAML::LoadFile("/etc/myapp.yaml"));

=cut

sub options {
  my ($self, $options) = @_;

  $self = $static unless ref $self; # static call or non-static?

  $self->set_verify($options);
}

#-----------------------------------------------------------------------------

=item C<cmdline($arguments)>

Set options based on command line arguments (arrayref). If C<$arguments> was
not specified, C<@ARGV> is used.

Method returns list of messages (single line, no C<\n> at end) for errors that

lib/App/Getconf.pm  view on Meta::CPAN

}

=end Internal

=pod }}}

=cut

#-----------------------------------------------------------------------------

=item C<set_verify($data)>

=item C<set_verify($data, $path)>

Set value(s) with verification against schema. If C<$path> was specified,
options start with this prefix. If values were verified successfully, they are
saved in internal storage.

B<NOTE>: This is a semi-internal API.

=cut

sub set_verify {
  my ($self, $data, $path) = @_;

  $self = $static unless ref $self; # static call or non-static?

  $path ||= "";

  my $datum_type = lc(ref $data) || "scalar";

  if ($datum_type ne 'hash') {
    # this is an option, but there's no corresponding schema node

lib/App/Getconf.pm  view on Meta::CPAN

  # more complex case: data is a hash

  # if no corresponding node in schema, just go deeper
  # if there is corresponding node, but it's not a hash, just go deeper, too
  if (!$self->has_option($path) ||
      $self->option_node($path)->storage() ne 'hash') {
    for my $o (keys %$data) {
      my $new_path = "$path.$o";
      $new_path =~ s/^\.|\.$//g;

      $self->set_verify($data->{$o}, $new_path);
    }

    return;
  }

  # it's sure that option called $path exists and it's storage type is "hash"
  # also, this option's type is hash

  my $node = $self->option_node($path);
  for my $k (keys %$data) {

lib/App/Getconf.pm  view on Meta::CPAN

    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

lib/App/Getconf/Node.pm  view on Meta::CPAN

  # not a supported check type
  my $check_type = ref $self->{check};
  if ($self->{check} &&
      !($check_type eq 'CODE' ||
        $check_type =~ /(^|::)Regexp$/ ||
        $check_type eq 'ARRAY')) {
    croak "Unknown check type: $check_type";
  }

  $self->set($opts{value}) if exists $opts{value};
  $self->{default} = $self->verify($opts{default}) if exists $opts{default};

  return $self;
}

#-----------------------------------------------------------------------------

=item C<uses_arg()>

Method tells whether this option I<accepts> an argument passed in command line
(but it may be still possible not to pass an argument to this option; see

lib/App/Getconf/Node.pm  view on Meta::CPAN

    croak "Option requires an argument, but none was provided";
  }

  if (@_ > 1 && !$self->uses_arg) {
    croak "Option doesn't use an argument, but one was provided";
  }

  if ($self->storage eq 'hash') {
    # TODO: how about an array as the value?
    if (defined $key) {
      $self->{value}{$key} = $self->verify($value);
    } elsif ($value =~ /^(.*?)=(.*)$/) {
      $self->{value}{$1} = $self->verify($2);
    } else {
      croak "For hash option key=value pair must be provided";
    }
    return;
  }

  if (defined $key) {
    croak "Can't store key=value pair in @{[ $self->storage ]} storage";
  }

lib/App/Getconf/Node.pm  view on Meta::CPAN

  if ($self->type eq 'flag') {
    # for flags, just increment the counter
    $self->{value} += 1;
  } elsif ($self->type eq 'bool' && @_ == 1) {
    # if Boolean option with no argument is being set, it means the option
    # value is TRUE
    $self->{value} = 1;
  } elsif (@_ == 1 && exists $self->{default}) {
    $self->{value} = $self->{default};
  } else {
    $self->{value} = $self->verify($value);
  }
}

=item C<get()>

Retrieve value of this option.

=cut

sub get {

lib/App/Getconf/Node.pm  view on Meta::CPAN

=cut

sub enum {
  my ($self) = @_;

  return ref $self->{check} eq 'ARRAY' ? $self->{check} : undef;
}

#-----------------------------------------------------------------------------

=item C<verify($value)>

Check correctness of C<$value> for this option.

Method will C<die()> if the value is incorrect.

For convenience, C<$value> is returned. This way following is possible:

  my $foo = $node->verify($value);

=cut

sub verify {
  my ($self, $value) = @_;

  my $type  = $self->{type};
  my $check = $self->{check};

  eval {
    # convert warnings to errors
    local $SIG{__WARN__} = sub { die $@ };

    if ($type eq 'string') {



( run in 0.306 second using v1.01-cache-2.11-cpan-73692580452 )