2 |
## $Id$ |
## $Id$ |
3 |
## ------------------------------------------------------------------------ |
## ------------------------------------------------------------------------ |
4 |
## $Log$ |
## $Log$ |
5 |
|
## Revision 1.3 2002/12/27 16:06:55 joko |
6 |
|
## + sub _checkRequired |
7 |
|
## + sub getPossibleOptionKeys |
8 |
|
## + sub get |
9 |
|
## |
10 |
|
## Revision 1.2 2002/12/23 04:27:03 joko |
11 |
|
## + refactored, more oo-style now |
12 |
|
## |
13 |
## Revision 1.1 2002/12/22 14:16:23 joko |
## Revision 1.1 2002/12/22 14:16:23 joko |
14 |
## + initial check-in |
## + initial check-in |
15 |
## |
## |
27 |
|
|
28 |
my $opt; |
my $opt; |
29 |
|
|
30 |
|
sub _cb_readOption { |
31 |
|
#my $self = shift; |
32 |
|
my $opt_name = shift; |
33 |
|
my $opt_value = shift; |
34 |
|
$opt_value ||= 1; |
35 |
|
$opt->{$opt_name} = $opt_value; |
36 |
|
#$self->{$opt_name} = $opt_value; |
37 |
|
} |
38 |
|
|
39 |
sub new { |
sub new { |
40 |
my $invocant = shift; |
my $invocant = shift; |
41 |
my $class = ref($invocant) || $invocant; |
my $class = ref($invocant) || $invocant; |
43 |
bless $self, $class; |
bless $self, $class; |
44 |
|
|
45 |
#my $fields = shift; |
#my $fields = shift; |
46 |
my @fields = @_; |
my @fields; |
47 |
|
foreach (@_) { |
48 |
|
if (ref $_ eq 'HASH') { |
49 |
|
$self->{__metadata} = $_; |
50 |
|
} else { |
51 |
|
push @fields, $_; |
52 |
|
} |
53 |
|
} |
54 |
|
|
55 |
# build mapping (hash with argument as key and callback (CODEref) as value) |
# build mapping (hash with argument as key and callback (CODEref) as value) |
56 |
my $getopt_optionmapping = []; |
$self->{__possible} = []; |
57 |
|
$self->{__available} = []; |
58 |
|
$self->{__result} = {}; |
59 |
|
$self->{__getopt_mapping} = []; |
60 |
#foreach (@$fields) { |
#foreach (@$fields) { |
61 |
foreach (@fields) { |
foreach (@fields) { |
62 |
#$option_mapping->{$_} = \&rOpt; |
|
63 |
push @$getopt_optionmapping, $_; |
my $key = $_; |
64 |
push @$getopt_optionmapping, \&rOpt; |
$key =~ s/=.*$//; |
65 |
|
push @{$self->{__possible}}, $key; |
66 |
|
|
67 |
|
|
68 |
|
#$option_mapping->{$_} = \&readOption; |
69 |
|
push @{$self->{__getopt_mapping}}, $_; |
70 |
|
push @{$self->{__getopt_mapping}}, \&_cb_readOption; |
71 |
} |
} |
72 |
|
|
73 |
# V1: |
GetOptions(@{$self->{__getopt_mapping}}); |
|
# GetOptions( |
|
|
# 'force' => \&rOpt, |
|
|
# ); |
|
|
|
|
|
# V2: |
|
|
GetOptions(@$getopt_optionmapping); |
|
74 |
|
|
75 |
foreach my $key (keys %{$opt}) { |
foreach my $key (keys %{$opt}) { |
76 |
|
push @{$self->{__available}}, $key; |
77 |
|
$self->{__result}->{$key} = $opt->{$key}; |
78 |
|
# for convenience: store inside object itself, too |
79 |
$self->{$key} = $opt->{$key}; |
$self->{$key} = $opt->{$key}; |
80 |
} |
} |
81 |
|
|
82 |
|
$self->_checkRequired(); |
83 |
|
|
84 |
return $self; |
return $self; |
85 |
} |
} |
86 |
|
|
87 |
sub rOpt { |
sub _checkRequired { |
88 |
#my $self = shift; |
my $self = shift; |
89 |
my $opt_name = shift; |
foreach (keys %{$self->{__metadata}->{required}}) { |
90 |
my $opt_value = shift; |
if (!$self->{__result}->{$_}) { |
91 |
$opt_value ||= 1; |
$self->{__metadata}->{required}->{$_}->($self); |
92 |
$opt->{$opt_name} = $opt_value; |
} |
93 |
#$self->{$opt_name} = $opt_value; |
} |
94 |
|
} |
95 |
|
|
96 |
|
sub getOptions { |
97 |
|
my $self = shift; |
98 |
|
return $self->{__result}; |
99 |
|
} |
100 |
|
|
101 |
|
sub getPossibleOptionKeys { |
102 |
|
my $self = shift; |
103 |
|
return $self->{__possible}; |
104 |
|
} |
105 |
|
|
106 |
|
sub get { |
107 |
|
my $self = shift; |
108 |
|
my $key = shift; |
109 |
|
return $self->{$key}; |
110 |
} |
} |
111 |
|
|
112 |
1; |
1; |