line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
package Getopt::Std::WithCheck;
|
2
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
21167
|
use 5.008004;
|
|
1
|
|
|
|
|
4
|
|
|
1
|
|
|
|
|
40
|
|
4
|
1
|
|
|
1
|
|
5
|
use strict;
|
|
1
|
|
|
|
|
2
|
|
|
1
|
|
|
|
|
35
|
|
5
|
1
|
|
|
1
|
|
6
|
use warnings;
|
|
1
|
|
|
|
|
5
|
|
|
1
|
|
|
|
|
164
|
|
6
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
require Exporter;
|
8
|
|
|
|
|
|
|
|
9
|
|
|
|
|
|
|
our @ISA = qw(Exporter);
|
10
|
|
|
|
|
|
|
|
11
|
|
|
|
|
|
|
# Items to export into callers namespace by default. Note: do not export
|
12
|
|
|
|
|
|
|
# names by default without a very good reason. Use EXPORT_OK instead.
|
13
|
|
|
|
|
|
|
# Do not simply export all your public functions/methods/constants.
|
14
|
|
|
|
|
|
|
|
15
|
|
|
|
|
|
|
# This allows declaration use Getopt::Std::WithCheck ':all';
|
16
|
|
|
|
|
|
|
# If you do not need this, moving things directly into @EXPORT or @EXPORT_OK
|
17
|
|
|
|
|
|
|
# will save memory.
|
18
|
|
|
|
|
|
|
our %EXPORT_TAGS = ( 'all' => [ qw(
|
19
|
|
|
|
|
|
|
|
20
|
|
|
|
|
|
|
) ] );
|
21
|
|
|
|
|
|
|
|
22
|
|
|
|
|
|
|
our @EXPORT_OK = ( @{ $EXPORT_TAGS{'all'} } );
|
23
|
|
|
|
|
|
|
|
24
|
|
|
|
|
|
|
our @EXPORT = qw(
|
25
|
|
|
|
|
|
|
|
26
|
|
|
|
|
|
|
);
|
27
|
|
|
|
|
|
|
|
28
|
|
|
|
|
|
|
our $VERSION = '0.04';
|
29
|
|
|
|
|
|
|
|
30
|
|
|
|
|
|
|
##############################################################################
|
31
|
|
|
|
|
|
|
## Some modules required #####################################################
|
32
|
|
|
|
|
|
|
##############################################################################
|
33
|
1
|
|
|
1
|
|
2655
|
use Getopt::Std;
|
|
1
|
|
|
|
|
48
|
|
|
1
|
|
|
|
|
1262
|
|
34
|
|
|
|
|
|
|
|
35
|
|
|
|
|
|
|
##############################################################################
|
36
|
|
|
|
|
|
|
## Ok, methods here ##########################################################
|
37
|
|
|
|
|
|
|
##############################################################################
|
38
|
|
|
|
|
|
|
|
39
|
|
|
|
|
|
|
my $usage = '';
|
40
|
|
|
|
|
|
|
|
41
|
|
|
|
|
|
|
my $checkHArg = sub
|
42
|
|
|
|
|
|
|
{
|
43
|
|
|
|
|
|
|
if ($_[0])
|
44
|
|
|
|
|
|
|
{
|
45
|
|
|
|
|
|
|
print STDERR usage();
|
46
|
|
|
|
|
|
|
exit 0;
|
47
|
|
|
|
|
|
|
};
|
48
|
|
|
|
|
|
|
return 0;
|
49
|
|
|
|
|
|
|
};
|
50
|
|
|
|
|
|
|
|
51
|
|
|
|
|
|
|
my $checkHOpt = sub
|
52
|
|
|
|
|
|
|
{
|
53
|
|
|
|
|
|
|
my ($opts) = @_;
|
54
|
|
|
|
|
|
|
|
55
|
|
|
|
|
|
|
foreach my $opt (@{$opts})
|
56
|
|
|
|
|
|
|
{
|
57
|
|
|
|
|
|
|
if ($opt->[0] eq 'h')
|
58
|
|
|
|
|
|
|
{ return; };
|
59
|
|
|
|
|
|
|
};
|
60
|
|
|
|
|
|
|
unshift(@{$opts}, ['h', 0, 0, 'Print help notice, and exit', $checkHArg]);
|
61
|
|
|
|
|
|
|
};
|
62
|
|
|
|
|
|
|
|
63
|
|
|
|
|
|
|
my $addLF = sub
|
64
|
|
|
|
|
|
|
{ return $_[0].($_[0] =~ m/\n$/ ? '' : "\n"); };
|
65
|
|
|
|
|
|
|
|
66
|
|
|
|
|
|
|
my $makeOpts = sub
|
67
|
|
|
|
|
|
|
{
|
68
|
|
|
|
|
|
|
my ($opts) = @_;
|
69
|
|
|
|
|
|
|
|
70
|
|
|
|
|
|
|
my $result = '';
|
71
|
|
|
|
|
|
|
foreach my $opt (@{$opts})
|
72
|
|
|
|
|
|
|
{ $result .= $opt->[0].($opt->[1] ? ':' : ''); };
|
73
|
|
|
|
|
|
|
return $result;
|
74
|
|
|
|
|
|
|
};
|
75
|
|
|
|
|
|
|
|
76
|
|
|
|
|
|
|
my $makeCfg = sub
|
77
|
|
|
|
|
|
|
{
|
78
|
|
|
|
|
|
|
my ($opts) = @_;
|
79
|
|
|
|
|
|
|
|
80
|
|
|
|
|
|
|
my %result = ();
|
81
|
|
|
|
|
|
|
foreach my $opt (@{$opts})
|
82
|
|
|
|
|
|
|
{ $result{$opt->[0]} = $opt->[2]; };
|
83
|
|
|
|
|
|
|
return \%result;
|
84
|
|
|
|
|
|
|
};
|
85
|
|
|
|
|
|
|
|
86
|
|
|
|
|
|
|
my $defaultCheck = sub
|
87
|
|
|
|
|
|
|
{ return $_[0]; };
|
88
|
|
|
|
|
|
|
|
89
|
|
|
|
|
|
|
my $makeChk = sub
|
90
|
|
|
|
|
|
|
{
|
91
|
|
|
|
|
|
|
my ($opts) = @_;
|
92
|
|
|
|
|
|
|
|
93
|
|
|
|
|
|
|
my %result = ();
|
94
|
|
|
|
|
|
|
foreach my $opt (@{$opts})
|
95
|
|
|
|
|
|
|
{ $result{$opt->[0]} = defined($opt->[4]) ? $opt->[4] : $defaultCheck; };
|
96
|
|
|
|
|
|
|
return %result;
|
97
|
|
|
|
|
|
|
};
|
98
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
my $makeUsage = sub
|
100
|
|
|
|
|
|
|
{
|
101
|
|
|
|
|
|
|
my ($opts) = @_;
|
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
my $result = '';
|
104
|
|
|
|
|
|
|
|
105
|
|
|
|
|
|
|
foreach my $opt (@{$opts})
|
106
|
|
|
|
|
|
|
{
|
107
|
|
|
|
|
|
|
$result .= sprintf("-%s: %s\t%s\n",
|
108
|
|
|
|
|
|
|
$opt->[0],
|
109
|
|
|
|
|
|
|
&{$addLF}($opt->[3]),
|
110
|
|
|
|
|
|
|
defined($opt->[2]) ? 'Default is \''.($opt->[1] ? $opt->[2] : ($opt->[2] ? 'Yes' : 'No')).'\'' : 'Parameter required'
|
111
|
|
|
|
|
|
|
);
|
112
|
|
|
|
|
|
|
};
|
113
|
|
|
|
|
|
|
return $result;
|
114
|
|
|
|
|
|
|
};
|
115
|
|
|
|
|
|
|
|
116
|
|
|
|
|
|
|
|
117
|
|
|
|
|
|
|
my $setUsage = sub
|
118
|
|
|
|
|
|
|
{
|
119
|
|
|
|
|
|
|
my ($name, $descr, $opts) = @_;
|
120
|
|
|
|
|
|
|
$usage = &{$addLF}($name).&{$addLF}($descr).&{$addLF}(&{$makeUsage}($opts)).($^W ? "\ngetops string is '".&{$makeOpts}($opts)."'\n" : '');
|
121
|
|
|
|
|
|
|
};
|
122
|
|
|
|
|
|
|
|
123
|
|
|
|
|
|
|
my $checkOpts = sub
|
124
|
|
|
|
|
|
|
{
|
125
|
|
|
|
|
|
|
my ($opts) = @_;
|
126
|
|
|
|
|
|
|
|
127
|
|
|
|
|
|
|
if (ref($opts) eq 'HASH')
|
128
|
|
|
|
|
|
|
{
|
129
|
|
|
|
|
|
|
my @tmpOpts = ();
|
130
|
|
|
|
|
|
|
while(my ($key, $val) = each(%{$opts}))
|
131
|
|
|
|
|
|
|
{
|
132
|
|
|
|
|
|
|
push(@tmpOpts, [$key,
|
133
|
|
|
|
|
|
|
$val->{'argument'},
|
134
|
|
|
|
|
|
|
$val->{'default'},
|
135
|
|
|
|
|
|
|
$val->{'description'},
|
136
|
|
|
|
|
|
|
$val->{'checkRoutine'},
|
137
|
|
|
|
|
|
|
]
|
138
|
|
|
|
|
|
|
);
|
139
|
|
|
|
|
|
|
};
|
140
|
|
|
|
|
|
|
$opts = \@tmpOpts;
|
141
|
|
|
|
|
|
|
};
|
142
|
|
|
|
|
|
|
|
143
|
|
|
|
|
|
|
&{$checkHOpt}($opts);
|
144
|
|
|
|
|
|
|
|
145
|
|
|
|
|
|
|
return $opts;
|
146
|
|
|
|
|
|
|
};
|
147
|
|
|
|
|
|
|
|
148
|
|
|
|
|
|
|
sub getOpts($$$)
|
149
|
|
|
|
|
|
|
{
|
150
|
0
|
|
|
0
|
1
|
|
my ($name, $descr, $opts) = @_;
|
151
|
|
|
|
|
|
|
|
152
|
0
|
|
|
|
|
|
my $self = undef;
|
153
|
|
|
|
|
|
|
|
154
|
0
|
|
|
|
|
|
$opts = &{$checkOpts}($opts);
|
|
0
|
|
|
|
|
|
|
155
|
|
|
|
|
|
|
|
156
|
0
|
|
|
|
|
|
&{$setUsage}($name, $descr, $opts);
|
|
0
|
|
|
|
|
|
|
157
|
|
|
|
|
|
|
|
158
|
0
|
|
|
|
|
|
my $conf = &{$makeCfg}($opts);
|
|
0
|
|
|
|
|
|
|
159
|
0
|
|
|
|
|
|
my %check = &{$makeChk}($opts);
|
|
0
|
|
|
|
|
|
|
160
|
|
|
|
|
|
|
|
161
|
0
|
|
|
|
|
|
Getopt::Std::getopts(&{$makeOpts}($opts), $conf);
|
|
0
|
|
|
|
|
|
|
162
|
|
|
|
|
|
|
|
163
|
0
|
|
|
|
|
|
foreach my $key (keys(%{$conf}))
|
|
0
|
|
|
|
|
|
|
164
|
|
|
|
|
|
|
{
|
165
|
0
|
0
|
|
|
|
|
if (!defined($conf->{$key}))
|
166
|
0
|
|
|
|
|
|
{ die "Required param '".$key."' is not set\n\n".usage(); };
|
167
|
|
|
|
|
|
|
|
168
|
0
|
|
|
|
|
|
$conf->{$key} = &{$check{$key}}($conf->{$key});
|
|
0
|
|
|
|
|
|
|
169
|
|
|
|
|
|
|
};
|
170
|
|
|
|
|
|
|
|
171
|
0
|
|
|
|
|
|
return $conf;
|
172
|
|
|
|
|
|
|
};
|
173
|
|
|
|
|
|
|
|
174
|
|
|
|
|
|
|
sub usage
|
175
|
|
|
|
|
|
|
{
|
176
|
0
|
|
|
0
|
1
|
|
my ($name, $descr, $opts) = @_;
|
177
|
0
|
0
|
|
|
|
|
if (scalar(@_) > 0)
|
178
|
|
|
|
|
|
|
{
|
179
|
0
|
|
|
|
|
|
$opts = &{$checkOpts}($opts);
|
|
0
|
|
|
|
|
|
|
180
|
0
|
|
|
|
|
|
&{$setUsage}($name, $descr, $opts);
|
|
0
|
|
|
|
|
|
|
181
|
|
|
|
|
|
|
};
|
182
|
0
|
|
|
|
|
|
return $usage;
|
183
|
|
|
|
|
|
|
};
|
184
|
|
|
|
|
|
|
|
185
|
|
|
|
|
|
|
|
186
|
|
|
|
|
|
|
1;
|
187
|
|
|
|
|
|
|
__END__
|