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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 joko 1.1 ## ------------------------------------------------------------------------
2 joko 1.3 ## $Id: Simple.pm,v 1.2 2002/12/23 04:27:03 joko Exp $
3 joko 1.1 ## ------------------------------------------------------------------------
4 joko 1.2 ## $Log: Simple.pm,v $
5 joko 1.3 ## Revision 1.2 2002/12/23 04:27:03 joko
6     ## + refactored, more oo-style now
7     ##
8 joko 1.2 ## Revision 1.1 2002/12/22 14:16:23 joko
9     ## + initial check-in
10     ##
11 joko 1.1 ## ------------------------------------------------------------------------
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 joko 1.2 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 joko 1.1 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 joko 1.3 my @fields;
42     foreach (@_) {
43     if (ref $_ eq 'HASH') {
44     $self->{__metadata} = $_;
45     } else {
46     push @fields, $_;
47     }
48     }
49 joko 1.1
50     # build mapping (hash with argument as key and callback (CODEref) as value)
51 joko 1.2 $self->{__possible} = [];
52     $self->{__available} = [];
53     $self->{__result} = {};
54     $self->{__getopt_mapping} = [];
55 joko 1.1 #foreach (@$fields) {
56     foreach (@fields) {
57 joko 1.2
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 joko 1.1 }
67    
68 joko 1.2 GetOptions(@{$self->{__getopt_mapping}});
69 joko 1.1
70     foreach my $key (keys %{$opt}) {
71 joko 1.2 push @{$self->{__available}}, $key;
72     $self->{__result}->{$key} = $opt->{$key};
73     # for convenience: store inside object itself, too
74 joko 1.1 $self->{$key} = $opt->{$key};
75     }
76 joko 1.3
77     $self->_checkRequired();
78    
79 joko 1.1 return $self;
80     }
81    
82 joko 1.3 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 joko 1.2 sub getOptions {
92     my $self = shift;
93     return $self->{__result};
94 joko 1.3 }
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 joko 1.1 }
106    
107     1;

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