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 # This collects non-options values.
68 push @{$options{ARGV}}, @_;
71 # Parse options and return a hash of the values.
76 "v" => \$options{VERBOSE},
77 "verbose" => \$options{VERBOSE},
80 "indep" => \&AddPackage,
83 "arch" => \&AddPackage,
85 "p=s" => \&AddPackage,
86 "package=s" => \&AddPackage,
88 "dbg-package=s" => \&AddDebugPackage,
91 "same-arch" => \&AddPackage,
93 "N=s" => \&ExcludePackage,
94 "no-package=s" => \&ExcludePackage,
96 "n" => \$options{NOSCRIPTS},
97 "noscripts" => \$options{NOSCRIPTS},
99 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
100 "include-conffiles" => \$options{INCLUDE_CONFFILES},
102 "X=s" => \&AddExclude,
103 "exclude=s" => \&AddExclude,
105 "d" => \$options{D_FLAG},
106 "remove-d" => \$options{D_FLAG},
107 "dirs-only" => \$options{D_FLAG},
109 "r" => \$options{R_FLAG},
110 "no-restart-on-upgrade" => \$options{R_FLAG},
111 "no-start" => \$options{NO_START},
113 "k" => \$options{K_FLAG},
114 "keep" => \$options{K_FLAG},
116 "P=s" => \$options{TMPDIR},
117 "tmpdir=s" => \$options{TMPDIR},
119 "u=s", => \$options{U_PARAMS},
120 "update-rcd-params=s", => \$options{U_PARAMS},
121 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
122 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
124 "l=s", => \$options{L_PARAMS},
126 "m=s", => \$options{M_PARAMS},
127 "major=s" => \$options{M_PARAMS},
129 "V:s", => \$options{V_FLAG},
130 "version-info:s" => \$options{V_FLAG},
132 "A" => \$options{PARAMS_ALL},
133 "all" => \$options{PARAMS_ALL},
135 "no-act" => \$options{NO_ACT},
137 "init-script=s" => \$options{INIT_SCRIPT},
139 "sourcedir=s" => \$options{SOURCEDIR},
141 "destdir=s" => \$options{DESTDIR},
143 "filename=s" => \$options{FILENAME},
145 "priority=i" => \$options{PRIORITY},
147 "flavor=s" => \$options{FLAVOR},
149 "autodest" => \$options{AUTODEST},
151 "h|help" => \&showhelp,
153 "mainpackage=s" => \$options{MAINPACKAGE},
155 "list-missing" => \$options{LIST_MISSING},
157 "fail-missing" => \$options{FAIL_MISSING},
159 "L|libpackage=s" => \$options{LIBPACKAGE},
161 "name=s" => \$options{NAME},
163 "keep-debug" => \$options{KEEP_DEBUG},
165 "error-handler=s" => \$options{ERROR_HANDLER},
171 error("unknown option; aborting");
174 # Check to see if -V was specified. If so, but no parameters were
175 # passed, the variable will be defined but empty.
176 if (defined($options{V_FLAG})) {
177 $options{V_FLAG_SET}=1;
180 # If we have not been given any packages to act on, assume they
181 # want us to act on them all. Note we have to do this before excluding
182 # packages out, below.
183 if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
184 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
185 # User specified that all arch (in)dep package be
186 # built, and there are none of that type.
187 error("I have no package to build");
189 push @{$options{DOPACKAGES}},getpackages();
192 # Remove excluded packages from the list of packages to act on.
193 # Also unique the list, in case some options were specified that
194 # added a package to it twice.
198 foreach $package (@{$options{DOPACKAGES}}) {
199 if (! $exclude_package{$package}) {
200 if (! exists $packages_seen{$package}) {
201 $packages_seen{$package}=1;
202 push @package_list, $package;
206 @{$options{DOPACKAGES}}=@package_list;
208 # If there are no packages to act on now, it's an error.
209 if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
210 error("I have no package to build");
213 if (defined $options{U_PARAMS}) {
214 # Split the U_PARAMS up into an array.
215 my $u=$options{U_PARAMS};
216 undef $options{U_PARAMS};
217 push @{$options{U_PARAMS}}, split(/\s+/,$u);
220 # Anything left in @ARGV is options that appeared after a --
221 # These options are added to the U_PARAMS array, while the
222 # non-option values we collected replace them in @ARGV;
223 push @{$options{U_PARAMS}}, @ARGV;
224 @ARGV=@{$options{ARGV}} if exists $options{ARGV};
230 # Enable bundling of short command line options.
231 Getopt::Long::config("bundling");