Просмотр исходного кода

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.
Guillem Jover лет назад: 13
Родитель
Сommit
edda3a6d24

+ 2 - 0
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.

+ 4 - 2
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;
 

+ 3 - 3
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';

+ 4 - 3
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)

+ 4 - 3
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'));

+ 4 - 1
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;