/[cvs]/nfo/perl/libs/Getopt/Simple.pm
ViewVC logotype

Contents of /nfo/perl/libs/Getopt/Simple.pm

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.3 - (show annotations)
Fri Dec 27 16:06:55 2002 UTC (21 years, 4 months ago) by joko
Branch: MAIN
Changes since 1.2: +35 -2 lines
+ sub _checkRequired
+ sub getPossibleOptionKeys
+ sub get

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

MailToCvsAdmin">MailToCvsAdmin
ViewVC Help
Powered by ViewVC 1.1.26 RSS 2.0 feed