File Coverage

blib/lib/Getopt/Long/Complete.pm
Criterion Covered Total %
statement 22 67 32.8
branch 8 30 26.6
condition 1 8 12.5
subroutine 4 10 40.0
pod 7 7 100.0
total 42 122 34.4


line stmt bran cond sub pod time code
1             ## no critic (Modules::ProhibitAutomaticExportation)
2              
3             package Getopt::Long::Complete;
4              
5 1     1   69283 use strict;
  1         15  
  1         32  
6 1     1   5 use warnings;
  1         2  
  1         999  
7              
8             require Exporter;
9             our @ISA = qw(Exporter);
10             our @EXPORT = qw(
11             GetOptions
12              
13             $REQUIRE_ORDER $PERMUTE $RETURN_IN_ORDER
14             );
15             our @EXPORT_OK = qw(
16             GetOptionsWithCompletion
17              
18             GetOptionsFromArray
19             GetOptionsFromString
20             Configure
21             HelpMessage
22             VersionMessage
23             );
24              
25             our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
26             our $DATE = '2021-08-24'; # DATE
27             our $DIST = 'Getopt-Long-Complete'; # DIST
28             our $VERSION = '0.316'; # VERSION
29              
30             # we don't want to always load Getopt::Long to avoid startup overhead.
31             our ($REQUIRE_ORDER, $PERMUTE, $RETURN_IN_ORDER) = (0..2); # copied from Getopt::Long
32 0     0 1 0 sub GetOptionsFromArray { require Getopt::Long; goto &Getopt::Long::GetOptionsFromArray }
  0         0  
33 0     0 1 0 sub GetOptionsFromString { require Getopt::Long; goto &Getopt::Long::GetOptionsFromString }
  0         0  
34 0     0 1 0 sub Configure { require Getopt::Long; goto &Getopt::Long::Configure }
  0         0  
35 0     0 1 0 sub HelpMessage { require Getopt::Long; goto &Getopt::Long::HelpMessage }
  0         0  
36 0     0 1 0 sub VersionMessage { require Getopt::Long; goto &Getopt::Long::VersionMessage }
  0         0  
37              
38             # default follows Getopt::Long
39             our $opt_permute = $ENV{POSIXLY_CORRECT} ? 0 : 1;
40             our $opt_pass_through = 0;
41              
42             our $opt_bundling = 1; # in Getopt::Long the default is off
43              
44             sub GetOptionsWithCompletion {
45 1     1 1 3 my $comp = shift;
46              
47 1         3 my $hash;
48             my $ospec;
49 1 50       5 if (ref($_[0]) eq 'HASH') {
50 0         0 $hash = shift;
51 0         0 for (my $i = 0; $i < @_; $i++) {
52 0 0 0     0 if (($i+1 < @_) && (ref $_[$i+1])) {
53 0         0 $ospec->{$_[$i]} = $_[$i+1];
54 0         0 $i++;
55             } else {
56 0     0   0 $ospec->{$_[$i]} = sub {};
57             }
58             }
59             } else {
60 1         4 $ospec = \@_;
61             }
62              
63 1         2 my $shell;
64 1 50       7 if ($ENV{COMP_SHELL}) {
    50          
65 0         0 ($shell = $ENV{COMP_SHELL}) =~ s!.+/!!;
66             } elsif ($ENV{COMMAND_LINE}) {
67 0         0 $shell = 'tcsh';
68             } else {
69 1         2 $shell = 'bash';
70             }
71              
72 1 50 33     10 if ($ENV{COMP_LINE} || $ENV{COMMAND_LINE}) {
73 0         0 my ($words, $cword);
74 0 0       0 if ($ENV{COMP_LINE}) {
    0          
75 0         0 require Complete::Bash;
76 0         0 ($words,$cword) = @{ Complete::Bash::parse_cmdline(undef, undef, {truncate_current_word=>1}) };
  0         0  
77 0         0 ($words,$cword) = @{ Complete::Bash::join_wordbreak_words($words, $cword) };
  0         0  
78             } elsif ($ENV{COMMAND_LINE}) {
79 0         0 require Complete::Tcsh;
80 0   0     0 $shell ||= 'tcsh';
81 0         0 ($words, $cword) = @{ Complete::Tcsh::parse_cmdline() };
  0         0  
82             }
83              
84 0         0 require Complete::Getopt::Long;
85              
86 0         0 shift @$words; $cword--; # strip program name
  0         0  
87 0         0 my $compres = Complete::Getopt::Long::complete_cli_arg(
88             words => $words, cword => $cword, getopt_spec => $ospec,
89             completion => $comp,
90             bundling => $opt_bundling,
91             );
92              
93 0 0       0 if ($shell eq 'bash') {
    0          
    0          
    0          
94 0         0 require Complete::Bash;
95 0         0 print Complete::Bash::format_completion(
96             $compres, {word=>$words->[$cword]});
97             } elsif ($shell eq 'fish') {
98 0         0 require Complete::Fish;
99 0         0 print Complete::Bash::format_completion(
100             $compres, {word=>$words->[$cword]});
101             } elsif ($shell eq 'tcsh') {
102 0         0 require Complete::Tcsh;
103 0         0 print Complete::Tcsh::format_completion($compres);
104             } elsif ($shell eq 'zsh') {
105 0         0 require Complete::Zsh;
106 0         0 print Complete::Zsh::format_completion($compres);
107             } else {
108 0         0 die "Unknown shell '$shell'";
109             }
110              
111 0         0 exit 0;
112             }
113              
114 1         866 require Getopt::Long;
115 1 50       10350 my $old_conf = Getopt::Long::Configure(
    50          
    50          
116             'no_ignore_case',
117             'no_getopt_compat',
118             'gnu_compat',
119             $opt_bundling ? 'bundling' : 'no_bundling',
120             $opt_permute ? 'permute' : 'no_permute',
121             $opt_pass_through ? 'pass_through' : 'no_pass_through',
122             );
123 1         85 my $res;
124 1 50       6 if ($hash) {
125 0         0 $res = Getopt::Long::GetOptions($hash, @_);
126             } else {
127 1         6 $res = Getopt::Long::GetOptions(@_);
128             }
129 1         590 Getopt::Long::Configure($old_conf);
130 1         19 $res;
131             }
132              
133             sub GetOptions {
134 1     1 1 1683 GetOptionsWithCompletion(undef, @_);
135             }
136              
137             1;
138             # ABSTRACT: A drop-in replacement for Getopt::Long, with shell tab completion
139              
140             __END__