source: trunk/gsdl/perllib/parsargv.pm@ 537

Last change on this file since 537 was 537, checked in by sjboddie, 25 years ago

added GPL headers

  • Property svn:executable set to *
  • Property svn:keywords set to Author Date Id Revision
File size: 4.9 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#
66sub parse
67{
68 my $arglist = shift;
69 my ($spec, $var);
70 my %option;
71
72 while (($spec, $var) = splice(@_, 0, 2))
73 {
74 die "Variable for $spec is not a valid type."
75 unless ref($var) eq 'SCALAR' || ref($var) eq 'ARRAY';
76
77 my $delimiter;
78 if ($spec !~ /^\w/)
79 {
80 $delimiter = substr($spec, 0, 1);
81 $spec = substr($spec, 1);
82 }
83 else
84 {
85 $delimiter = '/';
86 }
87 my ($name, $regex, $default) = split(/$delimiter/, $spec, 3);
88
89 if ($name)
90 {
91 if ($default && $default !~ /$regex/)
92 {
93 die "Default value for $name doesn't match regex ($spec).";
94 }
95 $option{$name} = {'name' => $name,
96 'regex' => $regex,
97 'default' => $default,
98 'varref' => $var,
99 'set' => 0};
100 }
101 else
102 {
103 die "Invalid argument ($spec) for parsargv.";
104 }
105 }
106
107 my @argv;
108 my $arg;
109 my $parse_options = 1;
110 my $errors = 0;
111
112 while ($arg = shift(@$arglist))
113 {
114 if ($parse_options && $arg eq '--')
115 {
116 $parse_options = 0;
117 next;
118 }
119
120 if ($parse_options && $arg =~ /^-+\w/)
121 {
122 $arg =~ s/^-+//;
123
124 if (defined $option{$arg})
125 {
126 &process_arg($option{$arg}, $arglist, \$errors);
127 }
128 else
129 {
130 print STDERR "$arg is not a valid option.\n";
131 $errors++;
132 }
133 }
134 else
135 {
136 push(@argv, $arg);
137 }
138 }
139 @$arglist = @argv;
140
141 foreach $arg (keys %option)
142 {
143 if ($option{$arg}->{'set'} == 0)
144 {
145 if (defined $option{$arg}->{'default'})
146 {
147 &set_var($option{$arg}, $option{$arg}->{'default'});
148 }
149 elsif (!$option{$arg}->{'regex'})
150 {
151 &set_var($option{$arg}, 0)
152 }
153 elsif (ref($option{$arg}->{'varref'}) ne 'ARRAY')
154 {
155 print STDERR "Missing command line argument -$arg.\n";
156 $errors++;
157 }
158 }
159 }
160 return $errors == 0;
161}
162
163sub process_arg
164{
165 my ($option, $arglist, $errors) = @_;
166
167 if ($option->{'regex'} && @$arglist > 0 && $arglist->[0] !~ /^-+\w/)
168 {
169 if ($arglist->[0] =~ /$option->{'regex'}/)
170 {
171 &set_var($option, shift(@$arglist));
172 }
173 else
174 {
175 print STDERR "Bad value for -$option->{'name'} argument.\n";
176 $$errors++;
177 }
178 }
179 elsif (!$option->{'regex'})
180 {
181 &set_var($option, 1);
182 }
183 else
184 {
185 print STDERR "No value given for -$option->{'name'}.\n";
186 $$errors++;
187 }
188}
189
190sub set_var
191{
192 my ($option, $value) = @_;
193 my $type = ref($option->{'varref'});
194
195 if ($type eq 'SCALAR')
196 {
197 ${$option->{'varref'}} = $value;
198 }
199 elsif ($type eq 'ARRAY')
200 {
201 push(@{$option->{'varref'}}, $value);
202 }
203 $option->{'set'} = 1;
204}
205
2061;
Note: See TracBrowser for help on using the repository browser.