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.
86 "v" => \$options{VERBOSE},
87 "verbose" => \$options{VERBOSE},
90 "indep" => \&AddPackage,
93 "arch" => \&AddPackage,
95 "p=s" => \&AddPackage,
96 "package=s" => \&AddPackage,
98 "dbg-package=s" => \&AddDebugPackage,
101 "same-arch" => \&AddPackage,
103 "N=s" => \&ExcludePackage,
104 "no-package=s" => \&ExcludePackage,
106 "n" => \$options{NOSCRIPTS},
107 "noscripts" => \$options{NOSCRIPTS},
108 "o" => \$options{ONLYSCRIPTS},
109 "onlyscripts" => \$options{ONLYSCRIPTS},
111 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
112 "include-conffiles" => \$options{INCLUDE_CONFFILES},
114 "X=s" => \&AddExclude,
115 "exclude=s" => \&AddExclude,
117 "ignore=s" => \&AddIgnore,
119 "d" => \$options{D_FLAG},
120 "remove-d" => \$options{D_FLAG},
121 "dirs-only" => \$options{D_FLAG},
123 "r" => \$options{R_FLAG},
124 "no-restart-on-upgrade" => \$options{R_FLAG},
125 "no-start" => \$options{NO_START},
126 "R|restart-after-upgrade" => \$options{RESTART_AFTER_UPGRADE},
128 "k" => \$options{K_FLAG},
129 "keep" => \$options{K_FLAG},
130 "keep-debug" => \$options{K_FLAG},
132 "P=s" => \$options{TMPDIR},
133 "tmpdir=s" => \$options{TMPDIR},
135 "u=s", => \$options{U_PARAMS},
136 "update-rcd-params=s", => \$options{U_PARAMS},
137 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
138 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
140 "l=s", => \$options{L_PARAMS},
142 "m=s", => \$options{M_PARAMS},
143 "major=s" => \$options{M_PARAMS},
145 "V:s", => \$options{V_FLAG},
146 "version-info:s" => \$options{V_FLAG},
148 "A" => \$options{PARAMS_ALL},
149 "all" => \$options{PARAMS_ALL},
151 "no-act" => \$options{NO_ACT},
153 "init-script=s" => \$options{INIT_SCRIPT},
155 "sourcedir=s" => \$options{SOURCEDIR},
157 "destdir=s" => \$options{DESTDIR},
159 "filename=s" => \$options{FILENAME},
161 "priority=s" => \$options{PRIORITY},
163 "flavor=s" => \$options{FLAVOR},
165 "autodest" => \$options{AUTODEST},
167 "h|help" => \&showhelp,
169 "mainpackage=s" => \$options{MAINPACKAGE},
171 "list-missing" => \$options{LIST_MISSING},
173 "fail-missing" => \$options{FAIL_MISSING},
175 "L|libpackage=s" => \$options{LIBPACKAGE},
177 "name=s" => \$options{NAME},
179 "error-handler=s" => \$options{ERROR_HANDLER},
181 "add-udeb=s" => \$options{SHLIBS_UDEB},
183 "language=s" => \$options{LANGUAGE},
185 "until=s" => \$options{UNTIL},
186 "after=s" => \$options{AFTER},
187 "before=s" => \$options{BEFORE},
188 "remaining" => \$options{REMAINING},
189 "with=s" => \&AddWith,
195 error("unknown option; aborting");
198 # Check to see if -V was specified. If so, but no parameters were
199 # passed, the variable will be defined but empty.
200 if (defined($options{V_FLAG})) {
201 $options{V_FLAG_SET}=1;
204 # If we have not been given any packages to act on, assume they
205 # want us to act on them all. Note we have to do this before excluding
206 # packages out, below.
207 if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
208 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
209 # User specified that all arch (in)dep package be
210 # built, and there are none of that type.
211 warning("I have no package to build");
214 push @{$options{DOPACKAGES}},getpackages();
217 # Remove excluded packages from the list of packages to act on.
218 # Also unique the list, in case some options were specified that
219 # added a package to it twice.
223 foreach $package (@{$options{DOPACKAGES}}) {
224 if (! $exclude_package{$package}) {
225 if (! exists $packages_seen{$package}) {
226 $packages_seen{$package}=1;
227 push @package_list, $package;
231 @{$options{DOPACKAGES}}=@package_list;
233 # If there are no packages to act on now, it's an error.
234 if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
235 error("I have no package to build");
238 if (defined $options{U_PARAMS}) {
239 # Split the U_PARAMS up into an array.
240 my $u=$options{U_PARAMS};
241 undef $options{U_PARAMS};
242 push @{$options{U_PARAMS}}, split(/\s+/,$u);
245 # Anything left in @ARGV is options that appeared after a --
246 # These options are added to the U_PARAMS array, while the
247 # non-option values we collected replace them in @ARGV;
248 push @{$options{U_PARAMS}}, @ARGV;
249 @ARGV=@{$options{ARGV}} if exists $options{ARGV};
255 # Enable bundling of short command line options.
256 Getopt::Long::config("bundling");