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 / Args.pm < prev    next >
Encoding:
Perl POD Document  |  2003-04-29  |  7.3 KB  |  250 lines

  1. #============================================================================
  2. #
  3. # AppConfig::Args.pm
  4. #
  5. # Perl5 module to read command line argument and update the variable 
  6. # values in an AppConfig::State object accordingly.
  7. #
  8. # Written by Andy Wardley <abw@wardley.org>
  9. #
  10. # Copyright (C) 1997-2003 Andy Wardley.  All Rights Reserved.
  11. # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
  12. #
  13. # $Id: Args.pm,v 1.60 2003/04/29 10:42:39 abw Exp $
  14. #
  15. #============================================================================
  16.  
  17. package AppConfig::Args;
  18.  
  19. require 5.004;
  20. use AppConfig::State;
  21. use strict;
  22. use vars qw( $VERSION );
  23.  
  24. $VERSION = sprintf("%d.%02d", q$Revision: 1.60 $ =~ /(\d+)\.(\d+)/);
  25.  
  26.  
  27. #------------------------------------------------------------------------
  28. # new($state, \@args)
  29. #
  30. # Module constructor.  The first, mandatory parameter should be a 
  31. # reference to an AppConfig::State object to which all actions should 
  32. # be applied.  The second parameter may be a reference to a list of 
  33. # command line arguments.  This list reference is passed to args() for
  34. # processing.
  35. #
  36. # Returns a reference to a newly created AppConfig::Args object.
  37. #------------------------------------------------------------------------
  38.  
  39. sub new {
  40.     my $class = shift;
  41.     my $state = shift;
  42.     
  43.  
  44.     my $self = {
  45.         STATE    => $state,                # AppConfig::State ref
  46.     DEBUG    => $state->_debug(),      # store local copy of debug
  47.     PEDANTIC => $state->_pedantic,     # and pedantic flags
  48.     };
  49.  
  50.     bless $self, $class;
  51.     
  52.     # call parse() to parse any arg list passed 
  53.     $self->parse(shift)
  54.     if @_;
  55.  
  56.     return $self;
  57. }
  58.  
  59.  
  60. #------------------------------------------------------------------------
  61. # parse(\@args)
  62. #
  63. # Examines the argument list and updates the contents of the 
  64. # AppConfig::State referenced by $self->{ STATE } accordingly.  If 
  65. # no argument list is provided then the method defaults to examining 
  66. # @ARGV.  The method reports any warning conditions (such as undefined
  67. # variables) by calling $self->{ STATE }->_error() and then continues to
  68. # examine the rest of the list.  If the PEDANTIC option is set in the
  69. # AppConfig::State object, this behaviour is overridden and the method
  70. # returns 0 immediately on any parsing error.
  71. #
  72. # Returns 1 on success or 0 if one or more warnings were raised.
  73. #------------------------------------------------------------------------
  74.  
  75. sub parse {
  76.     my $self = shift;
  77.     my $argv = shift || \@ARGV;
  78.     my $warnings = 0;
  79.     my ($arg, $nargs, $variable, $value);
  80.  
  81.  
  82.     # take a local copy of the state to avoid much hash dereferencing
  83.     my ($state, $debug, $pedantic) = @$self{ qw( STATE DEBUG PEDANTIC ) };
  84.  
  85.  
  86.  
  87.     # loop around arguments
  88.     ARG: while (@$argv && $argv->[0] =~ /^-/) {
  89.     $arg = shift(@$argv);
  90.  
  91.     # '--' indicates the end of the options
  92.     last if $arg eq '--';
  93.  
  94.     # strip leading '-';
  95.     ($variable = $arg) =~ s/^-(-)?//;
  96.  
  97.     # test for '--' prefix and push back any '=value' item
  98.     if (defined $1) {
  99.         ($variable, $value) = split(/=/, $variable);
  100.         unshift(@$argv, $value) if defined $value;
  101.     }
  102.  
  103.     # check the variable exists
  104.     if ($state->_exists($variable)) {
  105.  
  106.         # see if it expects any mandatory arguments
  107.         $nargs = $state->_argcount($variable);
  108.         if ($nargs) {
  109.         # check there's another arg and it's not another '-opt'
  110.         if(defined($argv->[0])) {
  111.             $value = shift(@$argv);
  112.         }
  113.         else {
  114.             $state->_error("$arg expects an argument");
  115.             $warnings++;
  116.             last ARG if $pedantic;
  117.             next;
  118.         }
  119.         }
  120.         else {
  121.         # set a value of 1 if option doesn't expect an argument
  122.         $value = 1;
  123.         }
  124.  
  125.         # set the variable with the new value
  126.         $state->set($variable, $value);
  127.     }
  128.     else {
  129.         $state->_error("$arg: invalid option");
  130.         $warnings++;
  131.         last ARG if $pedantic;
  132.     }
  133.     }
  134.  
  135.     # return status
  136.     return $warnings ? 0 : 1;
  137. }
  138.  
  139.  
  140.  
  141. 1;
  142.  
  143. __END__
  144.  
  145. =head1 NAME
  146.  
  147. AppConfig::Args - Perl5 module for reading command line arguments.
  148.  
  149. =head1 SYNOPSIS
  150.  
  151.     use AppConfig::Args;
  152.  
  153.     my $state   = AppConfig::State->new(\%cfg);
  154.     my $cfgargs = AppConfig::Args->new($state);
  155.  
  156.     $cfgargs->parse(\@args);            # read args
  157.  
  158. =head1 OVERVIEW
  159.  
  160. AppConfig::Args is a Perl5 module which reads command line arguments and 
  161. uses the options therein to update variable values in an AppConfig::State 
  162. object.
  163.  
  164. AppConfig::File is distributed as part of the AppConfig bundle.
  165.  
  166. =head1 DESCRIPTION
  167.  
  168. =head2 USING THE AppConfig::Args MODULE
  169.  
  170. To import and use the AppConfig::Args module the following line should appear
  171. in your Perl script:
  172.  
  173.     use AppConfig::Args;
  174.  
  175. AppConfig::Args is used automatically if you use the AppConfig module 
  176. and create an AppConfig::Args object through the parse() method.
  177.       
  178. AppConfig::File is implemented using object-oriented methods.  A new 
  179. AppConfig::Args object is created and initialised using the new() method.
  180. This returns a reference to a new AppConfig::File object.  A reference to
  181. an AppConfig::State object should be passed in as the first parameter:
  182.        
  183.     my $state   = AppConfig::State->new();
  184.     my $cfgargs = AppConfig::Args->new($state);
  185.  
  186. This will create and return a reference to a new AppConfig::Args object. 
  187.  
  188. =head2 PARSING COMMAND LINE ARGUMENTS
  189.  
  190. The C<parse()> method is used to read a list of command line arguments and 
  191. update the STATE accordingly.  A reference to the list of arguments should
  192. be passed in.
  193.  
  194.     $cfgargs->parse(\@ARGV);
  195.  
  196. If the method is called without a reference to an argument list then it
  197. will examine and manipulate @ARGV.
  198.  
  199. If the PEDANTIC option is turned off in the AppConfig::State object, any 
  200. parsing errors (invalid variables, unvalidated values, etc) will generate
  201. warnings, but not cause the method to return.  Having processed all
  202. arguments, the method will return 1 if processed without warning or 0 if
  203. one or more warnings were raised.  When the PEDANTIC option is turned on,
  204. the method generates a warning and immediately returns a value of 0 as soon
  205. as it encounters any parsing error.
  206.  
  207. The method continues parsing arguments until it detects the first one that
  208. does not start with a leading dash, '-'.  Arguments that constitute values
  209. for other options are not examined in this way.
  210.  
  211. =head1 FUTURE DEVELOPMENT
  212.  
  213. This module was developed to provide backwards compatibility (to some 
  214. degree) with the preceeding App::Config module.  The argument parsing 
  215. it provides is basic but offers a quick and efficient solution for those
  216. times when simple option handling is all that is required.
  217.  
  218. If you require more flexibility in parsing command line arguments, then 
  219. you should consider using the AppConfig::Getopt module.  This is loaded 
  220. and used automatically by calling the AppConfig getopt() method.
  221.  
  222. The AppConfig::Getopt module provides considerably extended functionality 
  223. over the AppConfig::Args module by delegating out the task of argument 
  224. parsing to Johan Vromans' Getopt::Long module.  For advanced command-line 
  225. parsing, this module (either Getopt::Long by itself, or in conjunction with 
  226. AppConfig::Getopt) is highly recommended.
  227.  
  228. =head1 AUTHOR
  229.  
  230. Andy Wardley, E<lt>abw@wardley.orgE<gt>
  231.  
  232. =head1 REVISION
  233.  
  234. $Revision: 1.60 $
  235.  
  236. =head1 COPYRIGHT
  237.  
  238. Copyright (C) 1997-2003 Andy Wardley.  All Rights Reserved.
  239.  
  240. Copyright (C) 1997,1998 Canon Research Centre Europe Ltd.
  241.  
  242. This module is free software; you can redistribute it and/or modify it 
  243. under the same terms as Perl itself.
  244.  
  245. =head1 SEE ALSO
  246.  
  247. AppConfig, AppConfig::State, AppConfig::Getopt, Getopt::Long
  248.  
  249. =cut
  250.