File Coverage

blib/lib/Parse/CommandLine.pm
Criterion Covered Total %
statement 43 52 82.6
branch 19 24 79.1
condition 4 9 44.4
subroutine 5 5 100.0
pod 1 1 100.0
total 72 91 79.1


line stmt bran cond sub pod time code
1             package Parse::CommandLine;
2 2     2   37820 use 5.008001;
  2         10  
  2         94  
3 2     2   11 use strict;
  2         3  
  2         88  
4 2     2   21 use warnings;
  2         3  
  2         126  
5              
6             our $VERSION = "0.02";
7              
8 2     2   2242 use parent 'Exporter';
  2         776  
  2         12  
9             our @EXPORT = qw/parse_command_line/;
10              
11             sub parse_command_line {
12 8     8 1 22 my $str = shift;
13              
14 8         24 $str =~ s/\A\s+//ms;
15 8         23 $str =~ s/\s+\z//ms;
16              
17 8         10 my @argv;
18             my $buf;
19 0         0 my $escaped;
20 0         0 my $double_quoted;
21 0         0 my $single_quoted;
22              
23 8         38 for my $char (split //, $str) {
24 124 100       196 if ($escaped) {
25 1         1 $buf .= $char;
26 1         2 $escaped = undef;
27 1         2 next;
28             }
29              
30 123 100       187 if ($char eq '\\') {
31 1 50       5 if ($single_quoted) {
32 0         0 $buf .= $char;
33             }
34             else {
35 1         2 $escaped = 1;
36             }
37 1         2 next;
38             }
39              
40 122 100       225 if ($char =~ /\s/) {
41 12 100 66     47 if ($single_quoted || $double_quoted) {
42 3         5 $buf .= $char;
43             }
44             else {
45 9 100       22 push @argv, $buf if defined $buf;
46 9         12 undef $buf;
47             }
48 12         14 next;
49             }
50              
51 110 100       175 if ($char eq '"') {
52 14 50       25 if ($single_quoted) {
53 0         0 $buf .= $char;
54 0         0 next;
55             }
56 14         25 $double_quoted = !$double_quoted;
57 14         22 next;
58             }
59              
60 96 100       152 if ($char eq "'") {
61 3 50       6 if ($double_quoted) {
62 3         3 $buf .= $char;
63 3         3 next;
64             }
65 0         0 $single_quoted = !$single_quoted;
66 0         0 next;
67             }
68              
69 93         226 $buf .= $char;
70             }
71 8 50       33 push @argv, $buf if defined $buf;
72              
73 8 50 33     49 if ($escaped || $single_quoted || $double_quoted) {
      33        
74 0         0 die 'invalid command line string';
75             }
76              
77 8         64 @argv;
78             }
79              
80             1;
81             __END__