]> git.donarmstrong.com Git - debhelper.git/blob - Debian/Debhelper/Dh_Getopt.pm
cc15ddde9fb3b55e73d7a4448e66b6faed5f6222
[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
13 my %exclude_package;
14
15 sub showhelp {
16         my $prog=basename($0);
17         print "Usage: $prog [options]\n\n";
18         print "  $prog is a part of debhelper. See debhelper(7)\n";
19         print "  and $prog(1) for complete usage instructions.\n"; 
20         exit(1);
21 }
22
23 # Passed an option name and an option value, adds packages to the list
24 # of packages. We need this so the list will be built up in the right
25 # order.
26 sub AddPackage { my($option,$value)=@_;
27         if ($option eq 'i' or $option eq 'indep') {
28                 push @{$dh{DOPACKAGES}}, getpackages('indep');
29                 $dh{DOINDEP}=1;
30         }
31         elsif ($option eq 'a' or $option eq 'arch') {
32                 push @{$dh{DOPACKAGES}}, getpackages('arch');
33                 $dh{DOARCH}=1;
34         }
35         elsif ($option eq 'p' or $option eq 'package') {
36                 push @{$dh{DOPACKAGES}}, $value;
37         }
38         elsif ($option eq 's' or $option eq 'same-arch') {
39                 push @{$dh{DOPACKAGES}}, getpackages('same');
40                 $dh{DOSAME}=1;
41         }
42         else {
43                 error("bad option $option - should never happen!\n");
44         }
45 }
46
47 # Adds packages to the list of debug packages.
48 sub AddDebugPackage { my($option,$value)=@_;
49         push @{$dh{DEBUGPACKAGES}}, $value;
50 }
51
52 # Add a package to a list of packages that should not be acted on.
53 sub ExcludePackage { my($option,$value)=@_;
54         $exclude_package{$value}=1;
55 }
56
57 # Add another item to the exclude list.
58 sub AddExclude { my($option,$value)=@_;
59         push @{$dh{EXCLUDE}},$value;
60 }
61
62 # Add a file to the ignore list.
63 sub AddIgnore { my($option,$file)=@_;
64         $dh{IGNORE}->{$file}=1;
65 }
66
67 # This collects non-options values.
68 sub NonOption {
69         push @{$dh{ARGV}}, @_;
70 }
71
72 # Parse options and set %dh values.
73 sub parseopts {
74         my %options=%{shift()} if ref $_[0];
75
76         my $ret=GetOptions(
77                 "v" => \$dh{VERBOSE},
78                 "verbose" => \$dh{VERBOSE},
79
80                 "no-act" => \$dh{NO_ACT},
81         
82                 "i" => \&AddPackage,
83                 "indep" => \&AddPackage,
84         
85                 "a" => \&AddPackage,
86                 "arch" => \&AddPackage,
87         
88                 "p=s" => \&AddPackage,
89                 "package=s" => \&AddPackage,
90                 
91                 "N=s" => \&ExcludePackage,
92                 "no-package=s" => \&ExcludePackage,
93         
94                 "dbg-package=s" => \&AddDebugPackage,
95                 
96                 "s" => \&AddPackage,
97                 "same-arch" => \&AddPackage,
98         
99                 "n" => \$dh{NOSCRIPTS},
100                 "noscripts" => \$dh{NOSCRIPTS},
101                 "o" => \$dh{ONLYSCRIPTS},
102                 "onlyscripts" => \$dh{ONLYSCRIPTS},
103
104                 "X=s" => \&AddExclude,
105                 "exclude=s" => \&AddExclude,
106                 
107                 "d" => \$dh{D_FLAG},
108         
109                 "k" => \$dh{K_FLAG},
110                 "keep" => \$dh{K_FLAG},
111
112                 "P=s" => \$dh{TMPDIR},
113                 "tmpdir=s" => \$dh{TMPDIR},
114
115                 "u=s", => \$dh{U_PARAMS},
116
117                 "V:s", => \$dh{V_FLAG},
118
119                 "A" => \$dh{PARAMS_ALL},
120                 "all" => \$dh{PARAMS_ALL},
121         
122                 "sourcedir=s" => \$dh{SOURCEDIR},
123                 
124                 "destdir=s" => \$dh{DESTDIR},
125                 
126                 "priority=s" => \$dh{PRIORITY},
127                 
128                 "h|help" => \&showhelp,
129
130                 "mainpackage=s" => \$dh{MAINPACKAGE},
131
132                 "name=s" => \$dh{NAME},
133
134                 "error-handler=s" => \$dh{ERROR_HANDLER},
135                 
136                 "ignore=s" => \&AddIgnore,
137
138                 %options,
139
140                 "<>" => \&NonOption,
141         );
142
143         if (!$ret) {
144                 warning("warning: unknown options will be a fatal error in a future debhelper release");
145                 #error("unknown option; aborting");
146         }
147         
148         # Check to see if -V was specified. If so, but no parameters were
149         # passed, the variable will be defined but empty.
150         if (defined($dh{V_FLAG})) {
151                 $dh{V_FLAG_SET}=1;
152         }
153         
154         # If we have not been given any packages to act on, assume they
155         # want us to act on them all. Note we have to do this before excluding
156         # packages out, below.
157         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
158                 if ($dh{DOINDEP} || $dh{DOARCH} || $dh{DOSAME}) {
159                         # User specified that all arch (in)dep package be
160                         # built, and there are none of that type.
161                         warning("I have no package to build");
162                         exit(0);
163                 }
164                 push @{$dh{DOPACKAGES}},getpackages();
165         }
166
167         # Remove excluded packages from the list of packages to act on.
168         # Also unique the list, in case some options were specified that
169         # added a package to it twice.
170         my @package_list;
171         my $package;
172         my %packages_seen;
173         foreach $package (@{$dh{DOPACKAGES}}) {
174                 if (! $exclude_package{$package}) {
175                         if (! exists $packages_seen{$package}) {
176                                 $packages_seen{$package}=1;
177                                 push @package_list, $package;   
178                         }
179                 }
180         }
181         @{$dh{DOPACKAGES}}=@package_list;
182
183         # If there are no packages to act on now, it's an error.
184         if (! defined $dh{DOPACKAGES} || ! @{$dh{DOPACKAGES}}) {
185                 error("I have no package to build");
186         }
187
188         if (defined $dh{U_PARAMS}) {
189                 # Split the U_PARAMS up into an array.
190                 my $u=$dh{U_PARAMS};
191                 undef $dh{U_PARAMS};
192                 push @{$dh{U_PARAMS}}, split(/\s+/,$u);
193         }
194
195         # Anything left in @ARGV is options that appeared after a --
196         # These options are added to the U_PARAMS array, while the
197         # non-option values we collected replace them in @ARGV;
198         push @{$dh{U_PARAMS}}, @ARGV;
199         @ARGV=@{$dh{ARGV}} if exists $dh{ARGV};
200 }
201
202 sub import {
203         # Enable bundling of short command line options.
204         Getopt::Long::config("bundling");
205 }               
206
207 1