diff options
author | Guillem Jover <guillem@debian.org> | 2016-03-27 17:30:37 +0200 |
---|---|---|
committer | Guillem Jover <guillem@debian.org> | 2016-04-01 00:58:15 +0200 |
commit | 94e241761c06ab112ec3e899dd9449784928c6c5 (patch) | |
tree | ba8cf5ca10d7f8cb830d630bde8ddc49e51ce408 /scripts/Dpkg/Conf.pm | |
parent | 1fc3455ebaa71ad6e34e7a95d89d4bdd2718fa7a (diff) | |
download | dpkg-94e241761c06ab112ec3e899dd9449784928c6c5.tar.gz |
Dpkg::Conf: Switch implementation to be hash based
Store the options in a hash instead of a list so that we can more easily
retrieve them. And add two accessors and an option to the filter method
to control its behavior.
Diffstat (limited to 'scripts/Dpkg/Conf.pm')
-rw-r--r-- | scripts/Dpkg/Conf.pm | 88 |
1 files changed, 70 insertions, 18 deletions
diff --git a/scripts/Dpkg/Conf.pm b/scripts/Dpkg/Conf.pm index 114777f51..8e49147af 100644 --- a/scripts/Dpkg/Conf.pm +++ b/scripts/Dpkg/Conf.pm @@ -18,7 +18,7 @@ package Dpkg::Conf; use strict; use warnings; -our $VERSION = '1.01'; +our $VERSION = '1.02'; use Dpkg::Gettext; use Dpkg::ErrorHandling; @@ -58,7 +58,7 @@ sub new { my $class = ref($this) || $this; my $self = { - options => [], + options => {}, allow_short => 0, }; foreach my $opt (keys %opts) { @@ -79,7 +79,46 @@ Returns the list of options that can be parsed like @ARGV. sub get_options { my $self = shift; - return @{$self->{options}}; + my @options; + + foreach my $name (sort keys %{$self->{options}}) { + my $value = $self->{options}->{$name}; + + $name = "--$name" unless $name =~ /^-/; + if (defined $value) { + push @options, "$name=$value"; + } else { + push @options, $name; + } + } + + return @options; +} + +=item $value = $conf->get($name) + +Returns the value of option $name, where the option name should not have "--" +prefixed. If the option is not present the function will return undef. + +=cut + +sub get { + my ($self, $name) = @_; + + return $self->{options}->{$name}; +} + +=item $conf->set($name, $value) + +Set the $value of option $name, where the option name should not have "--" +prefixed. + +=cut + +sub set { + my ($self, $name, $value) = @_; + + $self->{options}->{$name} = $value; } =item $conf->load($file) @@ -111,13 +150,11 @@ sub parse { } if (/^([^=]+)(?:=(.*))?$/) { my ($name, $value) = ($1, $2); - $name = "--$name" unless $name =~ /^-/; if (defined $value) { $value =~ s/^"(.*)"$/$1/ or $value =~ s/^'(.*)'$/$1/; - push @{$self->{options}}, "$name=$value"; - } else { - push @{$self->{options}}, $name; } + $name =~ s/^--//; + $self->{options}->{$name} = $value; $count++; } else { warning(g_('invalid syntax for option in %s, line %d'), $desc, $.); @@ -126,12 +163,12 @@ sub parse { return $count; } -=item $conf->filter(remove => $rmfunc) - -=item $conf->filter(keep => $keepfunc) +=item $conf->filter(%opts) Filter the list of options, either removing or keeping all those that -return true when &$rmfunc($option) or &keepfunc($option) is called. +return true when &$opts{remove}($option) or &opts{keep}($option) is called. +If $opts{format_argv} is true the long options passed to the filter +functions will have "--" prefixed. =cut @@ -140,8 +177,16 @@ sub filter { my $remove = $opts{remove} // sub { 0 }; my $keep = $opts{keep} // sub { 1 }; - @{$self->{options}} = grep { not &$remove($_) and &$keep($_) } - @{$self->{options}}; + foreach my $name (keys %{$self->{options}}) { + my $option = $name; + + if ($opts{format_argv}) { + $option = "--$name" unless $name =~ /^-/; + } + if (&$remove($option) or not &$keep($option)) { + delete $self->{options}->{$name}; + } + } } =item $string = $conf->output($fh) @@ -162,11 +207,12 @@ Save the options in a file. sub output { my ($self, $fh) = @_; my $ret = ''; - foreach my $opt ($self->get_options()) { - $opt =~ s/^--//; - if ($opt =~ s/^([^=]+)=/$1 = "/) { - $opt .= '"'; - } + + foreach my $name (sort keys %{$self->{options}}) { + my $value = $self->{options}->{$name}; + + my $opt = $name; + $opt .= " = \"$value\"" if defined $value; $opt .= "\n"; print { $fh } $opt if defined $fh; $ret .= $opt; @@ -178,6 +224,12 @@ sub output { =head1 CHANGES +=head2 Version 1.02 (dpkg 1.18.5) + +New option: Accept new option 'format_argv' in $conf->filter(). + +New methods: $conf->get(), $conf->set(). + =head2 Version 1.01 (dpkg 1.15.8) New method: $conf->filter() |