--- nfo/perl/libs/Getopt/Simple.pm 2002/12/23 04:27:03 1.2 +++ nfo/perl/libs/Getopt/Simple.pm 2002/12/28 07:55:26 1.4 @@ -1,7 +1,15 @@ ## ------------------------------------------------------------------------ -## $Id: Simple.pm,v 1.2 2002/12/23 04:27:03 joko Exp $ +## $Id: Simple.pm,v 1.4 2002/12/28 07:55:26 joko Exp $ ## ------------------------------------------------------------------------ ## $Log: Simple.pm,v $ +## Revision 1.4 2002/12/28 07:55:26 joko +## - changed mechanism to read options +## +## Revision 1.3 2002/12/27 16:06:55 joko +## + sub _checkRequired +## + sub getPossibleOptionKeys +## + sub get +## ## Revision 1.2 2002/12/23 04:27:03 joko ## + refactored, more oo-style now ## @@ -23,7 +31,12 @@ my $opt; sub _cb_readOption { + #my $self = shift; + + #print Dumper(@_); + #return; + my $opt_name = shift; my $opt_value = shift; $opt_value ||= 1; @@ -38,7 +51,15 @@ bless $self, $class; #my $fields = shift; - my @fields = @_; + my @fields; + foreach (@_) { + #print ref $_, "\n"; + if (ref $_ eq 'HASH') { + $self->{__metadata} = $_; + } else { + push @fields, $_; + } + } # build mapping (hash with argument as key and callback (CODEref) as value) $self->{__possible} = []; @@ -59,6 +80,8 @@ } GetOptions(@{$self->{__getopt_mapping}}); + #Getopt::Long::Configure("permute"); + #GetOptions("<>" => \&_cb_readOption); foreach my $key (keys %{$opt}) { push @{$self->{__available}}, $key; @@ -66,12 +89,38 @@ # for convenience: store inside object itself, too $self->{$key} = $opt->{$key}; } + + $self->_checkRequired(); + return $self; } +sub _checkRequired { + my $self = shift; + foreach my $entry (@{$self->{__metadata}->{required}}) { + my $optionkey = $entry->[0]; + my $coderef = $entry->[1]; + if (!$self->{__result}->{$optionkey}) { + #$self->{__metadata}->{required}->{$_}->($self); + $coderef->($self); + } + } +} + sub getOptions { my $self = shift; return $self->{__result}; } +sub getPossibleOptionKeys { + my $self = shift; + return $self->{__possible}; +} + +sub get { + my $self = shift; + my $key = shift; + return $self->{$key}; +} + 1;