1 |
joko |
1.1 |
## ------------------------------------------------------------------------ |
2 |
|
|
## $Id$ |
3 |
|
|
## ------------------------------------------------------------------------ |
4 |
|
|
## $Log$ |
5 |
|
|
## ------------------------------------------------------------------------ |
6 |
|
|
|
7 |
|
|
|
8 |
|
|
package Getopt::Simple; |
9 |
|
|
|
10 |
|
|
use strict; |
11 |
|
|
use warnings; |
12 |
|
|
|
13 |
|
|
|
14 |
|
|
use Data::Dumper; |
15 |
|
|
use Getopt::Long; |
16 |
|
|
|
17 |
|
|
my $opt; |
18 |
|
|
|
19 |
|
|
sub new { |
20 |
|
|
my $invocant = shift; |
21 |
|
|
my $class = ref($invocant) || $invocant; |
22 |
|
|
my $self = {}; |
23 |
|
|
bless $self, $class; |
24 |
|
|
|
25 |
|
|
#my $fields = shift; |
26 |
|
|
my @fields = @_; |
27 |
|
|
|
28 |
|
|
# build mapping (hash with argument as key and callback (CODEref) as value) |
29 |
|
|
my $getopt_optionmapping = []; |
30 |
|
|
#foreach (@$fields) { |
31 |
|
|
foreach (@fields) { |
32 |
|
|
#$option_mapping->{$_} = \&rOpt; |
33 |
|
|
push @$getopt_optionmapping, $_; |
34 |
|
|
push @$getopt_optionmapping, \&rOpt; |
35 |
|
|
} |
36 |
|
|
|
37 |
|
|
# V1: |
38 |
|
|
# GetOptions( |
39 |
|
|
# 'force' => \&rOpt, |
40 |
|
|
# ); |
41 |
|
|
|
42 |
|
|
# V2: |
43 |
|
|
GetOptions(@$getopt_optionmapping); |
44 |
|
|
|
45 |
|
|
foreach my $key (keys %{$opt}) { |
46 |
|
|
$self->{$key} = $opt->{$key}; |
47 |
|
|
} |
48 |
|
|
return $self; |
49 |
|
|
} |
50 |
|
|
|
51 |
|
|
sub rOpt { |
52 |
|
|
#my $self = shift; |
53 |
|
|
my $opt_name = shift; |
54 |
|
|
my $opt_value = shift; |
55 |
|
|
$opt_value ||= 1; |
56 |
|
|
$opt->{$opt_name} = $opt_value; |
57 |
|
|
#$self->{$opt_name} = $opt_value; |
58 |
|
|
} |
59 |
|
|
|
60 |
|
|
1; |