File Coverage

lib/AppConfig/Args.pm
Criterion Covered Total %
statement 35 42 83.3
branch 11 20 55.0
condition 5 5 100.0
subroutine 5 5 100.0
pod 0 2 0.0
total 56 74 75.6


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   342 use strict;
  1         2  
  1         33  
16 1     1   4 use warnings;
  1         1  
  1         19  
17 1     1   4 use AppConfig::State;
  1         1  
  1         341  
18             our $VERSION = '1.70';
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 8 my $class = shift;
35 1         1 my $state = shift;
36              
37              
38 1         3 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         1 bless $self, $class;
45              
46             # call parse() to parse any arg list passed
47 1 50       2 $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 685 my $self = shift;
71 4   100     13 my $argv = shift || \@ARGV;
72 4         5 my $warnings = 0;
73 4         4 my ($arg, $nargs, $variable, $value);
74              
75              
76             # take a local copy of the state to avoid much hash dereferencing
77 4         11 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         10 $arg = shift(@$argv);
82              
83             # '--' indicates the end of the options
84 8 50       15 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       17 if (defined $1) {
91 5         11 ($variable, $value) = split(/=/, $variable);
92 5 100       12 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         27 $nargs = $state->_argcount($variable);
100 8 100       10 if ($nargs) {
101             # check there's another arg and it's not another '-opt'
102 7 50       9 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         15 $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       14 return $warnings ? 0 : 1;
129             }
130              
131              
132              
133             1;
134              
135             __END__