From e90eebf73099da05ffb1d1e2be30a2eccafd153c Mon Sep 17 00:00:00 2001 From: Joey Hess Date: Sun, 14 Dec 2008 22:13:32 -0500 Subject: [PATCH] Ignore unknown options in DH_OPTIONS. Debhelper will always ignore such options, even when unknown command-line options are converted back to an error. This allows (ab)using DH_OPTIONS to pass command-specific options. (Note that getopt will warn about such unknown options. Eliminating this warning without reimplementing much of Getopt::Long wasn't practical.) --- Debian/Debhelper/Dh_Getopt.pm | 30 +++++++++++++++++++++++++----- Debian/Debhelper/Dh_Lib.pm | 26 ++++++-------------------- debhelper.pod | 11 +++++++---- debian/changelog | 6 ++++++ 4 files changed, 44 insertions(+), 29 deletions(-) diff --git a/Debian/Debhelper/Dh_Getopt.pm b/Debian/Debhelper/Dh_Getopt.pm index cc15ddd..950a37b 100644 --- a/Debian/Debhelper/Dh_Getopt.pm +++ b/Debian/Debhelper/Dh_Getopt.pm @@ -69,11 +69,11 @@ sub NonOption { push @{$dh{ARGV}}, @_; } -# Parse options and set %dh values. -sub parseopts { +sub getoptions { + my $array=shift; my %options=%{shift()} if ref $_[0]; - my $ret=GetOptions( + Getopt::Long::GetOptionsFromArray($array, "v" => \$dh{VERBOSE}, "verbose" => \$dh{VERBOSE}, @@ -138,8 +138,28 @@ sub parseopts { %options, "<>" => \&NonOption, - ); + ) +} + +# Parse options and set %dh values. +sub parseopts { + my $options=shift; + + # DH_OPTIONS can contain additional options + # to be parsed like @ARGV, but with unknown options + # skipped. + my @ARGV_extra; + if (defined $ENV{DH_OPTIONS}) { + $ENV{DH_OPTIONS}=~s/^\s+//; + $ENV{DH_OPTIONS}=~s/\s+$//; + @ARGV_extra=split(/\s+/,$ENV{DH_OPTIONS}); + my $ret=getoptions(\@ARGV_extra, $options); + if (!$ret) { + warning("warning: ignored unknown options in DH_OPTIONS"); + } + } + my $ret=getoptions(\@ARGV, $options); if (!$ret) { warning("warning: unknown options will be a fatal error in a future debhelper release"); #error("unknown option; aborting"); @@ -195,7 +215,7 @@ sub parseopts { # Anything left in @ARGV is options that appeared after a -- # These options are added to the U_PARAMS array, while the # non-option values we collected replace them in @ARGV; - push @{$dh{U_PARAMS}}, @ARGV; + push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra; @ARGV=@{$dh{ARGV}} if exists $dh{ARGV}; } diff --git a/Debian/Debhelper/Dh_Lib.pm b/Debian/Debhelper/Dh_Lib.pm index 82efc5b..4df64c8 100644 --- a/Debian/Debhelper/Dh_Lib.pm +++ b/Debian/Debhelper/Dh_Lib.pm @@ -22,28 +22,14 @@ my $max_compat=7; sub init { my %params=@_; - # If DH_OPTIONS is set, prepend it @ARGV. - if (defined($ENV{DH_OPTIONS})) { - # Ignore leading/trailing whitespace. - $ENV{DH_OPTIONS}=~s/^\s+//; - $ENV{DH_OPTIONS}=~s/\s+$//; - unshift @ARGV,split(/\s+/,$ENV{DH_OPTIONS}); - } - - # Check to see if an argument on the command line starts with a dash. - # if so, we need to pass this off to the resource intensive + # Check to see if an option line starts with a dash, + # or DH_OPTIONS is set. + # If so, we need to pass this off to the resource intensive # Getopt::Long, which I'd prefer to avoid loading at all if possible. - my $parseopt=undef; - my $arg; - foreach $arg (@ARGV) { - if ($arg=~m/^-/) { - $parseopt=1; - last; - } - } - if ($parseopt) { + if ((defined $ENV{DH_OPTIONS} && length $ENV{DH_OPTIONS}) || + grep /^-/, @ARGV) { eval "use Debian::Debhelper::Dh_Getopt"; - error($!) if $@; + error($@) if $@; Debian::Debhelper::Dh_Getopt::parseopts($params{options}); } diff --git a/debhelper.pod b/debhelper.pod index 3313d57..8f9bd1d 100644 --- a/debhelper.pod +++ b/debhelper.pod @@ -477,10 +477,13 @@ Set to 1 to enable no-act mode. =item DH_OPTIONS Anything in this variable will be prepended to the command line arguments -of all debhelper commands. This is useful in some situations, for example, -if you need to pass -p to all debhelper commands that will be run. One good -way to set DH_OPTIONS is by using "Target-specific Variable Values" in your -debian/rules file. See the make documentation for details on doing this. +of all debhelper commands. Command-specific options will be ignored by +commands that do not support them. + +This is useful in some situations, for example, if you need to pass -p to +all debhelper commands that will be run. One good way to set DH_OPTIONS is +by using "Target-specific Variable Values" in your debian/rules file. See +the make documentation for details on doing this. =item DH_ALWAYS_EXCLUDE diff --git a/debian/changelog b/debian/changelog index f2cbe2e..8f64ffc 100644 --- a/debian/changelog +++ b/debian/changelog @@ -4,6 +4,12 @@ debhelper (7.1.1) UNRELEASED; urgency=low * Fix some docs that refered to --srcdir rather than --sourcedir. Closes: #504742 * Add Vcs-Browser field. Closes: #507804 + * Ignore unknown options in DH_OPTIONS. Debhelper will always ignore + such options, even when unknown command-line options are converted back + to an error. This allows (ab)using DH_OPTIONS to pass command-specific + options. + (Note that getopt will warn about such unknown options. Eliminating this + warning without reimplementing much of Getopt::Long wasn't practical.) -- Joey Hess Mon, 03 Nov 2008 18:50:03 -0500 -- 2.39.2