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];
76 Getopt::Long::GetOptionsFromArray($array,
78 "verbose" => \$dh{VERBOSE},
80 "no-act" => \$dh{NO_ACT},
83 "indep" => \&AddPackage,
86 "arch" => \&AddPackage,
88 "p=s" => \&AddPackage,
89 "package=s" => \&AddPackage,
91 "N=s" => \&ExcludePackage,
92 "no-package=s" => \&ExcludePackage,
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 "sourcedir=s" => \$dh{SOURCEDIR},
124 "destdir=s" => \$dh{DESTDIR},
126 "priority=s" => \$dh{PRIORITY},
128 "h|help" => \&showhelp,
130 "mainpackage=s" => \$dh{MAINPACKAGE},
132 "name=s" => \$dh{NAME},
134 "error-handler=s" => \$dh{ERROR_HANDLER},
136 "ignore=s" => \&AddIgnore,
144 # Parse options and set %dh values.
150 # DH_INTERNAL_OPTIONS is used to pass additional options from
151 # dh through an override target to a command.
152 if (defined $ENV{DH_INTERNAL_OPTIONS}) {
153 $ENV{DH_INTERNAL_OPTIONS}=~s/^\s+//;
154 $ENV{DH_INTERNAL_OPTIONS}=~s/\s+$//;
155 @ARGV_extra=split(/\s+/,$ENV{DH_INTERNAL_OPTIONS});
156 my $ret=getoptions(\@ARGV_extra, $options);
158 warning("warning: unknown options will be a fatal error in a future debhelper release");
159 #error("unknown option; aborting");
162 # Avoid forcing acting on packages specified in
163 # DH_INTERNAL_OPTIONS. This way, -p can be specified
164 # at the command line to act on a specific package, and if
165 # nothing is specified, the excludes will cause the set of
166 # packages DH_INTERNAL_OPTIONS specifies to be acted on.
167 foreach my $package (getpackages()) {
168 if (! grep { $_ eq $package } @{$dh{DOPACKAGES}}) {
169 $exclude_package{$package}=1;
172 delete $dh{DOPACKAGES};
178 # DH_OPTIONS can contain additional options
179 # to be parsed like @ARGV, but with unknown options
181 if (defined $ENV{DH_OPTIONS}) {
182 $ENV{DH_OPTIONS}=~s/^\s+//;
183 $ENV{DH_OPTIONS}=~s/\s+$//;
184 @ARGV_extra=split(/\s+/,$ENV{DH_OPTIONS});
185 my $ret=getoptions(\@ARGV_extra, $options);
187 warning("warning: ignored unknown options in DH_OPTIONS");
191 my $ret=getoptions(\@ARGV, $options);
193 warning("warning: unknown options will be a fatal error in a future debhelper release");
194 #error("unknown option; aborting");
197 # Check to see if -V was specified. If so, but no parameters were
198 # passed, the variable will be defined but empty.
199 if (defined($dh{V_FLAG})) {
203 # If we have not been given any packages to act on, assume they
204 # want us to act on them all. Note we have to do this before excluding
205 # packages out, below.
206 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
207 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
208 # User specified that all arch (in)dep package be
209 # built, and there are none of that type.
210 warning("You asked that all arch in(dep) packages be built, but there are none of that type.");
213 push @{$dh{DOPACKAGES}},getpackages();
216 # Remove excluded packages from the list of packages to act on.
217 # Also unique the list, in case some options were specified that
218 # added a package to it twice.
222 foreach $package (@{$dh{DOPACKAGES}}) {
223 if (! $exclude_package{$package}) {
224 if (! exists $packages_seen{$package}) {
225 $packages_seen{$package}=1;
226 push @package_list, $package;
230 @{$dh{DOPACKAGES}}=@package_list;
232 if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
233 warning("No packages to build.");
237 if (defined $dh{U_PARAMS}) {
238 # Split the U_PARAMS up into an array.
241 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
244 # Anything left in @ARGV is options that appeared after a --
245 # These options are added to the U_PARAMS array, while the
246 # non-option values we collected replace them in @ARGV;
247 push @{$dh{U_PARAMS}}, @ARGV, @ARGV_extra;
248 @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
252 # Enable bundling of short command line options.
253 Getopt::Long::config("bundling");