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}}, @_;
71 my %options=%{shift()} if ref $_[0];
74 if ($ENV{DH_IGNORE_UNKNOWN_OPTIONS}) {
75 $oldwarn=$SIG{__WARN__};
76 $SIG{__WARN__}=sub {};
79 my $ret=Getopt::Long::GetOptionsFromArray($array,
81 "verbose" => \$dh{VERBOSE},
83 "no-act" => \$dh{NO_ACT},
86 "indep" => \&AddPackage,
89 "arch" => \&AddPackage,
91 "p=s" => \&AddPackage,
92 "package=s" => \&AddPackage,
94 "N=s" => \&ExcludePackage,
95 "no-package=s" => \&ExcludePackage,
97 "remaining-packages" => \$dh{EXCLUDE_LOGGED},
99 "dbg-package=s" => \&AddDebugPackage,
102 "same-arch" => \&AddPackage,
104 "n" => \$dh{NOSCRIPTS},
105 "noscripts" => \$dh{NOSCRIPTS},
106 "o" => \$dh{ONLYSCRIPTS},
107 "onlyscripts" => \$dh{ONLYSCRIPTS},
109 "X=s" => \&AddExclude,
110 "exclude=s" => \&AddExclude,
115 "keep" => \$dh{K_FLAG},
117 "P=s" => \$dh{TMPDIR},
118 "tmpdir=s" => \$dh{TMPDIR},
120 "u=s", => \$dh{U_PARAMS},
122 "V:s", => \$dh{V_FLAG},
124 "A" => \$dh{PARAMS_ALL},
125 "all" => \$dh{PARAMS_ALL},
127 "priority=s" => \$dh{PRIORITY},
129 "h|help" => \&showhelp,
131 "mainpackage=s" => \$dh{MAINPACKAGE},
133 "name=s" => \$dh{NAME},
135 "error-handler=s" => \$dh{ERROR_HANDLER},
137 "ignore=s" => \&AddIgnore,
144 if ($ENV{DH_IGNORE_UNKNOWN_OPTIONS}) {
145 $SIG{__WARN__}=$oldwarn;
153 sub split_options_string {
156 return split(/\s+/,$str);
159 # Parse options and set %dh values.
165 # DH_INTERNAL_OPTIONS is used to pass additional options from
166 # dh through an override target to a command.
167 if (defined $ENV{DH_INTERNAL_OPTIONS}) {
168 @ARGV_extra=split(/\x1e/, $ENV{DH_INTERNAL_OPTIONS});
169 getoptions(\@ARGV_extra, $options);
171 # Avoid forcing acting on packages specified in
172 # DH_INTERNAL_OPTIONS. This way, -p can be specified
173 # at the command line to act on a specific package, and if
174 # nothing is specified, the excludes will cause the set of
175 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
176 if (defined $dh{DOPACKAGES}) {
177 foreach my $package (getpackages()) {
178 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
179 $exclude_package{$package}=1;
183 delete $dh{DOPACKAGES};
188 # DH_OPTIONS can contain additional options
189 # to be parsed like @ARGV, but with unknown options
191 if (defined $ENV{DH_OPTIONS}) {
192 @ARGV_extra=split_options_string($ENV{DH_OPTIONS});
193 my $ret=getoptions(\@ARGV_extra, $options);
195 warning("warning: ignored unknown options in DH_OPTIONS");
199 my $ret=getoptions(\@ARGV, $options);
201 warning("warning: unknown options will be a fatal error in a future debhelper release");
202 #error("unknown option; aborting");
205 # Check to see if -V was specified. If so, but no parameters were
206 # passed, the variable will be defined but empty.
207 if (defined($dh{V_FLAG})) {
211 # If we have not been given any packages to act on, assume they
212 # want us to act on them all. Note we have to do this before excluding
213 # packages out, below.
214 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
215 if ($dh{DOINDEP} || $dh{DOARCH}) {
216 # User specified that all arch (in)dep package be
217 # built, and there are none of that type.
218 warning("You asked that all arch in(dep) packages be built, but there are none of that type.");
221 push @{$dh{DOPACKAGES}},getpackages();
224 # Remove excluded packages from the list of packages to act on.
225 # Also unique the list, in case some options were specified that
226 # added a package to it twice.
230 foreach $package (@{$dh{DOPACKAGES}}) {
231 if (defined($dh{EXCLUDE_LOGGED}) &&
232 grep { $_ eq basename($0) } load_log($package)) {
233 $exclude_package{$package}=1;
235 if (! $exclude_package{$package}) {
236 if (! exists $packages_seen{$package}) {
237 $packages_seen{$package}=1;
238 push @package_list, $package;
242 @{$dh{DOPACKAGES}}=@package_list;
244 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
245 warning("No packages to build.");
249 if (defined $dh{U_PARAMS}) {
250 # Split the U_PARAMS up into an array.
253 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
256 # Anything left in @ARGV is options that appeared after a --
257 # These options are added to the U_PARAMS array, while the
258 # non-option values we collected replace them in @ARGV;
259 push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra;
260 @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
264 # Enable bundling of short command line options.
265 Getopt::Long::config("bundling");