| line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
|
1
|
|
|
|
|
|
|
package Getopt::Param::Tiny; |
|
2
|
|
|
|
|
|
|
|
|
3
|
1
|
|
|
1
|
|
22296
|
use strict; |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
37
|
|
|
4
|
1
|
|
|
1
|
|
5
|
use vars qw($VERSION); |
|
|
1
|
|
|
|
|
2
|
|
|
|
1
|
|
|
|
|
863
|
|
|
5
|
|
|
|
|
|
|
$VERSION = 0.5; |
|
6
|
|
|
|
|
|
|
|
|
7
|
|
|
|
|
|
|
sub new { |
|
8
|
12
|
|
|
12
|
1
|
2447
|
my ($self, $arg_ref) = @_; |
|
9
|
12
|
|
|
|
|
30
|
$self = bless {}, $self; |
|
10
|
|
|
|
|
|
|
|
|
11
|
12
|
|
100
|
|
|
53
|
$arg_ref->{'quiet'} ||= 0; |
|
12
|
12
|
|
50
|
|
|
44
|
$arg_ref->{'strict'} ||= 0; |
|
13
|
|
|
|
|
|
|
|
|
14
|
12
|
100
|
|
|
|
36
|
my $args_ar = ref $arg_ref->{'array_ref'} ne 'ARRAY' ? \@ARGV : $arg_ref->{'array_ref'}; |
|
15
|
12
|
|
|
|
|
14
|
my @nodestruct = @{ $args_ar }; |
|
|
12
|
|
|
|
|
27
|
|
|
16
|
|
|
|
|
|
|
|
|
17
|
12
|
|
|
|
|
31
|
$self->{'opts'} = {}; |
|
18
|
|
|
|
|
|
|
|
|
19
|
12
|
|
|
|
|
16
|
my $idx = 0; |
|
20
|
|
|
|
|
|
|
ARG_ITEM: |
|
21
|
12
|
|
|
|
|
18
|
for my $arg ( @nodestruct ) { |
|
22
|
26
|
100
|
|
|
|
55
|
last ARG_ITEM if $arg eq '--'; |
|
23
|
|
|
|
|
|
|
|
|
24
|
25
|
50
|
|
|
|
104
|
my $rg = $arg_ref->{'strict'} ? qr{^--([^-])} : qr{^--(.)}; |
|
25
|
|
|
|
|
|
|
|
|
26
|
25
|
100
|
|
|
|
173
|
if( $arg =~ s/$rg/$1/ ) { |
|
27
|
23
|
|
|
|
|
56
|
my($flag, $value) = split /=/, $arg, 2; |
|
28
|
23
|
100
|
|
|
|
27
|
push @{ $self->{'opts'}{ $flag } }, defined $value ? $value : '--' . $flag; |
|
|
23
|
|
|
|
|
102
|
|
|
29
|
|
|
|
|
|
|
} |
|
30
|
|
|
|
|
|
|
else { |
|
31
|
2
|
50
|
|
|
|
6
|
warn( sprintf('Argument %s did not match %s', $idx, $rg) ) if !$arg_ref->{'quiet'}; |
|
32
|
|
|
|
|
|
|
} |
|
33
|
25
|
|
|
|
|
65
|
$idx++; |
|
34
|
|
|
|
|
|
|
} |
|
35
|
|
|
|
|
|
|
|
|
36
|
12
|
50
|
33
|
|
|
42
|
if( $self->{'opts'}{'help'} && $arg_ref->{'help_coderef'} ) { |
|
37
|
0
|
|
|
|
|
0
|
$arg_ref->{'help_coderef'}->($self); |
|
38
|
|
|
|
|
|
|
} |
|
39
|
|
|
|
|
|
|
|
|
40
|
12
|
|
100
|
0
|
|
52
|
$self->{'help_coderef'} = $arg_ref->{'help_coderef'} || sub { die( sprintf(q{No '%s' function defined}, 'help') ) }; |
|
|
0
|
|
|
|
|
0
|
|
|
41
|
|
|
|
|
|
|
|
|
42
|
12
|
100
|
66
|
|
|
16
|
if ( !keys %{$self->{'opts'}} && $arg_ref->{'no_args_help'} ) { |
|
|
12
|
|
|
|
|
47
|
|
|
43
|
1
|
|
|
|
|
7
|
$self->{'help_coderef'}->($self); |
|
44
|
|
|
|
|
|
|
} |
|
45
|
|
|
|
|
|
|
|
|
46
|
12
|
100
|
|
|
|
34
|
if( ref $arg_ref->{'known_only'} eq 'ARRAY') { |
|
47
|
2
|
|
|
|
|
4
|
my %lookup; |
|
48
|
2
|
|
|
|
|
3
|
@lookup{ @{$arg_ref->{'known_only'}} } = (); |
|
|
2
|
|
|
|
|
5
|
|
|
49
|
|
|
|
|
|
|
|
|
50
|
2
|
|
|
|
|
3
|
my $unknown = 0; |
|
51
|
2
|
|
|
|
|
3
|
for my $k (keys %{$self->{'opts'}}) { |
|
|
2
|
|
|
|
|
6
|
|
|
52
|
2
|
100
|
|
|
|
8
|
if (!exists $lookup{$k}) { |
|
53
|
1
|
|
|
|
|
9
|
$unknown++; |
|
54
|
|
|
|
|
|
|
# $k =~ s{\W}{?}g; # or quotemeta() |
|
55
|
1
|
|
|
|
|
27
|
warn( sprintf(q{Unknown argument '%s'}, quotemeta($k)) ); |
|
56
|
|
|
|
|
|
|
} |
|
57
|
|
|
|
|
|
|
} |
|
58
|
2
|
100
|
|
|
|
22
|
$self->{'help_coderef'}->($self) if $unknown; |
|
59
|
|
|
|
|
|
|
} |
|
60
|
|
|
|
|
|
|
|
|
61
|
12
|
100
|
|
|
|
35
|
if( ref $arg_ref->{'required'} eq 'ARRAY') { |
|
62
|
|
|
|
|
|
|
|
|
63
|
2
|
|
|
|
|
2
|
my $missing = 0; |
|
64
|
2
|
|
|
|
|
4
|
for my $k (@{$arg_ref->{'required'}}) { |
|
|
2
|
|
|
|
|
3
|
|
|
65
|
2
|
100
|
|
|
|
8
|
if (!exists $self->{'opts'}->{$k}) { |
|
66
|
1
|
|
|
|
|
2
|
$missing++; |
|
67
|
|
|
|
|
|
|
# $k =~ s{\W}{?}g; # or quotemeta() |
|
68
|
1
|
|
|
|
|
11
|
warn( sprintf(q{Missing argument '%s'}, quotemeta($k)) ); |
|
69
|
|
|
|
|
|
|
} |
|
70
|
|
|
|
|
|
|
} |
|
71
|
2
|
100
|
|
|
|
16
|
$self->{'help_coderef'}->($self) if $missing; |
|
72
|
|
|
|
|
|
|
} |
|
73
|
|
|
|
|
|
|
|
|
74
|
12
|
100
|
|
|
|
29
|
if( ref $arg_ref->{'validate'} eq 'CODE') { |
|
75
|
2
|
100
|
|
|
|
7
|
$arg_ref->{'validate'}->($self) || $self->{'help_coderef'}->($self); |
|
76
|
|
|
|
|
|
|
} |
|
77
|
|
|
|
|
|
|
|
|
78
|
12
|
100
|
|
|
|
69
|
if ( ref $arg_ref->{'actions'} eq 'ARRAY' ) { |
|
79
|
2
|
|
|
|
|
4
|
for my $k ($arg_ref->{'actions'}) { |
|
80
|
2
|
50
|
|
|
|
9
|
if (exists $self->{'opts'}{$k->[0]}) { |
|
81
|
2
|
100
|
|
|
|
8
|
if (ref $k->[1] eq 'CODE') { |
|
82
|
1
|
|
|
|
|
4
|
$k->[1]->($self); |
|
83
|
|
|
|
|
|
|
} |
|
84
|
|
|
|
|
|
|
else { |
|
85
|
1
|
|
|
|
|
5
|
$self->{'help_coderef'}->($self); |
|
86
|
|
|
|
|
|
|
} |
|
87
|
|
|
|
|
|
|
} |
|
88
|
|
|
|
|
|
|
} |
|
89
|
|
|
|
|
|
|
} |
|
90
|
|
|
|
|
|
|
|
|
91
|
12
|
|
|
|
|
50
|
return $self; |
|
92
|
|
|
|
|
|
|
} |
|
93
|
|
|
|
|
|
|
|
|
94
|
|
|
|
|
|
|
sub help { |
|
95
|
1
|
|
|
1
|
1
|
231
|
my ($prm) = @_; |
|
96
|
1
|
|
|
|
|
4
|
$prm->{'help_coderef'}->($prm); |
|
97
|
|
|
|
|
|
|
} |
|
98
|
|
|
|
|
|
|
|
|
99
|
|
|
|
|
|
|
sub param { |
|
100
|
28
|
|
|
28
|
1
|
5357
|
my ($self, $name, @val) = @_; |
|
101
|
28
|
50
|
|
|
|
60
|
return wantarray ? keys %{ $self->{'opts'} } : [ keys %{ $self->{'opts'} } ] if !$name; |
|
|
2
|
100
|
|
|
|
22
|
|
|
|
0
|
|
|
|
|
0
|
|
|
102
|
26
|
100
|
|
|
|
60
|
$self->{'opts'}{$name} = \@val if @val; |
|
103
|
26
|
100
|
|
|
|
66
|
return if !exists $self->{'opts'}{$name}; # do not auto vivify it |
|
104
|
23
|
100
|
|
|
|
94
|
return wantarray ? @{ $self->{'opts'}{$name} } : $self->{'opts'}{$name}->[0]; |
|
|
6
|
|
|
|
|
50
|
|
|
105
|
|
|
|
|
|
|
} |
|
106
|
|
|
|
|
|
|
|
|
107
|
|
|
|
|
|
|
1; |
|
108
|
|
|
|
|
|
|
|
|
109
|
|
|
|
|
|
|
__END__ |