home *** CD-ROM | disk | FTP | other *** search
/ PC Professionell 2004 December / PCpro_2004_12.ISO / files / webserver / tsw / TSW_3.4.0.exe / Apache2 / perl / Getopt.pm < prev    next >
Encoding:
Perl POD Document  |  2003-04-29  |  7.9 KB  |  282 lines

  1. #============================================================================
  2. #
  3. # AppConfig::Getopt.pm
  4. #
  5. # Perl5 module to interface AppConfig::* to Johan Vromans' Getopt::Long
  6. # module.  Getopt::Long implements the POSIX standard for command line
  7. # options, with GNU extensions, and also traditional one-letter options.
  8. # AppConfig::Getopt constructs the necessary Getopt:::Long configuration
  9. # from the internal AppConfig::State and delegates the parsing of command
  10. # line arguments to it.  Internal variable values are updated by callback
  11. # from GetOptions().
  12. # Written by Andy Wardley <abw@wardley.org>
  13. #
  14. # Copyright (C) 1997-2003 Andy Wardley.  All Rights Reserved.
  15. # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
  16. #
  17. # $Id: Getopt.pm,v 1.60 2003/04/29 10:43:21 abw Exp $
  18. #
  19. #============================================================================
  20.  
  21. package AppConfig::Getopt;
  22.  
  23. require 5.005;
  24. use AppConfig::State;
  25. use Getopt::Long 2.17;
  26. use strict;
  27. use vars qw( $VERSION );
  28.  
  29. $VERSION = sprintf("%d.%02d", q$Revision: 1.60 $ =~ /(\d+)\.(\d+)/);
  30.  
  31.  
  32. #------------------------------------------------------------------------
  33. # new($state, \@args)
  34. #
  35. # Module constructor.  The first, mandatory parameter should be a 
  36. # reference to an AppConfig::State object to which all actions should 
  37. # be applied.  The second parameter may be a reference to a list of 
  38. # command line arguments.  This list reference is passed to parse() for
  39. # processing.
  40. #
  41. # Returns a reference to a newly created AppConfig::Getopt object.
  42. #------------------------------------------------------------------------
  43.  
  44. sub new {
  45.     my $class = shift;
  46.     my $state = shift;
  47.  
  48.     
  49.     my $self = {
  50.         STATE => $state,
  51.    };
  52.  
  53.     bless $self, $class;
  54.     
  55.     # call parse() to parse any arg list passed 
  56.     $self->parse(@_)
  57.     if @_;
  58.  
  59.     return $self;
  60. }
  61.  
  62.  
  63. #------------------------------------------------------------------------
  64. # parse(@$config, \@args)
  65. #
  66. # Constructs the appropriate configuration information and then delegates
  67. # the task of processing command line options to Getopt::Long.
  68. #
  69. # Returns 1 on success or 0 if one or more warnings were raised.
  70. #------------------------------------------------------------------------
  71.  
  72. sub parse {
  73.     my $self  = shift;
  74.     my $state = $self->{ STATE };
  75.     my (@config, $args, $getopt);
  76.     
  77.     local $" = ', ';
  78.  
  79.     # we trap $SIG{__WARN__} errors and patch them into AppConfig::State
  80.     local $SIG{__WARN__} = sub {
  81.     my $msg = shift;
  82.  
  83.     # AppConfig::State doesn't expect CR terminated error messages
  84.     # and it uses printf, so we protect any embedded '%' chars 
  85.     chomp($msg);
  86.     $state->_error("%s", $msg);
  87.     };
  88.     
  89.     # slurp all config items into @config
  90.     push(@config, shift) while defined $_[0] && ! ref($_[0]);   
  91.  
  92.     # add debug status if appropriate (hmm...can't decide about this)
  93. #    push(@config, 'debug') if $state->_debug();
  94.  
  95.     # next parameter may be a reference to a list of args
  96.     $args = shift;
  97.  
  98.     # copy any args explicitly specified into @ARGV
  99.     @ARGV = @$args if defined $args;
  100.  
  101.     # we enclose in an eval block because constructor may die()
  102.     eval {
  103.     # configure Getopt::Long
  104.     Getopt::Long::Configure(@config);
  105.  
  106.     # construct options list from AppConfig::State variables
  107.     my @opts = $self->{ STATE   }->_getopt_state();
  108.  
  109.     # DEBUG
  110.     if ($state->_debug()) {
  111.         print STDERR "Calling GetOptions(@opts)\n";
  112.         print STDERR "\@ARGV = (@ARGV)\n";
  113.     };
  114.  
  115.     # call GetOptions() with specifications constructed from the state
  116.     $getopt = GetOptions(@opts);
  117.     };
  118.     if ($@) {
  119.     chomp($@);
  120.     $state->_error("%s", $@);
  121.     return 0;
  122.     }
  123.  
  124.     # udpdate any args reference passed to include only that which is left 
  125.     # in @ARGV
  126.     @$args = @ARGV if defined $args;
  127.  
  128.     return $getopt;
  129. }
  130.  
  131.  
  132. #========================================================================
  133. # AppConfig::State
  134. #========================================================================
  135.  
  136. package AppConfig::State;
  137.  
  138. #------------------------------------------------------------------------
  139. # _getopt_state()
  140. #
  141. # Constructs option specs in the Getopt::Long format for each variable 
  142. # definition.
  143. #
  144. # Returns a list of specification strings.
  145. #------------------------------------------------------------------------
  146.  
  147. sub _getopt_state {
  148.     my $self = shift;
  149.     my ($var, $spec, $args, $argcount, @specs);
  150.  
  151.     my $linkage = sub { $self->set(@_) };
  152.  
  153.     foreach $var (keys %{ $self->{ VARIABLE } }) {
  154.     $spec  = join('|', $var, @{ $self->{ ALIASES }->{ $var } || [ ] });
  155.  
  156.     # an ARGS value is used, if specified
  157.     unless (defined ($args = $self->{ ARGS }->{ $var })) {
  158.         # otherwise, construct a basic one from ARGCOUNT
  159.         ARGCOUNT: {
  160.         last ARGCOUNT unless 
  161.             defined ($argcount = $self->{ ARGCOUNT }->{ $var });
  162.  
  163.         $args = "=s",  last ARGCOUNT if $argcount eq ARGCOUNT_ONE;
  164.         $args = "=s@", last ARGCOUNT if $argcount eq ARGCOUNT_LIST;
  165.         $args = "=s%", last ARGCOUNT if $argcount eq ARGCOUNT_HASH;
  166.         $args = "!";
  167.         }
  168.     }
  169.     $spec .= $args if defined $args;
  170.  
  171.     push(@specs, $spec, $linkage);
  172.     }
  173.  
  174.     return @specs;
  175. }
  176.  
  177.  
  178.  
  179. 1;
  180.  
  181. __END__
  182.  
  183. =head1 NAME
  184.  
  185. AppConfig::Getopt - Perl5 module for processing command line arguments via delegation to Getopt::Long.
  186.  
  187. =head1 SYNOPSIS
  188.  
  189.     use AppConfig::Getopt;
  190.  
  191.     my $state  = AppConfig::State->new(\%cfg);
  192.     my $getopt = AppConfig::Getopt->new($state);
  193.  
  194.     $getopt->parse(\@args);            # read args
  195.  
  196. =head1 OVERVIEW
  197.  
  198. AppConfig::Getopt is a Perl5 module which delegates to Johan Vroman's
  199. Getopt::Long module to parse command line arguments and update values 
  200. in an AppConfig::State object accordingly.
  201.  
  202. AppConfig::Getopt is distributed as part of the AppConfig bundle.
  203.  
  204. =head1 DESCRIPTION
  205.  
  206. =head2 USING THE AppConfig::Getopt MODULE
  207.  
  208. To import and use the AppConfig::Getopt module the following line should appear
  209. in your Perl script:
  210.  
  211.     use AppConfig::Getopt;
  212.  
  213. AppConfig::Getopt is used automatically if you use the AppConfig module 
  214. and create an AppConfig::Getopt object through the getopt() method.
  215.       
  216. AppConfig::Getopt is implemented using object-oriented methods.  A new 
  217. AppConfig::Getopt object is created and initialised using the new() method.
  218. This returns a reference to a new AppConfig::Getopt object.  A reference to
  219. an AppConfig::State object should be passed in as the first parameter:
  220.        
  221.     my $state  = AppConfig::State->new();
  222.     my $getopt = AppConfig::Getopt->new($state);
  223.  
  224. This will create and return a reference to a new AppConfig::Getopt object. 
  225.  
  226. =head2 PARSING COMMAND LINE ARGUMENTS
  227.  
  228. The C<parse()> method is used to read a list of command line arguments and 
  229. update the state accordingly.  
  230.  
  231. The first (non-list reference) parameters may contain a number of 
  232. configuration strings to pass to Getopt::Long::Configure.  A reference 
  233. to a list of arguments may additionally be passed or @ARGV is used by 
  234. default.
  235.  
  236.     $getopt->parse();                       # uses @ARGV
  237.     $getopt->parse(\@myargs);
  238.     $getopt->parse(qw(auto_abbrev debug));  # uses @ARGV
  239.     $getopt->parse(qw(debug), \@myargs);
  240.  
  241. See Getopt::Long for details of the configuartion options available.
  242.  
  243. A Getopt::Long specification string is constructed for each variable 
  244. defined in the AppConfig::State.  This consists of the name, any aliases
  245. and the ARGS value for the variable.
  246.  
  247. These specification string are then passed to Getopt::Long, the arguments
  248. are parsed and the values in the AppConfig::State updated.
  249.  
  250. See AppConfig for information about using the AppConfig::Getopt
  251. module via the getopt() method.
  252.  
  253. =head1 AUTHOR
  254.  
  255. Andy Wardley, E<lt>abw@wardley.orgE<gt>
  256.  
  257. =head1 REVISION
  258.  
  259. $Revision: 1.60 $
  260.  
  261. =head1 COPYRIGHT
  262.  
  263. Copyright (C) 1997-2003 Andy Wardley.  All Rights Reserved.
  264.  
  265. Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
  266.  
  267. This module is free software; you can redistribute it and/or modify it 
  268. under the same terms as Perl itself.
  269.  
  270. =head1 ACKNOWLEDGMENTS
  271.  
  272. Many thanks are due to Johan Vromans for the Getopt::Long module.  He was 
  273. kind enough to offer assistance and access to early releases of his code to 
  274. enable this module to be written.
  275.  
  276. =head1 SEE ALSO
  277.  
  278. AppConfig, AppConfig::State, AppConfig::Args, Getopt::Long
  279.  
  280. =cut
  281.