Преглед изворни кода

Dpkg::Getopt: New private module

Guillem Jover пре 11 година
родитељ
комит
6f1097093f
5 измењених фајлова са 84 додато и 0 уклоњено
  1. 1 0
      debian/changelog
  2. 43 0
      scripts/Dpkg/Getopt.pm
  3. 2 0
      scripts/Makefile.am
  4. 1 0
      scripts/po/POTFILES.in
  5. 37 0
      scripts/t/Dpkg_Getopt.t

+ 1 - 0
debian/changelog

@@ -20,6 +20,7 @@ dpkg (1.17.17) UNRELEASED; urgency=low
     - Allow multiple whitespace in Dpkg::BuildProfiles::get_build_profiles()
       when parsing the DEB_BUILD_PROFILES environment variable.
       Thanks to Johannes Schauer <j.schauer@email.de>.
+    - New Dpkg::Getopt private module.
   * Set the DEB_TARGET_* dpkg-architecture variables from architecture.mk.
     Missed in dpkg 1.17.14.
 

+ 43 - 0
scripts/Dpkg/Getopt.pm

@@ -0,0 +1,43 @@
+# Copyright © 2014 Guillem Jover <guillem@debian.org>
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+package Dpkg::Getopt;
+
+use strict;
+use warnings;
+
+our $VERSION = '0.01';
+our @EXPORT = qw(normalize_options);
+
+use Exporter qw(import);
+
+sub normalize_options
+{
+    my (@args) = @_;
+
+    @args = map {
+        if (m/^(-[A-Za-z])(.+)$/) {
+            ($1, $2)
+        } elsif (m/^(--[A-Za-z-]+)=(.*)$/) {
+            ($1, $2)
+        } else {
+            $_;
+        }
+    } @args;
+
+    return @args;
+}
+
+1;

+ 2 - 0
scripts/Makefile.am

@@ -82,6 +82,7 @@ nobase_dist_perllib_DATA = \
 	Dpkg/ErrorHandling.pm \
 	Dpkg/Exit.pm \
 	Dpkg/File.pm \
+	Dpkg/Getopt.pm \
 	Dpkg/Gettext.pm \
 	Dpkg/Index.pm \
 	Dpkg/Interface/Storable.pm \
@@ -202,6 +203,7 @@ test_scripts = \
 	t/Dpkg_ErrorHandling.t \
 	t/Dpkg_Exit.t \
 	t/Dpkg_File.t \
+	t/Dpkg_Getopt.t \
 	t/Dpkg_Gettext.t \
 	t/Dpkg_Conf.t \
 	t/Dpkg_Deps.t \

+ 1 - 0
scripts/po/POTFILES.in

@@ -44,6 +44,7 @@ scripts/Dpkg/Dist/Files.pm
 scripts/Dpkg/ErrorHandling.pm
 scripts/Dpkg/Exit.pm
 scripts/Dpkg/File.pm
+scripts/Dpkg/Getopt.pm
 scripts/Dpkg/IPC.pm
 scripts/Dpkg/Index.pm
 scripts/Dpkg/Interface/Storable.pm

+ 37 - 0
scripts/t/Dpkg_Getopt.t

@@ -0,0 +1,37 @@
+#!/usr/bin/perl
+#
+# This program is free software; you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation; either version 2 of the License, or
+# (at your option) any later version.
+#
+# This program is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program.  If not, see <https://www.gnu.org/licenses/>.
+
+use strict;
+use warnings;
+
+use Test::More tests => 3;
+
+BEGIN {
+    use_ok('Dpkg::Getopt');
+}
+
+my @expect_argv;
+
+@ARGV = normalize_options(qw(-a -bfoo -c var));
+@expect_argv = qw(-a -b foo -c var);
+is_deeply(\@ARGV, \@expect_argv, 'unbundle short options');
+
+@ARGV = normalize_options(qw(--option-a --option-b value --option-c=value));
+@expect_argv = qw(--option-a --option-b value --option-c value);
+is_deeply(\@ARGV, \@expect_argv, 'unbundle long options');
+
+# TODO: Add actual test cases.
+
+1;