]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
5e3ffacf6239f0655ff60dd4e4c7bcd982a35fd5
[debhelper.git] / Debian / Debhelper / Dh_Getopt.pm
1 #!/usr/bin/perl -w
2 #
3 # Debhelper option processing library.
4 #
5 # Joey Hess GPL copyright 1998-2002
6
7 package Debian::Debhelper::Dh_Getopt;
8 use strict;
9
10 use Debian::Debhelper::Dh_Lib;
11 use Getopt::Long;
12 use Exporter;
13 #use vars qw{@ISA @EXPORT};
14 #@ISA=qw(Exporter);
15 #@EXPORT=qw(&aparseopts); # FIXME: for some reason, this doesn't work.
16
17 my (%options, %exclude_package);
18
19 sub showhelp {
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"; 
24         exit(1);
25 }
26
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
29 # order.
30 sub AddPackage { my($option,$value)=@_;
31         if ($option eq 'i' or $option eq 'indep') {
32                 push @{$options{DOPACKAGES}}, getpackages('indep');
33                 $options{DOINDEP}=1;
34         }
35         elsif ($option eq 'a' or $option eq 'arch') {
36                 push @{$options{DOPACKAGES}}, getpackages('arch');
37                 $options{DOARCH}=1;
38         }
39         elsif ($option eq 'p' or $option eq 'package') {
40                 push @{$options{DOPACKAGES}}, $value;
41         }
42         elsif ($option eq 's' or $option eq 'same-arch') {
43                 push @{$options{DOPACKAGES}}, getpackages('same');
44                 $options{DOSAME}=1;
45         }
46         else {
47                 error("bad option $option - should never happen!\n");
48         }
49 }
50
51 # Adds packages to the list of debug packages.
52 sub AddDebugPackage { my($option,$value)=@_;
53         push @{$options{DEBUGPACKAGES}}, $value;
54 }
55
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;
59 }
60
61 # Add another item to the exclude list.
62 sub AddExclude { my($option,$value)=@_;
63         push @{$options{EXCLUDE}},$value;
64 }
65
66 # This collects non-options values.
67 sub NonOption {
68         push @{$options{ARGV}}, @_;
69 }
70
71 # Parse options and return a hash of the values.
72 sub parseopts {
73         undef %options;
74         
75         my $ret=GetOptions(
76                 "v" => \$options{VERBOSE},
77                 "verbose" => \$options{VERBOSE},
78         
79                 "i" => \&AddPackage,
80                 "indep" => \&AddPackage,
81         
82                 "a" => \&AddPackage,
83                 "arch" => \&AddPackage,
84         
85                 "p=s" => \&AddPackage,
86                 "package=s" => \&AddPackage,
87         
88                 "dbg-package=s" => \&AddDebugPackage,
89                 
90                 "s" => \&AddPackage,
91                 "same-arch" => \&AddPackage,
92         
93                 "N=s" => \&ExcludePackage,
94                 "no-package=s" => \&ExcludePackage,
95         
96                 "n" => \$options{NOSCRIPTS},
97                 "noscripts" => \$options{NOSCRIPTS},
98                 "o" => \$options{ONLYSCRIPTS},
99                 "onlyscripts" => \$options{ONLYSCRIPTS},
100
101                 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
102                 "include-conffiles" => \$options{INCLUDE_CONFFILES},
103         
104                 "X=s" => \&AddExclude,
105                 "exclude=s" => \&AddExclude,
106         
107                 "d" => \$options{D_FLAG},
108                 "remove-d" => \$options{D_FLAG},
109                 "dirs-only" => \$options{D_FLAG},
110         
111                 "r" => \$options{R_FLAG},
112                 "no-restart-on-upgrade" => \$options{R_FLAG},
113                 "no-start" => \$options{NO_START},
114         
115                 "k" => \$options{K_FLAG},
116                 "keep" => \$options{K_FLAG},
117
118                 "P=s" => \$options{TMPDIR},
119                 "tmpdir=s" => \$options{TMPDIR},
120
121                 "u=s", => \$options{U_PARAMS},
122                 "update-rcd-params=s", => \$options{U_PARAMS},
123                 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
124                 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
125
126                 "l=s", => \$options{L_PARAMS},
127
128                 "m=s", => \$options{M_PARAMS},
129                 "major=s" => \$options{M_PARAMS},
130
131                 "V:s", => \$options{V_FLAG},
132                 "version-info:s" => \$options{V_FLAG},
133
134                 "A" => \$options{PARAMS_ALL},
135                 "all" => \$options{PARAMS_ALL},
136
137                 "no-act" => \$options{NO_ACT},
138         
139                 "init-script=s" => \$options{INIT_SCRIPT},
140                 
141                 "sourcedir=s" => \$options{SOURCEDIR},
142                 
143                 "destdir=s" => \$options{DESTDIR},
144
145                 "filename=s" => \$options{FILENAME},
146                 
147                 "priority=i" => \$options{PRIORITY},
148                 
149                 "flavor=s" => \$options{FLAVOR},
150
151                 "autodest" => \$options{AUTODEST},
152
153                 "h|help" => \&showhelp,
154
155                 "mainpackage=s" => \$options{MAINPACKAGE},
156
157                 "list-missing" => \$options{LIST_MISSING},
158
159                 "fail-missing" => \$options{FAIL_MISSING},
160                 
161                 "L|libpackage=s" => \$options{LIBPACKAGE},
162                 
163                 "name=s" => \$options{NAME},
164                 
165                 "keep-debug" => \$options{KEEP_DEBUG},
166                 
167                 "error-handler=s" => \$options{ERROR_HANDLER},
168                 
169                 "language=s" => \$options{LANGUAGE},
170
171                 "<>" => \&NonOption,
172         );
173
174         if (!$ret) {
175                 error("unknown option; aborting");
176         }
177         
178         # Check to see if -V was specified. If so, but no parameters were
179         # passed, the variable will be defined but empty.
180         if (defined($options{V_FLAG})) {
181                 $options{V_FLAG_SET}=1;
182         }
183         
184         # If we have not been given any packages to act on, assume they
185         # want us to act on them all. Note we have to do this before excluding
186         # packages out, below.
187         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
188                 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
189                         # User specified that all arch (in)dep package be
190                         # built, and there are none of that type.
191                         error("I have no package to build");
192                 }
193                 push @{$options{DOPACKAGES}},getpackages();
194         }
195
196         # Remove excluded packages from the list of packages to act on.
197         # Also unique the list, in case some options were specified that
198         # added a package to it twice.
199         my @package_list;
200         my $package;
201         my %packages_seen;
202         foreach $package (@{$options{DOPACKAGES}}) {
203                 if (! $exclude_package{$package}) {
204                         if (! exists $packages_seen{$package}) {
205                                 $packages_seen{$package}=1;
206                                 push @package_list, $package;   
207                         }
208                 }
209         }
210         @{$options{DOPACKAGES}}=@package_list;
211
212         # If there are no packages to act on now, it's an error.
213         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
214                 error("I have no package to build");
215         }
216
217         if (defined $options{U_PARAMS}) {
218                 # Split the U_PARAMS up into an array.
219                 my $u=$options{U_PARAMS};
220                 undef $options{U_PARAMS};
221                 push @{$options{U_PARAMS}}, split(/\s+/,$u);
222         }
223
224         # Anything left in @ARGV is options that appeared after a --
225         # These options are added to the U_PARAMS array, while the
226         # non-option values we collected replace them in @ARGV;
227         push @{$options{U_PARAMS}}, @ARGV;
228         @ARGV=@{$options{ARGV}} if exists $options{ARGV};
229
230         return %options;
231 }
232
233 sub import {
234         # Enable bundling of short command line options.
235         Getopt::Long::config("bundling");
236 }               
237
238 1