File Coverage

blib/lib/Getopt/AutoConf.pm
Criterion Covered Total %
statement 9 54 16.6
branch 0 28 0.0
condition 0 4 0.0
subroutine 3 6 50.0
pod 0 3 0.0
total 12 95 12.6


line stmt bran cond sub pod time code
1             package Getopt::AutoConf;
2              
3             # -------------------------------------------------------------------
4             #
5             # $Id: AutoConf.pm,v 1.6 2001/10/01 12:35:23 dlc Exp $
6             #
7             # -------------------------------------------------------------------
8             # Getopt::AutoConf -- use autoconf(1)-style options
9             #
10             # Copyright (C) 2001 darren chamberlain
11             #
12             # This is free software; you can redistribute it and/or modify it
13             # under the same terms as Perl itself.
14             #
15             # This software is distributed in the hope that it will be useful,
16             # but WITHOUT ANY WARRANTY; without even the implied warranty of
17             # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18             # GNU General Public License for more details.
19             #
20             # You should have received a copy of the GNU General Public License
21             # along with this software. If not, write to the Free Software
22             # Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23             # -------------------------------------------------------------------
24              
25 1     1   6494 use strict;
  1         2  
  1         37  
26 1     1   5 use vars qw($VERSION @EXPORT $DEBUG $ERROR);
  1         2  
  1         94  
27              
28             =head1 NAME
29              
30             Getopt::AutoConf -- use autoconf(1)-style options
31              
32             =head1 SYNOPSIS
33              
34             Getopt::AutoConf provides command-line parameter parsing similar to that
35             provided by GNU autoconf(1). Getopt::AutoConf simplifies parsing of
36             arguments in the form --with, --without, --enable, and --disable.
37              
38             =head1 SYNOPSIS
39              
40             ./configure.pl --with-foo=/usr/local/lib/libfoo.a --disable-bar \
41             --enable-baz --without-quux
42              
43             called as:
44              
45             use Getopt::AutoConf;
46              
47             GetOptions(
48             'foo' => \@foo,
49             'bar' => \$bar,
50             'baz' => \$baz,
51             'quux' => \&quux,
52             ) or die $Getopt::AutoConf::ERROR;
53              
54             print @foo, $bar, $baz;
55             # Prints: /usr/local/lib/libfoo.a 0 1
56              
57             =cut
58              
59             require Exporter;
60              
61 1     1   7 use base qw(Exporter);
  1         6  
  1         838  
