1 |
## ------------------------------------------------------------------------ |
2 |
## $Id: Simple.pm,v 1.1 2002/12/22 14:16:23 joko Exp $ |
3 |
## ------------------------------------------------------------------------ |
4 |
## $Log: Simple.pm,v $ |
5 |
## Revision 1.1 2002/12/22 14:16:23 joko |
6 |
## + initial check-in |
7 |
## |
8 |
## ------------------------------------------------------------------------ |
9 |
|
10 |
|
11 |
package Getopt::Simple; |
12 |
|
13 |
use strict; |
14 |
use warnings; |
15 |
|
16 |
|
17 |
use Data::Dumper; |
18 |
use Getopt::Long; |
19 |
|
20 |
my $opt; |
21 |
|
22 |
sub _cb_readOption { |
23 |
#my $self = shift; |
24 |
my $opt_name = shift; |
25 |
my $opt_value = shift; |
26 |
$opt_value ||= 1; |
27 |
$opt->{$opt_name} = $opt_value; |
28 |
#$self->{$opt_name} = $opt_value; |
29 |
} |
30 |
|
31 |
sub new { |
32 |
my $invocant = shift; |
33 |
my $class = ref($invocant) || $invocant; |
34 |
my $self = {}; |
35 |
bless $self, $class; |
36 |
|
37 |
#my $fields = shift; |
38 |
my @fields = @_; |
39 |
|
40 |
# build mapping (hash with argument as key and callback (CODEref) as value) |
41 |
$self->{__possible} = []; |
42 |
$self->{__available} = []; |
43 |
$self->{__result} = {}; |
44 |
$self->{__getopt_mapping} = []; |
45 |
#foreach (@$fields) { |
46 |
foreach (@fields) { |
47 |
|
48 |
my $key = $_; |
49 |
$key =~ s/=.*$//; |
50 |
push @{$self->{__possible}}, $key; |
51 |
|
52 |
|
53 |
#$option_mapping->{$_} = \&readOption; |
54 |
push @{$self->{__getopt_mapping}}, $_; |
55 |
push @{$self->{__getopt_mapping}}, \&_cb_readOption; |
56 |
} |
57 |
|
58 |
GetOptions(@{$self->{__getopt_mapping}}); |
59 |
|
60 |
foreach my $key (keys %{$opt}) { |
61 |
push @{$self->{__available}}, $key; |
62 |
$self->{__result}->{$key} = $opt->{$key}; |
63 |
# for convenience: store inside object itself, too |
64 |
$self->{$key} = $opt->{$key}; |
65 |
} |
66 |
return $self; |
67 |
} |
68 |
|
69 |
sub getOptions { |
70 |
my $self = shift; |
71 |
return $self->{__result}; |
72 |
} |
73 |
|
74 |
1; |