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') {
32 push @{$dh{DOPACKAGES}}, getpackages('arch');
35 elsif ($option eq 'p' or $option eq 'package') {
36 push @{$dh{DOPACKAGES}}, $value;
38 elsif ($option eq 's' or $option eq 'same-arch') {
39 push @{$dh{DOPACKAGES}}, getpackages('same');
43 error("bad option $option - should never happen!\n");
47 # Adds packages to the list of debug packages.
48 sub AddDebugPackage { my($option,$value)=@_;
49 push @{$dh{DEBUGPACKAGES}}, $value;
52 # Add a package to a list of packages that should not be acted on.
53 sub ExcludePackage { my($option,$value)=@_;
54 $exclude_package{$value}=1;
57 # Add another item to the exclude list.
58 sub AddExclude { my($option,$value)=@_;
59 push @{$dh{EXCLUDE}},$value;
62 # Add a file to the ignore list.
63 sub AddIgnore { my($option,$file)=@_;
64 $dh{IGNORE}->{$file}=1;
67 # This collects non-options values.
69 push @{$dh{ARGV}}, @_;
74 my %options=%{shift()} if ref $_[0];
77 if ($ENV{DH_IGNORE_UNKNOWN_OPTIONS}) {
78 $oldwarn=$SIG{__WARN__};
79 $SIG{__WARN__}=sub {};
82 my $ret=Getopt::Long::GetOptionsFromArray($array,
84 "verbose" => \$dh{VERBOSE},
86 "no-act" => \$dh{NO_ACT},
89 "indep" => \&AddPackage,
92 "arch" => \&AddPackage,
94 "p=s" => \&AddPackage,
95 "package=s" => \&AddPackage,
97 "N=s" => \&ExcludePackage,
98 "no-package=s" => \&ExcludePackage,
100 "remaining-packages" => \$dh{EXCLUDE_LOGGED},
102 "dbg-package=s" => \&AddDebugPackage,
105 "same-arch" => \&AddPackage,
107 "n" => \$dh{NOSCRIPTS},
108 "noscripts" => \$dh{NOSCRIPTS},
109 "o" => \$dh{ONLYSCRIPTS},
110 "onlyscripts" => \$dh{ONLYSCRIPTS},
112 "X=s" => \&AddExclude,
113 "exclude=s" => \&AddExclude,
118 "keep" => \$dh{K_FLAG},
120 "P=s" => \$dh{TMPDIR},
121 "tmpdir=s" => \$dh{TMPDIR},
123 "u=s", => \$dh{U_PARAMS},
125 "V:s", => \$dh{V_FLAG},
127 "A" => \$dh{PARAMS_ALL},
128 "all" => \$dh{PARAMS_ALL},
130 "priority=s" => \$dh{PRIORITY},
132 "h|help" => \&showhelp,
134 "mainpackage=s" => \$dh{MAINPACKAGE},
136 "name=s" => \$dh{NAME},
138 "error-handler=s" => \$dh{ERROR_HANDLER},
140 "ignore=s" => \&AddIgnore,
147 if ($ENV{DH_IGNORE_UNKNOWN_OPTIONS}) {
148 $SIG{__WARN__}=$oldwarn;
156 sub split_options_string {
159 return split(/\s+/,$str);
162 # Parse options and set %dh values.
168 # DH_INTERNAL_OPTIONS is used to pass additional options from
169 # dh through an override target to a command.
170 if (defined $ENV{DH_INTERNAL_OPTIONS}) {
171 @ARGV_extra=split_options_string($ENV{DH_INTERNAL_OPTIONS});
172 getoptions(\@ARGV_extra, $options);
174 # Avoid forcing acting on packages specified in
175 # DH_INTERNAL_OPTIONS. This way, -p can be specified
176 # at the command line to act on a specific package, and if
177 # nothing is specified, the excludes will cause the set of
178 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
179 if (defined $dh{DOPACKAGES}) {
180 foreach my $package (getpackages()) {
181 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
182 $exclude_package{$package}=1;
186 delete $dh{DOPACKAGES};
192 # DH_OPTIONS can contain additional options
193 # to be parsed like @ARGV, but with unknown options
195 if (defined $ENV{DH_OPTIONS}) {
196 @ARGV_extra=split_options_string($ENV{DH_OPTIONS});
197 my $ret=getoptions(\@ARGV_extra, $options);
199 warning("warning: ignored unknown options in DH_OPTIONS");
203 my $ret=getoptions(\@ARGV, $options);
205 warning("warning: unknown options will be a fatal error in a future debhelper release");
206 #error("unknown option; aborting");
209 # Check to see if -V was specified. If so, but no parameters were
210 # passed, the variable will be defined but empty.
211 if (defined($dh{V_FLAG})) {
215 # If we have not been given any packages to act on, assume they
216 # want us to act on them all. Note we have to do this before excluding
217 # packages out, below.
218 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
219 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
220 # User specified that all arch (in)dep package be
221 # built, and there are none of that type.
222 warning("You asked that all arch in(dep) packages be built, but there are none of that type.");
225 push @{$dh{DOPACKAGES}},getpackages();
228 # Remove excluded packages from the list of packages to act on.
229 # Also unique the list, in case some options were specified that
230 # added a package to it twice.
234 foreach $package (@{$dh{DOPACKAGES}}) {
235 if (defined($dh{EXCLUDE_LOGGED}) &&
236 grep { $_ eq basename($0) } load_log($package)) {
237 $exclude_package{$package}=1;
239 if (! $exclude_package{$package}) {
240 if (! exists $packages_seen{$package}) {
241 $packages_seen{$package}=1;
242 push @package_list, $package;
246 @{$dh{DOPACKAGES}}=@package_list;
248 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
249 warning("No packages to build.");
253 if (defined $dh{U_PARAMS}) {
254 # Split the U_PARAMS up into an array.
257 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
260 # Anything left in @ARGV is options that appeared after a --
261 # These options are added to the U_PARAMS array, while the
262 # non-option values we collected replace them in @ARGV;
263 push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra;
264 @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
268 # Enable bundling of short command line options.
269 Getopt::Long::config("bundling");