62             @EXPORT = qw(GetOptions);
63             $VERSION = sprintf("%d.%02d", q$Revision: 1.6 $ =~ /(\d+)\.(\d+)/);
64             $DEBUG = 0 unless defined $DEBUG;
65              
66             =head1 DESCRIPTION
67              
68             Getopt::AutoConf allows for autoconf-style parameters with no extra
69             parsing on the part of the script writer.
70              
71             The module exports a single function, called GetOptions, which takes a
72             hash describing what options should be parsed. Each key in this hash
73             is a variable name, and each value is a reference to a variable into
74             which the value should be placed, similar to Getopt::Long. GetOptions
75             returns 1 on success or undef on failure. The variables referenced
76             should already be defined, although in the absence of 'use strict'
77             this is not required.
78              
79             Getopt::AutoConf::GetOptions is written in such a way that arguments not
80             beginning with '--enable-', '--disable-', '--with-', or '--without-'
81             are passed through unmodified; another option processing module can
82             then process the remaining arguments. For example:
83              
84             use Getopt::Long ();
85             use Getopt::AutoConf ();
86              
87             my ($foo, $bar, $baz, $quux);
88             Getopt::AutoConf::GetOptions('foo' => \$foo, 'bar' => \$bar);
89             Getopt::Long::GetOptions('baz' => \$baz, 'quux' => \$quux);
90              
91             See t/03golngoa.t for another (working) example. Note that in this
92             case, modules should be used with () as their argument list, and the
93             functions' full name should be typed, to avoid the name clash.
94              
95             The keys to the hash passed into GetOptions can be references of one
96             of three types: references to scalar variables, references to arrays,
97             or code references. How each reference type is dereferenced depends
98             on whether they were preceded by enable, disable, with, or without
99             (each is detailed below).
100              
101             Options can be passed in the any of the following forms:
102              
103             =over 4
104              
105             =cut
106              
107             sub GetOptions {
108 0 0   0 0   if (@_ % 2) {
109 0           $ERROR = "Must call GetOptions with a hash";
110 0           return;
111             }
112 0           my %options = @_;
113 0           my @argv;
114 0           debug("+-> Looking at \@ARGV\n");
115              
116             #
117             # Big foreach loop.
118             #
119 0           for (@ARGV) {
120 0           debug(" +-> Looking at `$_'\n");
121 0 0         if (/^--(?:enable|with)-([a-zA-Z][a-zA-Z0-9_-]*)(?:=(.*))?$/) {
    0          
122              
123             =item B<--with-$var=$value>, B<--enable-$var=$value>
124              
125             This sets $var to $value. If a reference to a scalar is passed to
126             GetOptions, then $value will be assigned to $var. If a reference to
127             an array is passed, the $value will be pushed onto @{$var}. If a code
128             ref is passed, then the code is executed, with ($var, $value) as
129             parameters.
130              
131             If $val is attached to a scalar reference, and there are multiple
132             occurances of $var on the command line, the last one passed overrides
133             all earlier occurances.
134              
135             =cut
136 0           debug(" | `-> Got 'enable' option: `$1' => `$2'\n");
137 0 0         next unless defined $options{$1};
138 0           my $reftype = ref $options{$1};
139 0 0         if ($reftype eq 'SCALAR') {
    0          
    0          
140 0 0         if ($2) {
141 0           ${$options{$1}} = $2;
  0            
142             } else {
143 0           ${$options{$1}} = 1;
  0            
144             }
145             } elsif ($reftype eq 'ARRAY') {
146 0   0       push @{$options{$1}}, ($2 or 1);
  0            
147             } elsif ($reftype eq 'CODE') {
148 0           $options{$1}->($1, $2);
149             } else {
150 0           return error($2, $reftype, $1);
151             }
152             } elsif (/^--(?:without|disable)-([a-zA-Z][-a-zA-Z0-9_]*)(?:=(.*))?$/) {
153              
154             =item B<--without-$var(=$value)?>, B<--disable-$var(=$value)?>
155              
156             Both --without- and --disable- act identically. If a reference to a
157             scalar variable is passed to GetOptions, the this value is set to 0
158             (regardless of what, if anything, comes after the "=" on the command
159             line). If a reference to an array is passed in, and there is nothing
160             after the "=" (or no "="), the referent is set to the empty list. If
161             there is data after the "=", then this data is spliced from the
162             referenced array. Code references are invoked with ($var, $value) as
163             paramters, or ($var, "") if $value is not present (in this way,
164             enabled and disabled variables which are attached to code refs
165             function identically).
166              
167             =back
168              
169             =cut
170 0           debug(" | `-> Got negative option `$1'\n");
171 0 0         next unless defined $options{$1};
172 0           my $reftype = ref $options{$1};
173 0 0         if ($reftype eq 'SCALAR') {
    0          
    0          
174 0           ${$options{$1}} = 0;
  0            
175             } elsif ($reftype eq 'ARRAY') {
176 0 0         if ($2) {
177 0           @{$options{$1}} = grep !/^$2$/, @{$options{$1}};
  0            
  0            
178             } else {
179 0           debug(" | `-> Clearing `$1'\n");
180 0           undef @{$options{$1}};
  0            
181             }
182             } elsif ($reftype eq 'CODE') {
183 0   0       $options{$1}->($1, ($2 || ""));
184             } else {
185 0           error($2, $reftype, $1);
186             }
187             } else {
188 0           debug(" +-> Skipping `$_'\n");
189 0           push @argv, $_;
190             }
191             }
192 0           @ARGV = @argv;
193              
194 0           return 1;
195             }
196              
197 0     0 0   sub error { $ERROR= "Can't assign '$_[0]' to $_[1] '$_[2]'"; return; }
  0            
198 0 0   0 0   sub debug { if ($DEBUG) { warn @_; } }
  0            
199              
200             1;
201             __END__