| 1 | package Getopt::App::Complete; | ||||||||||||||||||||||||||||||||||||||||||
| 2 | 2 | ▶ | use feature qw(:5.16); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 3 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 4 | 2 | ▶ | use warnings; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 5 | 2 | ▶ | use utf8; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 6 | 2 | ▶ | use Cwd qw(abs_path); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 7 | 2 | ▶ | use File::Basename qw(basename); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 8 | 2 | ▶ | use Exporter qw(import); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 9 | |||||||||||||||||||||||||||||||||||||||||||
| 10 | 2 | our @EXPORT_OK = qw(complete_reply generate_completion_script); | |||||||||||||||||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||||||||||||||||
| 12 | 2 | require Getopt::App; | |||||||||||||||||||||||||||||||||||||||||
| 13 | 2 | ▶ | our $call_maybe = do { no warnings qw(once); $Getopt::App::call_maybe }; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 14 | 2 | our $argv_index = 0; | |||||||||||||||||||||||||||||||||||||||||
| 15 | |||||||||||||||||||||||||||||||||||||||||||
| 16 | sub complete_reply { | ||||||||||||||||||||||||||||||||||||||||||
| 17 | 10 | ▶ | my $app = shift; | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
complete_reply: called CC=14 SCAR=26.4
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 18 | 10 | ▶ | my $subcommands = $app->$call_maybe('getopt_subcommands') || []; | ||||||||||||||||||||||||||||||||||||||||
|
Condition100%2/2
$app->$Getopt::App::Complete::call_maybe('getopt_subcommands') || []
MC/DC100%1/1
$app->$Getopt::App::Complete::call_maybe('getopt_subcommands') || []
$app->$Getopt::App::Complete::call_maybe('getopt_subcommands')
Truth table100%2/2
A
A= $app->$Getopt::App::Complete::call_maybe('getopt_subcommands')
| |||||||||||||||||||||||||||||||||||||||||||
| 19 | 10 | my ($script, @argv) = split /\s+/, $ENV{COMP_LINE}; | |||||||||||||||||||||||||||||||||||||||||
| 20 | |||||||||||||||||||||||||||||||||||||||||||
| 21 | # Recurse into subcommand | ||||||||||||||||||||||||||||||||||||||||||
| 22 | 10 | ▶ | if ($argv[$argv_index] and $argv[$argv_index] =~ m!^\w! and @$subcommands) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition83%5/6
$argv[$Getopt::App::Complete::argv_index] and $argv[$Getopt::App::Complete::argv_index] =~ /^\w/u and @$subcommands
$argv[$Getopt::App::Complete::argv_index] and $argv[$Getopt::App::Complete::argv_index] =~ /^\w/u
MC/DC66%2/3
$argv[$Getopt::App::Complete::argv_index] and $argv[$Getopt::App::Complete::argv_index] =~ /^\w/u and @$subcommands
$argv[$Getopt::App::Complete::argv_index]
$argv[$Getopt::App::Complete::argv_index] =~ /^\w/u
@$subcommands
Truth table75%3/4
A and B and C
A= $argv[$Getopt::App::Complete::argv_index]B= $argv[$Getopt::App::Complete::argv_index] =~ /^\w/uC= @$subcommands
| |||||||||||||||||||||||||||||||||||||||||||
| 23 | 4 | for my $subcommand (@$subcommands) { | |||||||||||||||||||||||||||||||||||||||||
| 24 | 15 | ▶ | next unless $argv[$argv_index] eq $subcommand->[0]; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 25 | 3 | local $argv_index = $argv_index + 1; | |||||||||||||||||||||||||||||||||||||||||
| 26 | 3 | return Getopt::App::_subcommand_run($app, $subcommand, [@argv[$argv_index - 1, $#argv]]); | |||||||||||||||||||||||||||||||||||||||||
| 27 | } | ||||||||||||||||||||||||||||||||||||||||||
| 28 | } | ||||||||||||||||||||||||||||||||||||||||||
| 29 | |||||||||||||||||||||||||||||||||||||||||||
| 30 | # List matching subcommands | ||||||||||||||||||||||||||||||||||||||||||
| 31 | 7 | ▶ | my $got = substr($ENV{COMP_LINE}, 0, $ENV{COMP_POINT}) =~ m!(\S+)$! ? $1 : ''; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 32 | 7 | for my $subcommand (@$subcommands) { | |||||||||||||||||||||||||||||||||||||||||
| 33 | 24 | ▶ | say $subcommand->[0] if index($subcommand->[0], $got) == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 34 | } | ||||||||||||||||||||||||||||||||||||||||||
| 35 | |||||||||||||||||||||||||||||||||||||||||||
| 36 | # List matching command line options | ||||||||||||||||||||||||||||||||||||||||||
| 37 | 2 | ▶ | no warnings q(once); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||||||||||||||||
| 38 | 7 | ▶ | for (@{$Getopt::App::OPTIONS || []}) { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 39 | 21 | my $opt = $_; | |||||||||||||||||||||||||||||||||||||||||
| 40 | 21 | $opt =~ s!(=[si][@%]?|\!|\+|\s)(.*)!!; | |||||||||||||||||||||||||||||||||||||||||
| 41 | 21 | ($opt) = sort { length $b <=> length $a } split /\|/, $opt; # use --version instead of -v | |||||||||||||||||||||||||||||||||||||||||
| 42 | 21 | ▶ | $opt = length($opt) == 1 ? "-$opt" : "--$opt"; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 43 | 21 | ▶ | next unless index($opt, $got) == 0; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 44 | 13 | say $opt; | |||||||||||||||||||||||||||||||||||||||||
| 45 | } | ||||||||||||||||||||||||||||||||||||||||||
| 46 | |||||||||||||||||||||||||||||||||||||||||||
| 47 | 7 | return 0; | |||||||||||||||||||||||||||||||||||||||||
| 48 | } | ||||||||||||||||||||||||||||||||||||||||||
| 49 | |||||||||||||||||||||||||||||||||||||||||||
| 50 | sub generate_completion_script { | ||||||||||||||||||||||||||||||||||||||||||
| 51 | 4 | ▶ | my $script_path = abs_path($0); | ||||||||||||||||||||||||||||||||||||||||
|
Subroutine
generate_completion_script: called CC=4 SCAR=13.9
Pod
documented
| |||||||||||||||||||||||||||||||||||||||||||
| 52 | 4 | my $script_name = basename($0); | |||||||||||||||||||||||||||||||||||||||||
| 53 | 4 | ▶ | my $shell = ($ENV{SHELL} || 'bash') =~ m!\bzsh\b! ? 'zsh' : 'bash'; | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$ENV{'SHELL'} || 'bash'
MC/DC0%0/1
$ENV{'SHELL'} || 'bash'
$ENV{'SHELL'}
Truth table50%1/2
A
A= $ENV{'SHELL'}
| |||||||||||||||||||||||||||||||||||||||||||
| 54 | |||||||||||||||||||||||||||||||||||||||||||
| 55 | 4 | ▶ | if ($shell eq 'zsh') { | ||||||||||||||||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||||||||||||||||
| 56 | 3 | my $function = '_' . $script_name =~ s!\W!_!gr; | |||||||||||||||||||||||||||||||||||||||||
| 57 | 3 | return <<"HERE"; | |||||||||||||||||||||||||||||||||||||||||
| 58 | $function() { | ||||||||||||||||||||||||||||||||||||||||||
| 59 | read -l; local l="\$REPLY"; | ||||||||||||||||||||||||||||||||||||||||||
| 60 | read -ln; local p="\$REPLY"; | ||||||||||||||||||||||||||||||||||||||||||
| 61 | reply=(\$(COMP_LINE="\$l" COMP_POINT="\$p" COMP_SHELL="zsh" $script_path)); | ||||||||||||||||||||||||||||||||||||||||||
| 62 | }; | ||||||||||||||||||||||||||||||||||||||||||
| 63 | |||||||||||||||||||||||||||||||||||||||||||
| 64 | compctl -f -K $function $script_name; | ||||||||||||||||||||||||||||||||||||||||||
| 65 | HERE | ||||||||||||||||||||||||||||||||||||||||||
| 66 | } | ||||||||||||||||||||||||||||||||||||||||||
| 67 | else { | ||||||||||||||||||||||||||||||||||||||||||
| 68 | 1 | return "complete -o default -C $script_path $script_name;\n"; | |||||||||||||||||||||||||||||||||||||||||
| 69 | } | ||||||||||||||||||||||||||||||||||||||||||
| 70 | } | ||||||||||||||||||||||||||||||||||||||||||
| 71 | |||||||||||||||||||||||||||||||||||||||||||
| 72 | 2 | 1; | |||||||||||||||||||||||||||||||||||||||||
| 73 | |||||||||||||||||||||||||||||||||||||||||||
| 74 | =encoding utf8 | ||||||||||||||||||||||||||||||||||||||||||
| 75 | |||||||||||||||||||||||||||||||||||||||||||
| 76 | =head1 NAME | ||||||||||||||||||||||||||||||||||||||||||
| 77 | |||||||||||||||||||||||||||||||||||||||||||
| 78 | Getopt::App::Complete - Add auto-completion to you Getopt::App script | ||||||||||||||||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||||||||||||||||
| 80 | =head1 SYNOPSIS | ||||||||||||||||||||||||||||||||||||||||||
| 81 | |||||||||||||||||||||||||||||||||||||||||||
| 82 | use Getopt::App -complete; | ||||||||||||||||||||||||||||||||||||||||||
| 83 | |||||||||||||||||||||||||||||||||||||||||||
| 84 | run( | ||||||||||||||||||||||||||||||||||||||||||
| 85 | 'h # Print help', | ||||||||||||||||||||||||||||||||||||||||||
| 86 | 'completion-script # Print autocomplete script', | ||||||||||||||||||||||||||||||||||||||||||
| 87 | sub { | ||||||||||||||||||||||||||||||||||||||||||
| 88 | my ($app, @args) = @_; | ||||||||||||||||||||||||||||||||||||||||||
| 89 | return print generate_completion_script() if $app->{'completion-script'}; | ||||||||||||||||||||||||||||||||||||||||||
| 90 | return print extract_usage() if $app->{h}; | ||||||||||||||||||||||||||||||||||||||||||
| 91 | }, | ||||||||||||||||||||||||||||||||||||||||||
| 92 | ); | ||||||||||||||||||||||||||||||||||||||||||
| 93 | |||||||||||||||||||||||||||||||||||||||||||
| 94 | =head1 DESCRIPTION | ||||||||||||||||||||||||||||||||||||||||||
| 95 | |||||||||||||||||||||||||||||||||||||||||||
| 96 | L<Getopt::App::Complete> contains helper functions for adding auto-completion to | ||||||||||||||||||||||||||||||||||||||||||
| 97 | your L<Getopt::App> powered script. | ||||||||||||||||||||||||||||||||||||||||||
| 98 | |||||||||||||||||||||||||||||||||||||||||||
| 99 | This module is currently EXPERIMENTAL. | ||||||||||||||||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||||||||||||||||
| 101 | =head1 EXPORTED FUNCTIONS | ||||||||||||||||||||||||||||||||||||||||||
| 102 | |||||||||||||||||||||||||||||||||||||||||||
| 103 | =head2 complete_reply | ||||||||||||||||||||||||||||||||||||||||||
| 104 | |||||||||||||||||||||||||||||||||||||||||||
| 105 | $int = complete_reply($app_obj); | ||||||||||||||||||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||||||||||||||||
| 107 | This function is the default behaviour when L<Getopt::App/run> is called with | ||||||||||||||||||||||||||||||||||||||||||
| 108 | C<COMP_POINT> and C<COMP_LINE> set. | ||||||||||||||||||||||||||||||||||||||||||
| 109 | |||||||||||||||||||||||||||||||||||||||||||
| 110 | This function will print completion options based on C<COMP_POINT> and | ||||||||||||||||||||||||||||||||||||||||||
| 111 | C<COMP_LINE> to STDOUT and is aware of subcommands. | ||||||||||||||||||||||||||||||||||||||||||
| 112 | |||||||||||||||||||||||||||||||||||||||||||
| 113 | =head2 generate_completion_script | ||||||||||||||||||||||||||||||||||||||||||
| 114 | |||||||||||||||||||||||||||||||||||||||||||
| 115 | $str = generate_completion_script(); | ||||||||||||||||||||||||||||||||||||||||||
| 116 | |||||||||||||||||||||||||||||||||||||||||||
| 117 | This function will detect if the C<bash> or C<zsh> shell is in use and return | ||||||||||||||||||||||||||||||||||||||||||
| 118 | the appropriate initialization commands. | ||||||||||||||||||||||||||||||||||||||||||
| 119 | |||||||||||||||||||||||||||||||||||||||||||
| 120 | =head1 SEE ALSO | ||||||||||||||||||||||||||||||||||||||||||
| 121 | |||||||||||||||||||||||||||||||||||||||||||
| 122 | L<Getopt::App> | ||||||||||||||||||||||||||||||||||||||||||
| 123 | |||||||||||||||||||||||||||||||||||||||||||
| 124 | =cut | ||||||||||||||||||||||||||||||||||||||||||