3 # Debhelper option processing library.
5 # Joey Hess GPL copyright 1998-2002
7 package Debian::Debhelper::Dh_Getopt;
10 use Debian::Debhelper::Dh_Lib;
13 #use vars qw{@ISA @EXPORT};
15 #@EXPORT=qw(&aparseopts); # FIXME: for some reason, this doesn't work.
17 my (%options, %exclude_package);
20 my $prog=basename($0);
21 print "Usage: $prog [options]\n\n";
22 print " $prog is a part of debhelper. See debhelper(7)\n";
23 print " and $prog(1) for complete usage instructions.\n";
27 # Passed an option name and an option value, adds packages to the list
28 # of packages. We need this so the list will be built up in the right
30 sub AddPackage { my($option,$value)=@_;
31 if ($option eq 'i' or $option eq 'indep') {
32 push @{$options{DOPACKAGES}}, getpackages('indep');
35 elsif ($option eq 'a' or $option eq 'arch') {
36 push @{$options{DOPACKAGES}}, getpackages('arch');
39 elsif ($option eq 'p' or $option eq 'package') {
40 push @{$options{DOPACKAGES}}, $value;
42 elsif ($option eq 's' or $option eq 'same-arch') {
43 push @{$options{DOPACKAGES}}, getpackages('same');
47 error("bad option $option - should never happen!\n");
51 # Adds packages to the list of debug packages.
52 sub AddDebugPackage { my($option,$value)=@_;
53 push @{$options{DEBUGPACKAGES}}, $value;
56 # Add a package to a list of packages that should not be acted on.
57 sub ExcludePackage { my($option,$value)=@_;
58 $exclude_package{$value}=1;
61 # Add another item to the exclude list.
62 sub AddExclude { my($option,$value)=@_;
63 push @{$options{EXCLUDE}},$value;
66 # Add a file to the ignore list.
67 sub AddIgnore { my($option,$file)=@_;
68 $options{IGNORE}->{$file}=1;
71 # Add an item to the with list.
72 sub AddWith { my($option,$value)=@_;
73 push @{$options{WITH}},$value;
76 # This collects non-options values.
78 push @{$options{ARGV}}, @_;
81 # Parse options and return a hash of the values.
85 # DH_INTERNAL_OPTIONS is used to pass additional options from
86 # dh through an override target to a command.
87 if (defined $ENV{DH_INTERNAL_OPTIONS}) {
88 $ENV{DH_INTERNAL_OPTIONS}=~s/^\s+//;
89 $ENV{DH_INTERNAL_OPTIONS}=~s/\s+$//;
90 unshift @ARGV, split(/\s+/,$ENV{DH_INTERNAL_OPTIONS});
94 "v" => \$options{VERBOSE},
95 "verbose" => \$options{VERBOSE},
98 "indep" => \&AddPackage,
101 "arch" => \&AddPackage,
103 "p=s" => \&AddPackage,
104 "package=s" => \&AddPackage,
106 "dbg-package=s" => \&AddDebugPackage,
109 "same-arch" => \&AddPackage,
111 "N=s" => \&ExcludePackage,
112 "no-package=s" => \&ExcludePackage,
114 "n" => \$options{NOSCRIPTS},
115 "noscripts" => \$options{NOSCRIPTS},
116 "o" => \$options{ONLYSCRIPTS},
117 "onlyscripts" => \$options{ONLYSCRIPTS},
119 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
120 "include-conffiles" => \$options{INCLUDE_CONFFILES},
122 "X=s" => \&AddExclude,
123 "exclude=s" => \&AddExclude,
125 "ignore=s" => \&AddIgnore,
127 "d" => \$options{D_FLAG},
128 "remove-d" => \$options{D_FLAG},
129 "dirs-only" => \$options{D_FLAG},
131 "r" => \$options{R_FLAG},
132 "no-restart-on-upgrade" => \$options{R_FLAG},
133 "no-start" => \$options{NO_START},
134 "R|restart-after-upgrade" => \$options{RESTART_AFTER_UPGRADE},
136 "k" => \$options{K_FLAG},
137 "keep" => \$options{K_FLAG},
138 "keep-debug" => \$options{K_FLAG},
140 "P=s" => \$options{TMPDIR},
141 "tmpdir=s" => \$options{TMPDIR},
143 "u=s", => \$options{U_PARAMS},
144 "update-rcd-params=s", => \$options{U_PARAMS},
145 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
146 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
148 "l=s", => \$options{L_PARAMS},
150 "m=s", => \$options{M_PARAMS},
151 "major=s" => \$options{M_PARAMS},
153 "V:s", => \$options{V_FLAG},
154 "version-info:s" => \$options{V_FLAG},
156 "A" => \$options{PARAMS_ALL},
157 "all" => \$options{PARAMS_ALL},
159 "no-act" => \$options{NO_ACT},
161 "init-script=s" => \$options{INIT_SCRIPT},
163 "sourcedir=s" => \$options{SOURCEDIR},
165 "destdir=s" => \$options{DESTDIR},
167 "filename=s" => \$options{FILENAME},
169 "priority=s" => \$options{PRIORITY},
171 "flavor=s" => \$options{FLAVOR},
173 "autodest" => \$options{AUTODEST},
175 "h|help" => \&showhelp,
177 "mainpackage=s" => \$options{MAINPACKAGE},
179 "list-missing" => \$options{LIST_MISSING},
181 "fail-missing" => \$options{FAIL_MISSING},
183 "L|libpackage=s" => \$options{LIBPACKAGE},
185 "name=s" => \$options{NAME},
187 "error-handler=s" => \$options{ERROR_HANDLER},
189 "add-udeb=s" => \$options{SHLIBS_UDEB},
191 "language=s" => \$options{LANGUAGE},
193 "until=s" => \$options{UNTIL},
194 "after=s" => \$options{AFTER},
195 "before=s" => \$options{BEFORE},
196 "remaining" => \$options{REMAINING},
197 "with=s" => \&AddWith,
203 error("unknown option; aborting");
206 # Check to see if -V was specified. If so, but no parameters were
207 # passed, the variable will be defined but empty.
208 if (defined($options{V_FLAG})) {
209 $options{V_FLAG_SET}=1;
212 # If we have not been given any packages to act on, assume they
213 # want us to act on them all. Note we have to do this before excluding
214 # packages out, below.
215 if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
216 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
217 # User specified that all arch (in)dep package be
218 # built, and there are none of that type.
219 warning("I have no package to build");
222 push @{$options{DOPACKAGES}},getpackages();
225 # Remove excluded packages from the list of packages to act on.
226 # Also unique the list, in case some options were specified that
227 # added a package to it twice.
231 foreach $package (@{$options{DOPACKAGES}}) {
232 if (! $exclude_package{$package}) {
233 if (! exists $packages_seen{$package}) {
234 $packages_seen{$package}=1;
235 push @package_list, $package;
239 @{$options{DOPACKAGES}}=@package_list;
241 # If there are no packages to act on now, it's an error.
242 if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
243 error("I have no package to build");
246 if (defined $options{U_PARAMS}) {
247 # Split the U_PARAMS up into an array.
248 my $u=$options{U_PARAMS};
249 undef $options{U_PARAMS};
250 push @{$options{U_PARAMS}}, split(/\s+/,$u);
253 # Anything left in @ARGV is options that appeared after a --
254 # These options are added to the U_PARAMS array, while the
255 # non-option values we collected replace them in @ARGV;
256 push @{$options{U_PARAMS}}, @ARGV;
257 @ARGV=@{$options{ARGV}} if exists $options{ARGV};
263 # Enable bundling of short command line options.
264 Getopt::Long::config("bundling");