]> git.donarmstrong.com Git - debhelper.git/blob - Dh_Getopt.pm
eb6aef98b7a63896fc8cc2b2e48ea1aacbe69d06
[debhelper.git] / Dh_Getopt.pm
1 #!/usr/bin/perl -w
2 #
3 # Debhelper option processing library.
4 #
5 # Joey Hess GPL copyright 1998.
6
7 package Dh_Getopt;
8 use strict;
9
10 use Exporter;
11 my @ISA=qw(Exporter);
12 my @EXPORT=qw(&parseopts);
13
14 use Dh_Lib;
15 use Getopt::Long;
16
17 my (%options, %exclude_package);
18
19 # Passed an option name and an option value, adds packages to the list
20 # of packages. We need this so the list will be built up in the right
21 # order.
22 sub AddPackage { my($option,$value)=@_;
23         if ($option eq 'i' or $option eq 'indep') {
24                 push @{$options{DOPACKAGES}}, GetPackages('indep');
25                 $options{DOINDEP}=1;
26         }
27         elsif ($option eq 'a' or $option eq 'arch') {
28                 push @{$options{DOPACKAGES}}, GetPackages('arch');
29                 $options{DOARCH}=1;
30         }
31         elsif ($option eq 'p' or $option eq 'package') {
32                 push @{$options{DOPACKAGES}}, $value;
33         }
34         else {
35                 error("bad option $option - should never happen!\n");
36         }
37 }
38
39 # Add a package to a list of packages that should not be acted on.
40 sub ExcludePackage { my($option,$value)=@_;
41         $exclude_package{$value}=1;
42 }
43
44 # Add another item to the exclude list.
45 sub AddExclude { my($option,$value)=@_;
46         push @{$options{EXCLUDE}},$value;
47 }
48
49 sub import {
50         # Enable bundling of short command line options.
51         Getopt::Long::config("bundling");
52 }
53
54 # Parse options and return a hash of the values.
55 sub parseopts {
56         undef %options;
57
58         my $ret=GetOptions(
59                 "v" => \$options{VERBOSE},
60                 "verbose" => \$options{VERBOSE},
61         
62                 "i" => \&AddPackage,
63                 "indep" => \&AddPackage,
64         
65                 "a" => \&AddPackage,
66                 "arch" => \&AddPackage,
67         
68                 "p=s" => \&AddPackage,
69                 "package=s" => \&AddPackage,
70         
71                 "N=s" => \&ExcludePackage,
72                 "no-package=s" => \&ExcludePackage,
73         
74                 "n" => \$options{NOSCRIPTS},
75 #               "noscripts" => \$options(NOSCRIPTS},
76         
77                 "x" => \$options{INCLUDE_CONFFILES}, # is -x for some unknown historical reason..
78                 "include-conffiles" => \$options{INCLUDE_CONFFILES},
79         
80                 "X=s" => \&AddExclude,
81                 "exclude=s" => \&AddExclude,
82         
83                 "d" => \$options{D_FLAG},
84                 "remove-d" => \$options{D_FLAG},
85         
86                 "r" => \$options{R_FLAG},
87                 "no-restart-on-upgrade" => \$options{R_FLAG},
88         
89                 "k" => \$options{K_FLAG},
90                 "keep" => \$options{K_FLAG},
91
92                 "P=s" => \$options{TMPDIR},
93                 "tmpdir=s" => \$options{TMPDIR},
94
95                 "u=s", => \$options{U_PARAMS},
96                 "update-rcd-params=s", => \$options{U_PARAMS},
97                 "dpkg-shlibdeps-params=s", => \$options{U_PARAMS},
98
99                 "m=s", => \$options{M_PARAMS},
100                 "major=s" => \$options{M_PARAMS},
101
102                 "V:s", => \$options{V_FLAG},
103                 "version-info:s" => \$options{V_FLAG},
104
105                 "A" => \$options{PARAMS_ALL},
106                 "all" => \$options{PARAMS_ALL},
107
108                 "no-act" => \$options{NO_ACT},
109         
110                 "init-script=s" => \$options{INIT_SCRIPT},
111         );
112
113         if (!$ret) {
114                 error("unknown option; aborting");
115         }
116
117         # Check to see if -V was specified. If so, but no parameters were
118         # passed, the variable will be defined but empty.
119         if (defined($options{V_FLAG})) {
120                 $options{V_FLAG_SET}=1;
121         }
122         
123         # Check to see if DH_VERBOSE environment variable was set, if so,
124         # make sure verbose is on.
125         if ($ENV{DH_VERBOSE} ne undef) {
126                 $options{VERBOSE}=1;
127         }
128         
129         # Check to see if DH_NO_ACT environment variable was set, if so, 
130         # make sure no act mode is on.
131         if ($ENV{DH_NO_ACT} ne undef) {
132                 $options{NO_ACT}=1;
133         }
134
135         # Remove excluded packages from the list of packages to act on.
136         my @package_list;
137         my $package;
138         foreach $package (@{$options{DOPACKAGES}}) {
139                 if (! $exclude_package{$package}) {
140                         push @package_list, $package;   
141                 }
142         }
143         @{$options{DOPACKAGES}}=@package_list;
144         
145         return %options;
146 }       
147
148 1