]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
79674144a95a04021ff63fa309a54a8bce4a63cb
[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(1)\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         
105                 "k" => \$options{K_FLAG},
106                 "keep" => \$options{K_FLAG},
107
108                 "P=s" => \$options{TMPDIR},
109                 "tmpdir=s" => \$options{TMPDIR},
110
111                 "u=s", => \$options{U_PARAMS},
112                 "update-rcd-params=s", => \$options{U_PARAMS},
113                 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
114                 "dpkg-gencontrol-params=s", => \$options{U_PARAMS},
115
116                 "l=s", => \$options{L_PARAMS},
117
118                 "m=s", => \$options{M_PARAMS},
119                 "major=s" => \$options{M_PARAMS},
120
121                 "V:s", => \$options{V_FLAG},
122                 "version-info:s" => \$options{V_FLAG},
123
124                 "A" => \$options{PARAMS_ALL},
125                 "all" => \$options{PARAMS_ALL},
126
127                 "no-act" => \$options{NO_ACT},
128         
129                 "init-script=s" => \$options{INIT_SCRIPT},
130                 
131                 "sourcedir=s" => \$options{SOURCEDIR},
132                 
133                 "destdir=s" => \$options{DESTDIR},
134
135                 "filename=s" => \$options{FILENAME},
136                 
137                 "priority=i" => \$options{PRIORITY},
138                 
139                 "flavor=s" => \$options{FLAVOR},
140
141                 "autodest" => \$options{AUTODEST},
142
143                 "h|help" => \&showhelp,
144
145                 "mainpackage=s" => \$options{MAINPACKAGE},
146
147                 "list-missing" => \$options{LIST_MISSING},
148                 
149                 "<>" => \&NonOption,
150         );
151
152         if (!$ret) {
153                 error("unknown option; aborting");
154         }
155         
156         # Check to see if -V was specified. If so, but no parameters were
157         # passed, the variable will be defined but empty.
158         if (defined($options{V_FLAG})) {
159                 $options{V_FLAG_SET}=1;
160         }
161         
162         # If we have not been given any packages to act on, assume they
163         # want us to act on them all. Note we have to do this before excluding
164         # packages out, below.
165         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
166                 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
167                         # User specified that all arch (in)dep package be
168                         # built, and there are none of that type.
169                         error("I have no package to build");
170                 }
171                 push @{$options{DOPACKAGES}},GetPackages();
172         }
173         
174         # Remove excluded packages from the list of packages to act on.
175         my @package_list;
176         my $package;
177         foreach $package (@{$options{DOPACKAGES}}) {
178                 if (! $exclude_package{$package}) {
179                         push @package_list, $package;   
180                 }
181         }
182         @{$options{DOPACKAGES}}=@package_list;
183
184         # If there are no packages to act on now, it's an error.
185         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
186                 error("I have no package to build");
187         }
188
189         if (defined $options{U_PARAMS}) {
190                 # Split the U_PARAMS up into an array.
191                 my $u=$options{U_PARAMS};
192                 undef $options{U_PARAMS};
193                 push @{$options{U_PARAMS}}, split(/\s+/,$u);
194         }
195
196         # Anything left in @ARGV is options that appeared after a --
197         # These options are added to the U_PARAMS array, while the
198         # non-option values we collected replace them in @ARGV;
199         push @{$options{U_PARAMS}}, @ARGV;
200         @ARGV=@{$options{ARGV}} if exists $options{ARGV};
201
202         return %options;
203 }
204
205 sub import {
206         # Enable bundling of short command line options.
207         Getopt::Long::config("bundling");
208 }               
209
210 1