3 # Debhelper option processing library.
5 # Joey Hess GPL copyright 1998-2002
7 package Debian::Debhelper::Dh_Getopt;
10 use Debian::Debhelper::Dh_Lib;
16 my $prog=basename($0);
17 print "Usage: $prog [options]\n\n";
18 print " $prog is a part of debhelper. See debhelper(7)\n";
19 print " and $prog(1) for complete usage instructions.\n";
23 # Passed an option name and an option value, adds packages to the list
24 # of packages. We need this so the list will be built up in the right
26 sub AddPackage { my($option,$value)=@_;
27 if ($option eq 'i' or $option eq 'indep') {
28 push @{$dh{DOPACKAGES}}, getpackages('indep');
31 elsif ($option eq 'a' or $option eq 'arch' or
32 $option eq 's' or $option eq 'same-arch') {
33 push @{$dh{DOPACKAGES}}, getpackages('arch');
36 elsif ($option eq 'p' or $option eq 'package') {
37 push @{$dh{DOPACKAGES}}, $value;
40 error("bad option $option - should never happen!\n");
44 # Adds packages to the list of debug packages.
45 sub AddDebugPackage { my($option,$value)=@_;
46 push @{$dh{DEBUGPACKAGES}}, $value;
49 # Add a package to a list of packages that should not be acted on.
50 sub ExcludePackage { my($option,$value)=@_;
51 $exclude_package{$value}=1;
54 # Add another item to the exclude list.
55 sub AddExclude { my($option,$value)=@_;
56 push @{$dh{EXCLUDE}},$value;
59 # Add a file to the ignore list.
60 sub AddIgnore { my($option,$file)=@_;
61 $dh{IGNORE}->{$file}=1;
64 # This collects non-options values.
66 push @{$dh{ARGV}}, @_;
76 "verbose" => \$dh{VERBOSE},
78 "no-act" => \$dh{NO_ACT},
81 "indep" => \&AddPackage,
84 "arch" => \&AddPackage,
86 "p=s" => \&AddPackage,
87 "package=s" => \&AddPackage,
89 "N=s" => \&ExcludePackage,
90 "no-package=s" => \&ExcludePackage,
92 "remaining-packages" => \$dh{EXCLUDE_LOGGED},
94 "dbg-package=s" => \&AddDebugPackage,
97 "same-arch" => \&AddPackage,
99 "n" => \$dh{NOSCRIPTS},
100 "noscripts" => \$dh{NOSCRIPTS},
101 "o" => \$dh{ONLYSCRIPTS},
102 "onlyscripts" => \$dh{ONLYSCRIPTS},
104 "X=s" => \&AddExclude,
105 "exclude=s" => \&AddExclude,
110 "keep" => \$dh{K_FLAG},
112 "P=s" => \$dh{TMPDIR},
113 "tmpdir=s" => \$dh{TMPDIR},
115 "u=s", => \$dh{U_PARAMS},
117 "V:s", => \$dh{V_FLAG},
119 "A" => \$dh{PARAMS_ALL},
120 "all" => \$dh{PARAMS_ALL},
122 "priority=s" => \$dh{PRIORITY},
124 "h|help" => \&showhelp,
126 "mainpackage=s" => \$dh{MAINPACKAGE},
128 "name=s" => \$dh{NAME},
130 "error-handler=s" => \$dh{ERROR_HANDLER},
132 "ignore=s" => \&AddIgnore,
134 "O=s" => sub { push @test, $_[1] },
136 (ref $params{options} ? %{$params{options}} : ()) ,
142 foreach my $key (keys %options) {
143 $options{$key}=sub {};
148 if ($params{test} || $params{ignore_unknown_options}) {
149 $oldwarn=$SIG{__WARN__};
150 $SIG{__WARN__}=sub {};
152 my $ret=Getopt::Long::GetOptionsFromArray($array, %options);
154 $SIG{__WARN__}=$oldwarn;
157 foreach my $opt (@test) {
158 # Try to parse an option, and skip it
159 # if it is not known.
160 if (getoptions([$opt], %params,
161 ignore_unknown_options => 0,
163 getoptions([$opt], %params);
167 return 1 if $params{ignore_unknown_options};
171 sub split_options_string {
174 return split(/\s+/,$str);
177 # Parse options and set %dh values.
183 # DH_INTERNAL_OPTIONS is used to pass additional options from
184 # dh through an override target to a command.
185 if (defined $ENV{DH_INTERNAL_OPTIONS}) {
186 @ARGV_extra=split(/\x1e/, $ENV{DH_INTERNAL_OPTIONS});
187 getoptions(\@ARGV_extra, %params);
189 # Avoid forcing acting on packages specified in
190 # DH_INTERNAL_OPTIONS. This way, -p can be specified
191 # at the command line to act on a specific package, but when
192 # nothing is specified, the excludes will cause the set of
193 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
194 if (defined $dh{DOPACKAGES}) {
195 foreach my $package (getpackages()) {
196 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
197 $exclude_package{$package}=1;
201 delete $dh{DOPACKAGES};
206 # DH_OPTIONS can contain additional options to be parsed like @ARGV
207 if (defined $ENV{DH_OPTIONS}) {
208 @ARGV_extra=split_options_string($ENV{DH_OPTIONS});
209 my $ret=getoptions(\@ARGV_extra, %params);
211 warning("warning: ignored unknown options in DH_OPTIONS");
215 my $ret=getoptions(\@ARGV, %params);
217 warning("warning: unknown options will be a fatal error in a future debhelper release");
218 #error("unknown option; aborting");
221 # Check to see if -V was specified. If so, but no parameters were
222 # passed, the variable will be defined but empty.
223 if (defined($dh{V_FLAG})) {
227 # If we have not been given any packages to act on, assume they
228 # want us to act on them all. Note we have to do this before excluding
229 # packages out, below.
230 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
231 if ($dh{DOINDEP} || $dh{DOARCH}) {
232 # User specified that all arch (in)dep package be
233 # built, and there are none of that type.
234 warning("You asked that all arch in(dep) packages be built, but there are none of that type.");
237 push @{$dh{DOPACKAGES}},getpackages();
240 # Remove excluded packages from the list of packages to act on.
241 # Also unique the list, in case some options were specified that
242 # added a package to it twice.
246 foreach $package (@{$dh{DOPACKAGES}}) {
247 if (defined($dh{EXCLUDE_LOGGED}) &&
248 grep { $_ eq basename($0) } load_log($package)) {
249 $exclude_package{$package}=1;
251 if (! $exclude_package{$package}) {
252 if (! exists $packages_seen{$package}) {
253 $packages_seen{$package}=1;
254 push @package_list, $package;
258 @{$dh{DOPACKAGES}}=@package_list;
260 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
261 warning("No packages to build.");
265 if (defined $dh{U_PARAMS}) {
266 # Split the U_PARAMS up into an array.
269 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
272 # Anything left in @ARGV is options that appeared after a --
273 # These options are added to the U_PARAMS array, while the
274 # non-option values we collected replace them in @ARGV;
275 push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra;
276 @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
280 # Enable bundling of short command line options.
281 Getopt::Long::config("bundling");