]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
16851a04902999cd2cb929e184e07080ba77f783
[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                 "<>" => \&NonOption,
146         );
147
148         if (!$ret) {
149                 error("unknown option; aborting");
150         }
151         
152         # Check to see if -V was specified. If so, but no parameters were
153         # passed, the variable will be defined but empty.
154         if (defined($options{V_FLAG})) {
155                 $options{V_FLAG_SET}=1;
156         }
157         
158         # If we have not been given any packages to act on, assume they
159         # want us to act on them all. Note we have to do this before excluding
160         # packages out, below.
161         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
162                 if ($options{DOINDEP} || $options{DOARCH} || $options{DOSAME}) {
163                         # User specified that all arch (in)dep package be
164                         # built, and there are none of that type.
165                         error("I have no package to build");
166                 }
167                 push @{$options{DOPACKAGES}},GetPackages();
168         }
169         
170         # Remove excluded packages from the list of packages to act on.
171         my @package_list;
172         my $package;
173         foreach $package (@{$options{DOPACKAGES}}) {
174                 if (! $exclude_package{$package}) {
175                         push @package_list, $package;   
176                 }
177         }
178         @{$options{DOPACKAGES}}=@package_list;
179
180         # If there are no packages to act on now, it's an error.
181         if (! defined $options{DOPACKAGES} || ! @{$options{DOPACKAGES}}) {
182                 error("I have no package to build");
183         }
184
185         if (defined $options{U_PARAMS}) {
186                 # Split the U_PARAMS up into an array.
187                 my $u=$options{U_PARAMS};
188                 undef $options{U_PARAMS};
189                 push @{$options{U_PARAMS}}, split(/\s+/,$u);
190         }
191
192         # Anything left in @ARGV is options that appeared after a --
193         # These options are added to the U_PARAMS array, while the
194         # non-option values we collected replace them in @ARGV;
195         push @{$options{U_PARAMS}}, @ARGV;
196         @ARGV=@{$options{ARGV}} if exists $options{ARGV};
197
198         return %options;
199 }
200
201 sub import {
202         # Enable bundling of short command line options.
203         Getopt::Long::config("bundling");
204 }               
205
206 1