source: branches/New_Config_Format-branch/gsdl/perllib/parsargv.pm@ 1279

Last change on this file since 1279 was 1279, checked in by sjboddie, 24 years ago

merged changes to trunk into New_Config_Format branch

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 5.5 KB
Line 
1###########################################################################
2#
3# parseargv.pm --
4# A component of the Greenstone digital library software
5# from the New Zealand Digital Library Project at the
6# University of Waikato, New Zealand.
7#
8# Copyright (C) 1999 New Zealand Digital Library Project
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program 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 program; if not, write to the Free Software
22# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
23#
24###########################################################################
25
26package parsargv;
27#
28# parse(ARGVREF, [SPEC, VARREF] ...)
29#
30# Parse command line arguments.
31#
32# ARGVREF is an array reference, usually to @ARGV. The remaining
33# arguments are paired (SPEC, VARREF). SPEC is a specification string
34# for a particular argument; VARREF is a variable reference that will
35# receive the argument.
36#
37# SPEC is in one of the following forms:
38#
39# ARG/REGEX/DEFAULT ARG is the name of command line argument. REGEX is
40# a regular expression that gives legal values for the argument.
41# DEFAULT is the default value assigned to VARREF if the option does
42# not appear on the command line. Example
43#
44#
45# ARG/REGEX ARG and REGEX are as above. Since no default is given, ARG
46# must appear on the command line; if it doesn't, parse() returns 0.
47#
48# ARG ARG is as above. ARG is a boolean option. VARREF is assigned 0 if ARG
49# is not on the command line; 1 otherwise.
50#
51# SPEC may start with a punctuation character, in which case this
52# character will be used instead of '/' as a delimiter. Useful when '/'
53# is needed in the REGEX part.
54#
55# VARREF is a reference to a scalar or an array. If VARREF is an array
56# reference, then multiple command line options are allowed an append. Example:
57#
58# Command line: -day mon -day fri
59#
60# parse(\@ARGV, "day/(mon|tue|wed|thu|fri)", \@days)
61#
62# days => ('mon', 'fri')
63#
64# Returns 0 if there was an error, nonzero otherwise.
65#
66
67
68 sub parse
69{
70 my $arglist = shift;
71 my ($spec, $var);
72 my %option;
73
74 my @rest = @_;
75
76 # if the last argument is the string "allow_extra_options" then options
77 # in \@rest without a corresponding SPEC will be ignored (i.e. the "$arg is
78 # not a valid option" error won't occur)\n";
79 my $allow_extra_options = pop @rest;
80 if (defined ($allow_extra_options)) {
81 if ($allow_extra_options eq "allow_extra_options") {
82 $allow_extra_options = 1;
83 } else {
84 # put it back where we got it
85 push (@rest, $allow_extra_options);
86 $allow_extra_options = 0;
87 }
88 } else {
89 $allow_extra_options = 0;
90 }
91
92 while (($spec, $var) = splice(@rest, 0, 2))
93 {
94 die "Variable for $spec is not a valid type."
95 unless ref($var) eq 'SCALAR' || ref($var) eq 'ARRAY';
96
97 my $delimiter;
98 if ($spec !~ /^\w/)
99 {
100 $delimiter = substr($spec, 0, 1);
101 $spec = substr($spec, 1);
102 }
103 else
104 {
105 $delimiter = '/';
106 }
107 my ($name, $regex, $default) = split(/$delimiter/, $spec, 3);
108
109 if ($name)
110 {
111 if ($default && $default !~ /$regex/)
112 {
113 die "Default value for $name doesn't match regex ($spec).";
114 }
115 $option{$name} = {'name' => $name,
116 'regex' => $regex,
117 'default' => $default,
118 'varref' => $var,
119 'set' => 0};
120 }
121 else
122 {
123 die "Invalid argument ($spec) for parsargv.";
124 }
125 }
126
127 my @argv;
128 my $arg;
129 my $parse_options = 1;
130 my $errors = 0;
131
132 while ($arg = shift(@$arglist))
133 {
134 if ($parse_options && $arg eq '--')
135 {
136 $parse_options = 0;
137 next;
138 }
139
140 if ($parse_options && $arg =~ /^-+\w/)
141 {
142 $arg =~ s/^-+//;
143
144 if (defined $option{$arg})
145 {
146 &process_arg($option{$arg}, $arglist, \$errors);
147 }
148 elsif (!$allow_extra_options)
149 {
150 print STDERR "$arg is not a valid option.\n";
151 $errors++;
152 }
153 }
154 else
155 {
156 push(@argv, $arg);
157 }
158 }
159 @$arglist = @argv;
160
161 foreach $arg (keys %option)
162 {
163 if ($option{$arg}->{'set'} == 0)
164 {
165 if (defined $option{$arg}->{'default'})
166 {
167 &set_var($option{$arg}, $option{$arg}->{'default'});
168 }
169 elsif (!$option{$arg}->{'regex'})
170 {
171 &set_var($option{$arg}, 0)
172 }
173 elsif (ref($option{$arg}->{'varref'}) ne 'ARRAY')
174 {
175 print STDERR "Missing command line argument -$arg.\n";
176 $errors++;
177 }
178 }
179 }
180 return $errors == 0;
181}
182
183sub process_arg
184{
185 my ($option, $arglist, $errors) = @_;
186
187 if ($option->{'regex'} && @$arglist > 0 && $arglist->[0] !~ /^-+\w/)
188 {
189 if ($arglist->[0] =~ /$option->{'regex'}/)
190 {
191 &set_var($option, shift(@$arglist));
192 }
193 else
194 {
195 print STDERR "Bad value for -$option->{'name'} argument.\n";
196 $$errors++;
197 }
198 }
199 elsif (!$option->{'regex'})
200 {
201 &set_var($option, 1);
202 }
203 else
204 {
205 print STDERR "No value given for -$option->{'name'}.\n";
206 $$errors++;
207 }
208}
209
210sub set_var
211{
212 my ($option, $value) = @_;
213 my $type = ref($option->{'varref'});
214
215 if ($type eq 'SCALAR')
216 {
217 ${$option->{'varref'}} = $value;
218 }
219 elsif ($type eq 'ARRAY')
220 {
221 push(@{$option->{'varref'}}, $value);
222 }
223 $option->{'set'} = 1;
224}
225
2261;
Note: See TracBrowser for help on using the repository browser.