/[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.4 - (show annotations)
Sat Dec 28 07:55:26 2002 UTC (21 years, 4 months ago) by joko
Branch: MAIN
CVS Tags: HEAD
Changes since 1.3: +20 -4 lines
- changed mechanism to read options

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

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