forked from yfpeng/pengyifan-bibtexformat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GetParameters.pm
372 lines (306 loc) · 13.3 KB
/
GetParameters.pm
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
package GetParameters;
####################################################################################################
#
# Package: GetParameters
#
# Author: Benjamin Bulheller
#
# Website: www.bulheller.com
#
# Mail address: webmaster.-at-.bulheller.com
#
# Version: $Revision: 4552 $, $Date: 2009-06-06 08:55:42 +0200 (Sat, 06 Jun 2009) $
#
# Date: November 2005
#
# Licence: This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see http://www.gnu.org/licenses/.
#
####################################################################################################
use strict; # always use this!!!
use Data::Dumper; # for easy printout of arrays and hashes
require Exporter;
our @ISA = qw( Exporter );
our @EXPORT = qw( GetParameters );
our $ERROR = "\nERROR (GetParameters.pm):";
####################################################################################################
sub GetParameters {
my @Parameters; # hold the keys of the given Parameter Hash, i.e. all possible defined parameters
my ($CurPar, $Boolean, $String, $File);
my ($ParameterHash, $Help, $OptionsHash);
my (@FileList, $WasGiven);
if (scalar @_ < 3) { # if less than 3 parameters are passed to the sub routine
print STDERR "$ERROR The command line parameters or the help message is not defined.\n\n";
exit 100;
} # of if (scalar(@_) < 2)
$ParameterHash = shift; # the definitions of the parameters (that is, which are possible, and what types they are
$OptionsHash = shift; # the hash that is filled with the parsed arguments
$Help = shift; # the help message
# if -h is not defined as a program parameter, use is as help request and check for it
if (not $ParameterHash->{h}) {
foreach (@ARGV) { # if -h parameters is given, display help screen
if ($_ =~ m/^-h/) {
print STDERR $Help;
exit 101;
}
} # of foreach
}
# check whether the "rest" array is defined (in which everything without a preceeding
# -option is collected). This has to be a list.
if (defined $ParameterHash->{rest} and $ParameterHash->{rest} !~ m/list/) {
print STDERR "$ERROR If the 'rest' parameter is defined it has to be a list.\n\n";
exit 113;
}
# display help if no parameter is given at all
if (not @ARGV) {
print STDERR $Help;
exit 102;
}
# parse the parameters
until (not @ARGV) {
$CurPar = shift @ARGV;
# if the parameters end with =0 or =1
if ($CurPar =~ m/=[01]$/) {
# set $Boolean to the last number after the "="
$Boolean = substr $CurPar, (length $CurPar)-1;
# remove the =[01]
$CurPar =~ s/=[01]//;
}
else {
$Boolean = undef;
}
if ( ($CurPar =~ m/^-/) and ($CurPar !~ m/^-\d/) # if the argument begins with a dash but no number follows
or ($ParameterHash->{rest}) ) # or the {rest} key is defined and this parameter is going to go in there
{
# print " ===> Current parameter: $CurPar\n";
# If the read parameter in $CurPar does not begin with "-" or is a negative number (-1.3456)
# it is going to be added to the {rest} array. If {rest} was predefined as e.g. a integerlist
# or a filelist then the type of this parameter has to be checked for being an integer or a
# file. In order to go through the following checks, it has to be faked that the user actually
# gave this parameter after a -rest option and $CurPar is added to the beginning of @ARGV again
# where it will be read further down
if ($ParameterHash->{rest} and ($CurPar !~ m/^-/ or $CurPar =~ m/^-\d/) ) {
unshift @ARGV, $CurPar; # add it again to the beginning of @ARGV
$CurPar = "-rest"; # "fake" the -rest option
}
$CurPar =~ s/^-//; # remove the leading dash
# the "--" parameter is only used to end list input
if ($CurPar eq "-") { next }
# if parameter has not been defined in the options hash
if (not $ParameterHash->{$CurPar}) {
print STDERR "$ERROR -$CurPar is not a defined Parameter!\n\n";
print STDERR $Help;
exit 103;
}
if ($ParameterHash->{$CurPar} =~ m/^switch\*?$/) {
# if =0 or =1 was given, set the parameter to the chosen value, otherwise the switch is "true"
if (defined $Boolean) {
$OptionsHash->{$CurPar} = $Boolean;
$OptionsHash->{GivenParams}{$CurPar} = $Boolean;
}
else {
$OptionsHash->{$CurPar} = 1;
$OptionsHash->{GivenParams}{$CurPar} = 1;
}
}
elsif ($ParameterHash->{"$CurPar"} =~ m/^(string|integer|real|file)?list/) {
# Parameters given via the command line overwrite default
# parameters. It needs to be checked, whether default array
# elements had been declared, which will be erased if this
# parameter is given. On the other hand, list parameters may also
# be split, e.g. "prog -l as er ty -f file -l fg cx" would result
# in a list "l" containing the five given elements. The
# parameter's hash entry in $WasGiven will be true, if it had been
# passed via the command line before, i.e. then existing elements
# of this array will be kept. If the entry is false, then existing
# values were given as default parameters and will be erased.
# delete default values
if (not $WasGiven->{$CurPar}) { $OptionsHash->{$CurPar} = [] }
# 'mark' this parameter, so that more elements can be added later without deleting these ones
$WasGiven->{$CurPar} = 1; # set this parameter true
@FileList = ();
# add the arguments following the list parameter to @FileList
# the -- parameter to end the list input is caught by the two regexes
until ( (not @ARGV) or ( ($ARGV[0] =~ m/^-/) and ($ARGV[0] !~ m/^-\d/) ) ) {
$String = shift (@ARGV); # until the next parameter appears or @ARGV is empty
if ($ParameterHash->{"$CurPar"} =~ m/^integerlist/) { &CheckInteger ($String, $CurPar) }
if ($ParameterHash->{"$CurPar"} =~ m/^reallist/) { &CheckReal ($String, $CurPar) }
if ($ParameterHash->{"$CurPar"} =~ m/^filelist/) { $String = &CheckFile ($String, $ParameterHash->{$CurPar}) }
push @FileList, $String;
} # of until (($ARGV[0] =~ m/^-/) or (not @ARGV))
push @{$OptionsHash->{$CurPar}}, @FileList;
push @{$OptionsHash->{GivenParams}{$CurPar}}, @FileList;
} # of elsif (${$ParameterHash}{$CurPar} eq "list")
elsif ($ParameterHash->{$CurPar} =~ m/^string\*?$/) {
$String = shift @ARGV; # read the parameter that followed -$CurPar
$OptionsHash->{$CurPar} = $String;
$OptionsHash->{GivenParams}{$CurPar} = $String;
}
elsif ($ParameterHash->{$CurPar} =~ m/^integer\*?$/) {
$String = shift @ARGV; # read the parameter that followed -$CurPar
&CheckInteger ($String, $CurPar);
$OptionsHash->{$CurPar} = $String;
$OptionsHash->{GivenParams}{$CurPar} = $String;
}
elsif ($ParameterHash->{$CurPar} =~ m/^real\*?$/) {
$String = shift @ARGV; # read the parameter that followed -$CurPar
&CheckReal ($String, $CurPar);
$OptionsHash->{$CurPar} = $String;
$OptionsHash->{GivenParams}{$CurPar} = $String;
}
elsif ($ParameterHash->{$CurPar} =~ m/^file(\[[^\]]+\])?\*?/) {
$File = shift @ARGV; # read the parameter that followed -$CurPar
$File = &CheckFile ($File, $ParameterHash->{$CurPar});
$OptionsHash->{$CurPar} = $File;
$OptionsHash->{GivenParams}{$CurPar} = $File;
# print Dumper $File;
}
else { # if the defintion of the parameter type is wrong (that is, not switch, string or list
print STDERR "\nParameter type \"", $ParameterHash->{"$CurPar"}, "\" illegal.\n";
print STDERR "Either \"switch\", \"string\" or \"list\" is expected.\n\n";
exit 104;
}
} # if ($CurPar =~ m/^-/)
else { # if the argument does not begin with a dash
push @{$OptionsHash->{rest}}, $CurPar;
push @{$OptionsHash->{GivenParams}{rest}}, $CurPar;
} # of else
} # of until (not @ARGV)
# check for missing mandatory parameters
foreach $CurPar (keys %{$ParameterHash}) {
# if the parameter type declaration contains an asterisk
if ($ParameterHash->{$CurPar} =~ m/\*/) {
# but the parameter was not given
if (not defined $OptionsHash->{$CurPar}) {
if ($CurPar eq "rest") {
print STDERR "\nAdditional values (without any parameter or switch) are required.\n$Help";
}
else {
print STDERR "\nMandatory parameter -$CurPar not given!\n$Help";
}
exit 105;
}
}
} # of foreach $CurPar (keys %{$ParameterHash})
# check for min, max numbers of elements in lists
foreach $CurPar (keys %{$ParameterHash}) {
# if the parameter type declaration defines a list
if ($ParameterHash->{$CurPar} =~ m/(integer|real|string|file)?list/) {
# if the definition contains a min/max-value or range
if ($ParameterHash->{$CurPar} =~ m/\[(\d+)?(,)?(\d+)?\]/) {
# save the backreferences of the RegEx
my $Min = $1; if (not defined $Min) { $Min = 0 }
my $Range = $2; # true if a comma was given, false otherwise
my $Max = $3;
if (defined $Max and $Max == 0) {
print STDERR "\nERROR: For the parameter -$CurPar a maximum amount of 0 options was defined.\n";
print STDERR " This has to be greater than zero.\n\n";
exit 140;
}
if (not defined $Max) { $Max = 0 }
my $Size;
# determine the size of the array in question
if (defined $OptionsHash->{$CurPar}) {
$Size = scalar @{$OptionsHash->{$CurPar}};
}
else {
next;
}
if (not $Range and $Size != $Min) {
if ($Min == 1) { $Min = "1 option" }
else { $Min = "$Min options" }
if ($CurPar eq "rest") {
print STDERR "\nERROR: Only $Min (values without any parameter or switch) are allowed!\n$Help!";
}
else {
print STDERR "\nERROR: Parameter -$CurPar requires $Min!\n$Help!";
}
exit 108;
}
if ($Size < $Min) {
if ($Min == 1) { $Min = "1 option" }
else { $Min = "$Min options" }
if ($CurPar eq "rest") {
print STDERR "\nERROR: At least $Min (values without any parameter or switch) are required!\n$Help!";
}
else {
print STDERR "\nERROR: Parameter -$CurPar needs at least $Min!\n$Help!";
}
exit 106;
}
if ($Max > 0 and $Size > $Max) {
if ($Max == 1) { $Max = "1 option" }
else { $Max = "$Min options" }
if ($CurPar eq "rest") {
print STDERR "\nERROR: At most $Max (values without any parameter or switch) are allowed!\n$Help!";
}
else {
print STDERR "\nERROR: Parameter -$CurPar can have at most $Max!\n$Help!";
}
exit 107;
}
}
}
} # of foreach $CurPar (keys %{$ParameterHash})
return 1;
} # of sub GetParameters
sub CheckInteger { # checks whether a given string is an integer number
my $String = shift;
my $CurPar = shift;
if (not defined $String or $String !~ m/^-{0,1}\d+$/) {
print STDERR "$ERROR -$CurPar must be an integer!\n\n";
exit 109;
}
} # of sub CheckInteger
sub CheckReal { # checks whether a given string is a real number
my $String = shift;
my $CurPar = shift;
if (not defined $String or $String !~ m/^-{0,1}\d*\.{0,1}\d+$/) {
print STDERR "$ERROR -$CurPar must be a real number!\n\n";
exit 110;
}
} # of sub CheckReal
sub CheckFile { # checks for a given file using multiple extensions if given
my $File = shift;
my $CurPar = shift;
my ($Extension, @Extensions);
if (-f $File) { # if the file was found
return $File;
}
# if the file was not found but extensions were given,
# containing "{" and "}" with multiple characters (not "}") in between
elsif ($CurPar =~ m/\{[^\}]+\}/) {
# cut out everything between { and }, syntax is substr (string, startpos, length)
$Extension = substr $CurPar, # the parameter definition
index ($CurPar, "{")+1, # the position of the first "{" (including it)
# the position of the last "}" again the position of the first "{"
rindex ($CurPar, "}") - (index ($CurPar, "{")+1);
# the last line is the number of chars between { and } ("lenght" of the string)
# split the string into multiple extensions, separates by "|" (this also works with only one extension)
@Extensions = split /\|/, $Extension;
foreach $Extension ( @Extensions ) {
# if the file is found with the current extension
if (-f "$File.$Extension") {
return "$File.$Extension";
}
}
}
# if the file was not found and no extensions were defined
print STDERR "$ERROR File $File not found.\n";
if (@Extensions) {
print STDERR " The following extensions were tested: ", join (", ", @Extensions), "\n";
}
print "\n";
exit 114;
} # of sub CheckFile
1;