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}}, @_;
75 "verbose" => \$dh{VERBOSE},
77 "no-act" => \$dh{NO_ACT},
80 "indep" => \&AddPackage,
83 "arch" => \&AddPackage,
85 "p=s" => \&AddPackage,
86 "package=s" => \&AddPackage,
88 "N=s" => \&ExcludePackage,
89 "no-package=s" => \&ExcludePackage,
91 "remaining-packages" => \$dh{EXCLUDE_LOGGED},
93 "dbg-package=s" => \&AddDebugPackage,
96 "same-arch" => \&AddPackage,
98 "n" => \$dh{NOSCRIPTS},
99 "noscripts" => \$dh{NOSCRIPTS},
100 "o" => \$dh{ONLYSCRIPTS},
101 "onlyscripts" => \$dh{ONLYSCRIPTS},
103 "X=s" => \&AddExclude,
104 "exclude=s" => \&AddExclude,
109 "keep" => \$dh{K_FLAG},
111 "P=s" => \$dh{TMPDIR},
112 "tmpdir=s" => \$dh{TMPDIR},
114 "u=s", => \$dh{U_PARAMS},
116 "V:s", => \$dh{V_FLAG},
118 "A" => \$dh{PARAMS_ALL},
119 "all" => \$dh{PARAMS_ALL},
121 "priority=s" => \$dh{PRIORITY},
123 "h|help" => \&showhelp,
125 "mainpackage=s" => \$dh{MAINPACKAGE},
127 "name=s" => \$dh{NAME},
129 "error-handler=s" => \$dh{ERROR_HANDLER},
131 "ignore=s" => \&AddIgnore,
133 "O=s" => sub { my($option,$value)=@_;
134 # Try to parse an option, but ignore it
135 # if it is not known.
136 if (getoptions([$value], %params, test => 1)) {
137 getoptions([$value], %params);
141 (ref $params{options} ? %{$params{options}} : ()) ,
147 foreach my $key (keys %options) {
148 $options{$key}=sub {};
153 if ($params{test} || $params{ignore_unknown_options}) {
154 $oldwarn=$SIG{__WARN__};
155 $SIG{__WARN__}=sub {};
157 my $ret=Getopt::Long::GetOptionsFromArray($array, %options);
159 $SIG{__WARN__}=$oldwarn;
162 return 1 if $params{ignore_unknown_options};
166 sub split_options_string {
169 return split(/\s+/,$str);
172 # Parse options and set %dh values.
178 # DH_INTERNAL_OPTIONS is used to pass additional options from
179 # dh through an override target to a command.
180 if (defined $ENV{DH_INTERNAL_OPTIONS}) {
181 @ARGV_extra=split(/\x1e/, $ENV{DH_INTERNAL_OPTIONS});
182 getoptions(\@ARGV_extra, %params, ignore_unknown_options => 1);
184 # Avoid forcing acting on packages specified in
185 # DH_INTERNAL_OPTIONS. This way, -p can be specified
186 # at the command line to act on a specific package, but when
187 # nothing is specified, the excludes will cause the set of
188 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
189 if (defined $dh{DOPACKAGES}) {
190 foreach my $package (getpackages()) {
191 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
192 $exclude_package{$package}=1;
196 delete $dh{DOPACKAGES};
201 # DH_OPTIONS can contain additional options to be parsed like @ARGV
202 if (defined $ENV{DH_OPTIONS}) {
203 @ARGV_extra=split_options_string($ENV{DH_OPTIONS});
204 my $ret=getoptions(\@ARGV_extra, %params);
206 warning("warning: ignored unknown options in DH_OPTIONS");
210 my $ret=getoptions(\@ARGV, %params);
212 warning("warning: unknown options will be a fatal error in a future debhelper release");
213 #error("unknown option; aborting");
216 # Check to see if -V was specified. If so, but no parameters were
217 # passed, the variable will be defined but empty.
218 if (defined($dh{V_FLAG})) {
222 # If we have not been given any packages to act on, assume they
223 # want us to act on them all. Note we have to do this before excluding
224 # packages out, below.
225 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
226 if ($dh{DOINDEP} || $dh{DOARCH}) {
227 # User specified that all arch (in)dep package be
228 # built, and there are none of that type.
229 warning("You asked that all arch in(dep) packages be built, but there are none of that type.");
232 push @{$dh{DOPACKAGES}},getpackages();
235 # Remove excluded packages from the list of packages to act on.
236 # Also unique the list, in case some options were specified that
237 # added a package to it twice.
241 foreach $package (@{$dh{DOPACKAGES}}) {
242 if (defined($dh{EXCLUDE_LOGGED}) &&
243 grep { $_ eq basename($0) } load_log($package)) {
244 $exclude_package{$package}=1;
246 if (! $exclude_package{$package}) {
247 if (! exists $packages_seen{$package}) {
248 $packages_seen{$package}=1;
249 push @package_list, $package;
253 @{$dh{DOPACKAGES}}=@package_list;
255 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
256 warning("No packages to build.");
260 if (defined $dh{U_PARAMS}) {
261 # Split the U_PARAMS up into an array.
264 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
267 # Anything left in @ARGV is options that appeared after a --
268 # These options are added to the U_PARAMS array, while the
269 # non-option values we collected replace them in @ARGV;
270 push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra;
271 @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
275 # Enable bundling of short command line options.
276 Getopt::Long::config("bundling");