The following commit has been merged in the master branch:
commit edda3a6d24eaa9cd07a4cc451df737a09a39b000
Author: Guillem Jover <[email protected]>
Date:   Sun Jun 23 17:22:18 2013 +0200

    scripts: Trap $SIG{__WARN__} to call usageerr() on option parse errors
    
    This gives a nicer error message, and makes sure we always exit on
    option parse errors.

diff --git a/debian/changelog b/debian/changelog
index 5e00810..ff03170 100644
--- a/debian/changelog
+++ b/debian/changelog
@@ -98,6 +98,8 @@ dpkg (1.17.0) UNRELEASED; urgency=low
   * Clarify that dpkg --set-selections needs an up-to-date available db,
     by documenting it on the dpkg(1) man page, and warning whenever dpkg
     finds unknown packages while setting the selections. Closes: #703092
+  * Print nicer error messages in perl scripts using Getopt::Long by trapping
+    $SIG{__WARN__} to call usageerr() on option parse errors.
 
   [ Updated programs translations ]
   * Fix typo in Spanish translation of update-alternatives.
diff --git a/scripts/changelog/debian.pl b/scripts/changelog/debian.pl
index 35c8412..d1ead9c 100755
--- a/scripts/changelog/debian.pl
+++ b/scripts/changelog/debian.pl
@@ -104,8 +104,10 @@ my @options_spec = (
     'all|a' => \$all,
 );
 
-GetOptions(@options_spec)
-    or do { usage(); exit(2) };
+{
+    local $SIG{__WARN__} = sub { usageerr($_[0]) };
+    GetOptions(@options_spec);
+}
 
 usageerr('too many arguments') if @ARGV > 1;
 
diff --git a/scripts/dpkg-checkbuilddeps.pl b/scripts/dpkg-checkbuilddeps.pl
index 758ecdb..108f034 100755
--- a/scripts/dpkg-checkbuilddeps.pl
+++ b/scripts/dpkg-checkbuilddeps.pl
@@ -76,9 +76,9 @@ my @options_spec = (
     'admindir=s' => \$admindir,
 );
 
-if (!GetOptions(@options_spec)) {
-       usage();
-       exit(2);
+{
+    local $SIG{__WARN__} = sub { usageerr($_[0]) };
+    GetOptions(@options_spec);
 }
 
 my $controlfile = shift || 'debian/control';
diff --git a/scripts/dpkg-mergechangelogs.pl b/scripts/dpkg-mergechangelogs.pl
index 87593e4..fd6530b 100755
--- a/scripts/dpkg-mergechangelogs.pl
+++ b/scripts/dpkg-mergechangelogs.pl
@@ -79,10 +79,11 @@ my @options_spec = (
     'merge-prereleases|m' => \$merge_prereleases,
 );
 
-unless (GetOptions(@options_spec)) {
-    usage();
-    exit(2);
+{
+    local $SIG{__WARN__} = sub { usageerr($_[0]) };
+    GetOptions(@options_spec);
 }
+
 my ($old, $new_a, $new_b, $out_file) = @ARGV;
 unless (defined $old and defined $new_a and defined $new_b and
         -e $old and -e $new_a and -e $new_b)
diff --git a/scripts/dpkg-scanpackages.pl b/scripts/dpkg-scanpackages.pl
index 6b01d48..cf5ab17 100755
--- a/scripts/dpkg-scanpackages.pl
+++ b/scripts/dpkg-scanpackages.pl
@@ -62,8 +62,6 @@ my @options_spec = (
     'medium|M=s',
 );
 
-my $result = GetOptions(\%options, @options_spec);
-
 sub version {
     printf _g("Debian %s version %s.\n"), $Dpkg::PROGNAME, $Dpkg::PROGVERSION;
     exit;
@@ -153,7 +151,10 @@ sub load_override_extra
     close($comp_file);
 }
 
-usage() and exit 1 if not $result;
+{
+    local $SIG{__WARN__} = sub { usageerr($_[0]) };
+    GetOptions(\%options, @options_spec);
+}
 
 if (not @ARGV >= 1 && @ARGV <= 3) {
     usageerr(_g('one to three arguments expected'));
diff --git a/scripts/dpkg-scansources.pl b/scripts/dpkg-scansources.pl
index 6c97c4e..6cc6667 100755
--- a/scripts/dpkg-scansources.pl
+++ b/scripts/dpkg-scansources.pl
@@ -298,7 +298,10 @@ sub process_dsc {
 sub main {
     my (@out);
 
-    GetOptions(@option_spec) or usage;
+    {
+        local $SIG{__WARN__} = sub { usageerr($_[0]) };
+        GetOptions(@option_spec);
+    }
     @ARGV >= 1 && @ARGV <= 3 or usageerr(_g('one to three arguments 
expected'));
 
     push @ARGV, undef          if @ARGV < 2;

-- 
dpkg's main repository


-- 
To UNSUBSCRIBE, email to [email protected]
with a subject of "unsubscribe". Trouble? Contact [email protected]

Reply via email to