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

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

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1.1 by joko, Sun Dec 22 14:16:23 2002 UTC revision 1.2 by joko, Mon Dec 23 04:27:03 2002 UTC
# Line 2  Line 2 
2  ##  $Id$  ##  $Id$
3  ## ------------------------------------------------------------------------  ## ------------------------------------------------------------------------
4  ##  $Log$  ##  $Log$
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  ##  Revision 1.1  2002/12/22 14:16:23  joko
9  ##  + initial check-in  ##  + initial check-in
10  ##  ##
# Line 19  use Getopt::Long; Line 22  use Getopt::Long;
22    
23  my $opt;  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 {  sub new {
35    my $invocant = shift;    my $invocant = shift;
36    my $class = ref($invocant) || $invocant;    my $class = ref($invocant) || $invocant;
# Line 29  sub new { Line 41  sub new {
41    my @fields = @_;    my @fields = @_;
42        
43    # build mapping (hash with argument as key and callback (CODEref) as value)    # build mapping (hash with argument as key and callback (CODEref) as value)
44    my $getopt_optionmapping = [];    $self->{__possible} = [];
45      $self->{__available} = [];
46      $self->{__result} = {};
47      $self->{__getopt_mapping} = [];
48    #foreach (@$fields) {    #foreach (@$fields) {
49    foreach (@fields) {    foreach (@fields) {
50      #$option_mapping->{$_} = \&rOpt;  
51      push @$getopt_optionmapping, $_;      my $key = $_;
52      push @$getopt_optionmapping, \&rOpt;      $key =~ s/=.*$//;
53        push @{$self->{__possible}}, $key;
54    
55    
56        #$option_mapping->{$_} = \&readOption;
57        push @{$self->{__getopt_mapping}}, $_;
58        push @{$self->{__getopt_mapping}}, \&_cb_readOption;
59    }    }
60        
61    # V1:    GetOptions(@{$self->{__getopt_mapping}});
   # GetOptions(  
   #  'force' => \&rOpt,  
   # );  
     
   # V2:  
   GetOptions(@$getopt_optionmapping);  
62        
63    foreach my $key (keys %{$opt}) {    foreach my $key (keys %{$opt}) {
64        push @{$self->{__available}}, $key;
65        $self->{__result}->{$key} = $opt->{$key};
66        # for convenience: store inside object itself, too
67      $self->{$key} = $opt->{$key};      $self->{$key} = $opt->{$key};
68    }    }
69    return $self;    return $self;
70  }  }
71    
72  sub rOpt {  sub getOptions {
73    #my $self = shift;    my $self = shift;
74    my $opt_name = shift;    return $self->{__result};
   my $opt_value = shift;  
   $opt_value ||= 1;  
   $opt->{$opt_name} = $opt_value;  
   #$self->{$opt_name} = $opt_value;  
75  }  }
76    
77  1;  1;

Legend:
Removed from v.1.1  
changed lines
  Added in v.1.2

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