]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
53db13ae294cbab97b5352adf8cf974ee7592522
[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 # Add a package to a list of packages that should not be acted on.
52 sub ExcludePackage { my($option,$value)=@_;
53         $exclude_package{$value}=1;
54 }
55
56 # Add another item to the exclude list.
57 sub AddExclude { my($option,$value)=@_;
58         push @{$options{EXCLUDE}},$value;
59 }
60
61 # This collects non-options values.
62 sub NonOption {
63         push @{$options{ARGV}}, @_;
64 }
65
66 # Parse options and return a hash of the values.
67 sub parseopts {
68         undef %options;
69         
70         my $ret=GetOptions(
71                 "v" => \$options{VERBOSE},
72                 "verbose" => \$options{VERBOSE},
73         
74                 "i" => \&AddPackage,
75                 "indep" => \&AddPackage,
76         
77                 "a" => \&AddPackage,
78                 "arch" => \&AddPackage,
79         
80                 "p=s" => \&AddPackage,
81                 "package=s" => \&AddPackage,
82         
83                 "s" => \&AddPackage,
84                 "same-arch" => \&AddPackage,
85         
86                 "N=s" => \&ExcludePackage,
87                 "no-package=s" => \&ExcludePackage,
88         
89                 "n" => \$options{NOSCRIPTS},
90                 "noscripts" => \$options{NOSCRIPTS},
91
92                 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
93                 "include-conffiles" => \$options{INCLUDE_CONFFILES},
94         
95                 "X=s" => \&AddExclude,
96                 "exclude=s" => \&AddExclude,
97         
98                 "d" => \$options{D_FLAG},
99                 "remove-d" => \$options{D_FLAG},
100                 "dirs-only" => \$options{D_FLAG},
101         
102                 "r" => \$options{R_FLAG},
103                 "no-restart-on-upgrade" => \$options{R_FLAG},
104                 "no-start" => \$options{NO_START},
105         
106                 "k" => \$options{K_FLAG},
107                 "keep" => \$options{K_FLAG},
108
109                 "P=s" => \$options{TMPDIR},
110                 "tmpdir=s" => \$options{TMPDIR},
111
112                 "u=s", => \$options{U_PARAMS},
113                 "update-rcd-params=s", => \$options{U_PARAMS},
114                 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
115                 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
116
117                 "l=s", => \$options{L_PARAMS},
118
119                 "m=s", => \$options{M_PARAMS},
120                 "major=s" => \$options{M_PARAMS},
121
122                 "V:s", => \$options{V_FLAG},
123                 "version-info:s" => \$options{V_FLAG},
124
125                 "A" => \$options{PARAMS_ALL},
126                 "all" => \$options{PARAMS_ALL},
127
128                 "no-act" => \$options{NO_ACT},
129         
130                 "init-script=s" => \$options{INIT_SCRIPT},
131                 
132                 "sourcedir=s" => \$options{SOURCEDIR},
133                 
134                 "destdir=s" => \$options{DESTDIR},
135
136                 "filename=s" => \$options{FILENAME},
137                 
138                 "priority=i" => \$options{PRIORITY},
139                 
140                 "flavor=s" => \$options{FLAVOR},
141
142                 "autodest" => \$options{AUTODEST},
143
144                 "h|help" => \&showhelp,
145
146                 "mainpackage=s" => \$options{MAINPACKAGE},
147
148                 "list-missing" => \$options{LIST_MISSING},
149                 
150                 "L|libpackage=s" => \$options{LIBPACKAGE},
151                 
152                 "<>" => \&NonOption,
153         );
154
155         if (!$ret) {
156                 error("unknown option; aborting");
157         }
158         
159         # Check to see if -V was specified. If so, but no parameters were
160         # passed, the variable will be defined but empty.
161         if (defined($options{V_FLAG})) {
162                 $options{V_FLAG_SET}=1;
163         }
164         
165         # If we have not been given any packages to act on, assume they
166         # want us to act on them all. Note we have to do this before excluding
167         # packages out, below.
168         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
169                 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
170                         # User specified that all arch (in)dep package be
171                         # built, and there are none of that type.
172                         error("I have no package to build");
173                 }
174                 push @{$options{DOPACKAGES}},GetPackages();
175         }
176         
177         # Remove excluded packages from the list of packages to act on.
178         my @package_list;
179         my $package;
180         foreach $package (@{$options{DOPACKAGES}}) {
181                 if (! $exclude_package{$package}) {
182                         push @package_list, $package;   
183                 }
184         }
185         @{$options{DOPACKAGES}}=@package_list;
186
187         # If there are no packages to act on now, it's an error.
188         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
189                 error("I have no package to build");
190         }
191
192         if (defined $options{U_PARAMS}) {
193                 # Split the U_PARAMS up into an array.
194                 my $u=$options{U_PARAMS};
195                 undef $options{U_PARAMS};
196                 push @{$options{U_PARAMS}}, split(/\s+/,$u);
197         }
198
199         # Anything left in @ARGV is options that appeared after a --
200         # These options are added to the U_PARAMS array, while the
201         # non-option values we collected replace them in @ARGV;
202         push @{$options{U_PARAMS}}, @ARGV;
203         @ARGV=@{$options{ARGV}} if exists $options{ARGV};
204
205         return %options;
206 }
207
208 sub import {
209         # Enable bundling of short command line options.
210         Getopt::Long::config("bundling");
211 }               
212
213 1