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