line |
stmt |
bran |
cond |
sub |
pod |
time |
code |
1
|
|
|
|
|
|
|
# |
2
|
|
|
|
|
|
|
# This file is part of Getopt-Mini |
3
|
|
|
|
|
|
|
# |
4
|
|
|
|
|
|
|
# This software is copyright (c) 2015 by Rodrigo de Oliveira. |
5
|
|
|
|
|
|
|
# |
6
|
|
|
|
|
|
|
# This is free software; you can redistribute it and/or modify it under |
7
|
|
|
|
|
|
|
# the same terms as the Perl 5 programming language system itself. |
8
|
|
|
|
|
|
|
# |
9
|
|
|
|
|
|
|
package Getopt::Mini; |
10
|
2
|
|
|
2
|
|
23591
|
use strict; |
|
2
|
|
|
|
|
6
|
|
|
2
|
|
|
|
|
58
|
|
11
|
2
|
|
|
2
|
|
12
|
use warnings; |
|
2
|
|
|
|
|
2
|
|
|
2
|
|
|
|
|
66
|
|
12
|
2
|
|
|
2
|
|
1922
|
use Encode qw(); |
|
2
|
|
|
|
|
22991
|
|
|
2
|
|
|
|
|
251
|
|
13
|
|
|
|
|
|
|
|
14
|
|
|
|
|
|
|
our $VERSION = '0.03'; |
15
|
|
|
|
|
|
|
|
16
|
|
|
|
|
|
|
sub import { |
17
|
2
|
|
|
2
|
|
20
|
my $class = shift; |
18
|
2
|
|
|
|
|
7
|
my %args = @_; |
19
|
2
|
50
|
|
|
|
11
|
if( defined $args{var} ) { |
|
|
100
|
|
|
|
|
|
20
|
0
|
0
|
|
|
|
0
|
if( $args{var} !~ /::/ ) { |
21
|
0
|
|
|
|
|
0
|
my $where = caller(0); |
22
|
0
|
|
|
|
|
0
|
$args{var} = $where . '::' . $args{var}; |
23
|
|
|
|
|
|
|
} |
24
|
0
|
|
|
|
|
0
|
my %hash = getopt( arrays=>0 ); |
25
|
2
|
|
|
2
|
|
18
|
no strict 'refs'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
117
|
|
26
|
0
|
|
|
|
|
0
|
*{ $args{var} } = \%hash; |
|
0
|
|
|
|
|
0
|
|
27
|
|
|
|
|
|
|
} |
28
|
|
|
|
|
|
|
elsif( defined $args{later} ) { |
29
|
|
|
|
|
|
|
# import getopt() so that user can call it |
30
|
1
|
|
|
|
|
3
|
my $where = caller(0); |
31
|
2
|
|
|
2
|
|
10
|
no strict 'refs'; |
|
2
|
|
|
|
|
4
|
|
|
2
|
|
|
|
|
1316
|
|
32
|
1
|
|
|
|
|
3
|
*{ $where . '::getopt' } = \&getopt; |
|
1
|
|
|
|
|
6
|
|
33
|
|
|
|
|
|
|
} |
34
|
|
|
|
|
|
|
else { |
35
|
|
|
|
|
|
|
# into %ARGV |
36
|
1
|
|
|
|
|
5
|
getopt( arrays=>0 ); |
37
|
|
|
|
|
|
|
} |
38
|
|
|
|
|
|
|
#unshift @ARGV, @barewords; |
39
|
2
|
|
|
|
|
1972
|
return; |
40
|
|
|
|
|
|
|
} |
41
|
|
|
|
|
|
|
|
42
|
|
|
|
|
|
|
sub getopt_array { |
43
|
0
|
|
|
0
|
0
|
0
|
getopt( arrays=>1 , @_ ); |
44
|
|
|
|
|
|
|
} |
45
|
|
|
|
|
|
|
|
46
|
|
|
|
|
|
|
sub getopt { |
47
|
4
|
|
|
4
|
0
|
2613
|
my ( $last_opt, $last_done, %hash ); |
48
|
0
|
|
|
|
|
0
|
my %opts; |
49
|
|
|
|
|
|
|
# get my own opts |
50
|
|
|
|
|
|
|
my @argv = @_ == 0 |
51
|
|
|
|
|
|
|
? @ARGV |
52
|
4
|
50
|
|
|
|
15
|
: do { |
53
|
4
|
|
|
|
|
13
|
%opts = @_; |
54
|
4
|
100
|
|
|
|
4
|
@{ delete $opts{argv} || [] }; |
|
4
|
|
|
|
|
30
|
|
55
|
|
|
|
|
|
|
}; |
56
|
4
|
100
|
|
|
|
14
|
if (not @argv){ push(@argv, Encode::decode('UTF-8' ,$_) ) for @ARGV; } |
|
1
|
|
|
|
|
3
|
|
57
|
4
|
100
|
|
|
|
15
|
return () unless @argv; |
58
|
3
|
|
|
|
|
11
|
$hash{_argv} = [ @argv ]; |
59
|
3
|
|
|
|
|
8
|
while(@argv) { |
60
|
16
|
|
|
|
|
25
|
my $arg = shift @argv; |
61
|
16
|
100
|
|
|
|
65
|
if ( $arg =~ m/^-(\w)$/ ) { # single letter |
|
|
100
|
|
|
|
|
|
62
|
5
|
|
|
|
|
12
|
my $flag = $1; |
63
|
5
|
50
|
33
|
|
|
22
|
if( $opts{hungry_flags} && defined $argv[0] && $argv[0] !~ /^-/ ) { |
|
|
|
33
|
|
|
|
|
64
|
0
|
|
|
|
|
0
|
$hash{$flag} = shift @argv; |
65
|
|
|
|
|
|
|
} else { |
66
|
5
|
|
|
|
|
10
|
$hash{$flag} ++; |
67
|
|
|
|
|
|
|
} |
68
|
5
|
|
|
|
|
13
|
$last_done= 1; |
69
|
|
|
|
|
|
|
} |
70
|
|
|
|
|
|
|
elsif ( $arg =~ m/^-+(.+)/ ) { |
71
|
4
|
|
|
|
|
9
|
$last_opt = $1; |
72
|
4
|
|
|
|
|
5
|
$last_done=0; |
73
|
4
|
50
|
|
|
|
11
|
if( $last_opt =~ m/^(.*)\=(.*)$/ ) { |
74
|
0
|
|
|
|
|
0
|
push @{ $hash{$1} }, $2 ; |
|
0
|
|
|
|
|
0
|
|
75
|
0
|
|
|
|
|
0
|
$last_done= 1; |
76
|
|
|
|
|
|
|
} else { |
77
|
4
|
100
|
|
|
|
21
|
$hash{$last_opt} = [] unless ref $hash{$last_opt}; |
78
|
|
|
|
|
|
|
} |
79
|
|
|
|
|
|
|
} |
80
|
|
|
|
|
|
|
else { |
81
|
|
|
|
|
|
|
#$arg = Encode::encode_utf8($arg) if Encode::is_utf8($arg); |
82
|
7
|
100
|
100
|
|
|
44
|
$last_opt ='' if !$opts{arrays} && ( $last_done || ! defined $last_opt ); |
|
|
|
33
|
|
|
|
|
83
|
7
|
|
|
|
|
11
|
push @{ $hash{$last_opt} }, $arg; |
|
7
|
|
|
|
|
18
|
|
84
|
7
|
|
|
|
|
21
|
$last_done = 1; |
85
|
|
|
|
|
|
|
} |
86
|
|
|
|
|
|
|
} |
87
|
|
|
|
|
|
|
# convert single option => scalar |
88
|
3
|
|
|
|
|
12
|
for( keys %hash ) { |
89
|
12
|
100
|
|
|
|
32
|
next unless ref( $hash{$_} ) eq 'ARRAY'; |
90
|
8
|
50
|
|
|
|
10
|
if( @{ $hash{$_} } == 0 ) { |
|
8
|
100
|
|
|
|
19
|
|
91
|
0
|
0
|
|
|
|
0
|
$hash{$_} = $opts{define} ? 1 : (); |
92
|
8
|
|
|
|
|
26
|
} elsif( @{ $hash{$_} } == 1 ) { |
93
|
3
|
|
|
|
|
82
|
$hash{$_} = $hash{$_}->[0]; |
94
|
|
|
|
|
|
|
} |
95
|
|
|
|
|
|
|
} |
96
|
3
|
50
|
|
|
|
10
|
if( defined wantarray ) { |
97
|
3
|
|
|
|
|
24
|
return %hash; |
98
|
|
|
|
|
|
|
} else { |
99
|
0
|
|
|
|
|
|
%ARGV = %hash; |
100
|
|
|
|
|
|
|
} |
101
|
|
|
|
|
|
|
} |
102
|
|
|
|
|
|
|
|
103
|
|
|
|
|
|
|
sub getopt_validate { |
104
|
0
|
|
|
0
|
0
|
|
my %args = @_; |
105
|
0
|
|
|
|
|
|
$args{''}={isa=>'Any'}; # ignores this |
106
|
0
|
|
|
|
|
|
require Data::Validator; |
107
|
0
|
|
|
|
|
|
my $rule = Data::Validator->new( %args ); |
108
|
0
|
|
|
|
|
|
@_ = ($rule, %ARGV ); |
109
|
0
|
|
|
|
|
|
goto \&Data::Validator::validate; |
110
|
|
|
|
|
|
|
} |
111
|
|
|
|
|
|
|
|
112
|
|
|
|
|
|
|
1; |
113
|
|
|
|
|
|
|
|
114
|
|
|
|
|
|
|
__END__ |