| line | stmt | bran | cond | sub | pod | time | code | 
| 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 | 
| 9 |  |  |  |  |  |  | # | 
| 10 |  |  |  |  |  |  | # Copyright (C) 1997-2007 Andy Wardley.  All Rights Reserved. | 
| 11 |  |  |  |  |  |  | # Copyright (C) 1997,1998 Canon Research Centre Europe Ltd. | 
| 12 |  |  |  |  |  |  | #============================================================================ | 
| 13 |  |  |  |  |  |  |  | 
| 14 |  |  |  |  |  |  | package AppConfig::Args; | 
| 15 | 1 |  |  | 1 |  | 388 | use strict; | 
|  | 1 |  |  |  |  | 2 |  | 
|  | 1 |  |  |  |  | 29 |  | 
| 16 | 1 |  |  | 1 |  | 3 | use warnings; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 21 |  | 
| 17 | 1 |  |  | 1 |  | 3 | use AppConfig::State; | 
|  | 1 |  |  |  |  | 1 |  | 
|  | 1 |  |  |  |  | 363 |  | 
| 18 |  |  |  |  |  |  | our $VERSION = '1.69'; | 
| 19 |  |  |  |  |  |  |  | 
| 20 |  |  |  |  |  |  |  | 
| 21 |  |  |  |  |  |  | #------------------------------------------------------------------------ | 
| 22 |  |  |  |  |  |  | # new($state, \@args) | 
| 23 |  |  |  |  |  |  | # | 
| 24 |  |  |  |  |  |  | # Module constructor.  The first, mandatory parameter should be a | 
| 25 |  |  |  |  |  |  | # reference to an AppConfig::State object to which all actions should | 
| 26 |  |  |  |  |  |  | # be applied.  The second parameter may be a reference to a list of | 
| 27 |  |  |  |  |  |  | # command line arguments.  This list reference is passed to args() for | 
| 28 |  |  |  |  |  |  | # processing. | 
| 29 |  |  |  |  |  |  | # | 
| 30 |  |  |  |  |  |  | # Returns a reference to a newly created AppConfig::Args object. | 
| 31 |  |  |  |  |  |  | #------------------------------------------------------------------------ | 
| 32 |  |  |  |  |  |  |  | 
| 33 |  |  |  |  |  |  | sub new { | 
| 34 | 1 |  |  | 1 | 0 | 9 | my $class = shift; | 
| 35 | 1 |  |  |  |  | 2 | my $state = shift; | 
| 36 |  |  |  |  |  |  |  | 
| 37 |  |  |  |  |  |  |  | 
| 38 | 1 |  |  |  |  | 4 | my $self = { | 
| 39 |  |  |  |  |  |  | STATE    => $state,                # AppConfig::State ref | 
| 40 |  |  |  |  |  |  | DEBUG    => $state->_debug(),      # store local copy of debug | 
| 41 |  |  |  |  |  |  | PEDANTIC => $state->_pedantic,     # and pedantic flags | 
| 42 |  |  |  |  |  |  | }; | 
| 43 |  |  |  |  |  |  |  | 
| 44 | 1 |  |  |  |  | 2 | bless $self, $class; | 
| 45 |  |  |  |  |  |  |  | 
| 46 |  |  |  |  |  |  | # call parse() to parse any arg list passed | 
| 47 | 1 | 50 |  |  |  | 9 | $self->parse(shift) | 
| 48 |  |  |  |  |  |  | if @_; | 
| 49 |  |  |  |  |  |  |  | 
| 50 | 1 |  |  |  |  | 2 | return $self; | 
| 51 |  |  |  |  |  |  | } | 
| 52 |  |  |  |  |  |  |  | 
| 53 |  |  |  |  |  |  |  | 
| 54 |  |  |  |  |  |  | #------------------------------------------------------------------------ | 
| 55 |  |  |  |  |  |  | # parse(\@args) | 
| 56 |  |  |  |  |  |  | # | 
| 57 |  |  |  |  |  |  | # Examines the argument list and updates the contents of the | 
| 58 |  |  |  |  |  |  | # AppConfig::State referenced by $self->{ STATE } accordingly.  If | 
| 59 |  |  |  |  |  |  | # no argument list is provided then the method defaults to examining | 
| 60 |  |  |  |  |  |  | # @ARGV.  The method reports any warning conditions (such as undefined | 
| 61 |  |  |  |  |  |  | # variables) by calling $self->{ STATE }->_error() and then continues to | 
| 62 |  |  |  |  |  |  | # examine the rest of the list.  If the PEDANTIC option is set in the | 
| 63 |  |  |  |  |  |  | # AppConfig::State object, this behaviour is overridden and the method | 
| 64 |  |  |  |  |  |  | # returns 0 immediately on any parsing error. | 
| 65 |  |  |  |  |  |  | # | 
| 66 |  |  |  |  |  |  | # Returns 1 on success or 0 if one or more warnings were raised. | 
| 67 |  |  |  |  |  |  | #------------------------------------------------------------------------ | 
| 68 |  |  |  |  |  |  |  | 
| 69 |  |  |  |  |  |  | sub parse { | 
| 70 | 4 |  |  | 4 | 0 | 422 | my $self = shift; | 
| 71 | 4 |  | 100 |  |  | 14 | my $argv = shift || \@ARGV; | 
| 72 | 4 |  |  |  |  | 4 | my $warnings = 0; | 
| 73 | 4 |  |  |  |  | 7 | my ($arg, $nargs, $variable, $value); | 
| 74 |  |  |  |  |  |  |  | 
| 75 |  |  |  |  |  |  |  | 
| 76 |  |  |  |  |  |  | # take a local copy of the state to avoid much hash dereferencing | 
| 77 | 4 |  |  |  |  | 10 | my ($state, $debug, $pedantic) = @$self{ qw( STATE DEBUG PEDANTIC ) }; | 
| 78 |  |  |  |  |  |  |  | 
| 79 |  |  |  |  |  |  | # loop around arguments | 
| 80 | 4 |  | 100 |  |  | 22 | ARG: while (@$argv && $argv->[0] =~ /^-/) { | 
| 81 | 8 |  |  |  |  | 8 | $arg = shift(@$argv); | 
| 82 |  |  |  |  |  |  |  | 
| 83 |  |  |  |  |  |  | # '--' indicates the end of the options | 
| 84 | 8 | 50 |  |  |  | 13 | last if $arg eq '--'; | 
| 85 |  |  |  |  |  |  |  | 
| 86 |  |  |  |  |  |  | # strip leading '-'; | 
| 87 | 8 |  |  |  |  | 23 | ($variable = $arg) =~ s/^-(-)?//; | 
| 88 |  |  |  |  |  |  |  | 
| 89 |  |  |  |  |  |  | # test for '--' prefix and push back any '=value' item | 
| 90 | 8 | 100 |  |  |  | 18 | if (defined $1) { | 
| 91 | 5 |  |  |  |  | 11 | ($variable, $value) = split(/=/, $variable); | 
| 92 | 5 | 100 |  |  |  | 10 | unshift(@$argv, $value) if defined $value; | 
| 93 |  |  |  |  |  |  | } | 
| 94 |  |  |  |  |  |  |  | 
| 95 |  |  |  |  |  |  | # check the variable exists | 
| 96 | 8 | 50 |  |  |  | 16 | if ($state->_exists($variable)) { | 
| 97 |  |  |  |  |  |  |  | 
| 98 |  |  |  |  |  |  | # see if it expects any mandatory arguments | 
| 99 | 8 |  |  |  |  | 30 | $nargs = $state->_argcount($variable); | 
| 100 | 8 | 100 |  |  |  | 12 | if ($nargs) { | 
| 101 |  |  |  |  |  |  | # check there's another arg and it's not another '-opt' | 
| 102 | 7 | 50 |  |  |  | 11 | if(defined($argv->[0])) { | 
| 103 | 7 |  |  |  |  | 9 | $value = shift(@$argv); | 
| 104 |  |  |  |  |  |  | } | 
| 105 |  |  |  |  |  |  | else { | 
| 106 | 0 |  |  |  |  | 0 | $state->_error("$arg expects an argument"); | 
| 107 | 0 |  |  |  |  | 0 | $warnings++; | 
| 108 | 0 | 0 |  |  |  | 0 | last ARG if $pedantic; | 
| 109 | 0 |  |  |  |  | 0 | next; | 
| 110 |  |  |  |  |  |  | } | 
| 111 |  |  |  |  |  |  | } | 
| 112 |  |  |  |  |  |  | else { | 
| 113 |  |  |  |  |  |  | # set a value of 1 if option doesn't expect an argument | 
| 114 | 1 |  |  |  |  | 2 | $value = 1; | 
| 115 |  |  |  |  |  |  | } | 
| 116 |  |  |  |  |  |  |  | 
| 117 |  |  |  |  |  |  | # set the variable with the new value | 
| 118 | 8 |  |  |  |  | 17 | $state->set($variable, $value); | 
| 119 |  |  |  |  |  |  | } | 
| 120 |  |  |  |  |  |  | else { | 
| 121 | 0 |  |  |  |  | 0 | $state->_error("$arg: invalid option"); | 
| 122 | 0 |  |  |  |  | 0 | $warnings++; | 
| 123 | 0 | 0 |  |  |  | 0 | last ARG if $pedantic; | 
| 124 |  |  |  |  |  |  | } | 
| 125 |  |  |  |  |  |  | } | 
| 126 |  |  |  |  |  |  |  | 
| 127 |  |  |  |  |  |  | # return status | 
| 128 | 4 | 50 |  |  |  | 23 | return $warnings ? 0 : 1; | 
| 129 |  |  |  |  |  |  | } | 
| 130 |  |  |  |  |  |  |  | 
| 131 |  |  |  |  |  |  |  | 
| 132 |  |  |  |  |  |  |  | 
| 133 |  |  |  |  |  |  | 1; | 
| 134 |  |  |  |  |  |  |  | 
| 135 |  |  |  |  |  |  | __END__ |