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}}, @_;
72 my %options=%{$params{options}} if ref $params{options};
75 if ($params{ignore_unknown_options}) {
76 $oldwarn=$SIG{__WARN__};
77 $SIG{__WARN__}=sub {};
80 my $ret=Getopt::Long::GetOptionsFromArray($array,
82 "verbose" => \$dh{VERBOSE},
84 "no-act" => \$dh{NO_ACT},
87 "indep" => \&AddPackage,
90 "arch" => \&AddPackage,
92 "p=s" => \&AddPackage,
93 "package=s" => \&AddPackage,
95 "N=s" => \&ExcludePackage,
96 "no-package=s" => \&ExcludePackage,
98 "remaining-packages" => \$dh{EXCLUDE_LOGGED},
100 "dbg-package=s" => \&AddDebugPackage,
103 "same-arch" => \&AddPackage,
105 "n" => \$dh{NOSCRIPTS},
106 "noscripts" => \$dh{NOSCRIPTS},
107 "o" => \$dh{ONLYSCRIPTS},
108 "onlyscripts" => \$dh{ONLYSCRIPTS},
110 "X=s" => \&AddExclude,
111 "exclude=s" => \&AddExclude,
116 "keep" => \$dh{K_FLAG},
118 "P=s" => \$dh{TMPDIR},
119 "tmpdir=s" => \$dh{TMPDIR},
121 "u=s", => \$dh{U_PARAMS},
123 "V:s", => \$dh{V_FLAG},
125 "A" => \$dh{PARAMS_ALL},
126 "all" => \$dh{PARAMS_ALL},
128 "priority=s" => \$dh{PRIORITY},
130 "h|help" => \&showhelp,
132 "mainpackage=s" => \$dh{MAINPACKAGE},
134 "name=s" => \$dh{NAME},
136 "error-handler=s" => \$dh{ERROR_HANDLER},
138 "ignore=s" => \&AddIgnore,
145 if ($params{ignore_unknown_options}) {
146 $SIG{__WARN__}=$oldwarn;
154 sub split_options_string {
157 return split(/\s+/,$str);
160 # Parse options and set %dh values.
166 # DH_INTERNAL_OPTIONS is used to pass additional options from
167 # dh through an override target to a command.
168 if (defined $ENV{DH_INTERNAL_OPTIONS}) {
169 @ARGV_extra=split(/\x1e/, $ENV{DH_INTERNAL_OPTIONS});
170 getoptions(\@ARGV_extra, %params, ignore_unknown_options => 1);
172 # Avoid forcing acting on packages specified in
173 # DH_INTERNAL_OPTIONS. This way, -p can be specified
174 # at the command line to act on a specific package, and if
175 # nothing is specified, the excludes will cause the set of
176 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
177 if (defined $dh{DOPACKAGES}) {
178 foreach my $package (getpackages()) {
179 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
180 $exclude_package{$package}=1;
184 delete $dh{DOPACKAGES};
189 # DH_OPTIONS can contain additional options to be parsed like @ARGV
190 if (defined $ENV{DH_OPTIONS}) {
191 @ARGV_extra=split_options_string($ENV{DH_OPTIONS});
192 my $ret=getoptions(\@ARGV_extra, %params);
194 warning("warning: ignored unknown options in DH_OPTIONS");
198 my $ret=getoptions(\@ARGV, %params);
200 warning("warning: unknown options will be a fatal error in a future debhelper release");
201 #error("unknown option; aborting");
204 # Check to see if -V was specified. If so, but no parameters were
205 # passed, the variable will be defined but empty.
206 if (defined($dh{V_FLAG})) {
210 # If we have not been given any packages to act on, assume they
211 # want us to act on them all. Note we have to do this before excluding
212 # packages out, below.
213 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
214 if ($dh{DOINDEP} || $dh{DOARCH}) {
215 # User specified that all arch (in)dep package be
216 # built, and there are none of that type.
217 warning("You asked that all arch in(dep) packages be built, but there are none of that type.");
220 push @{$dh{DOPACKAGES}},getpackages();
223 # Remove excluded packages from the list of packages to act on.
224 # Also unique the list, in case some options were specified that
225 # added a package to it twice.
229 foreach $package (@{$dh{DOPACKAGES}}) {
230 if (defined($dh{EXCLUDE_LOGGED}) &&
231 grep { $_ eq basename($0) } load_log($package)) {
232 $exclude_package{$package}=1;
234 if (! $exclude_package{$package}) {
235 if (! exists $packages_seen{$package}) {
236 $packages_seen{$package}=1;
237 push @package_list, $package;
241 @{$dh{DOPACKAGES}}=@package_list;
243 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
244 warning("No packages to build.");
248 if (defined $dh{U_PARAMS}) {
249 # Split the U_PARAMS up into an array.
252 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
255 # Anything left in @ARGV is options that appeared after a --
256 # These options are added to the U_PARAMS array, while the
257 # non-option values we collected replace them in @ARGV;
258 push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra;
259 @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
263 # Enable bundling of short command line options.
264 Getopt::Long::config("bundling");