1 package App
::Codeowners
::Options
;
2 # ABSTRACT: Getopt and shell completion for App::Codeowners
7 use Getopt
::Long
2.39 ();
11 our $VERSION = '9999.999'; # VERSION
15 'color|colour!' => (-t STDOUT
? 1 : 0), ## no critic (InputOutput::ProhibitInteractiveTest)
16 'format|f=s' => undef,
19 'shell-completion:s' => undef,
42 my @commands = sort keys %{$self->command_options};
49 if (my $command = $self->{command
}) {
50 @command_options = keys %{$self->command_options->{$command} || {}};
52 return (keys %{$self->early_options}, @command_options);
59 my $self = bless {}, $class;
61 my @args_copy = @args;
63 my $opts = $self->get_options(
65 spec
=> $self->early_options,
66 config
=> 'pass_through',
69 if ($ENV{CODEOWNERS_COMPLETIONS
}) {
70 $self->{command
} = $args[0] || '';
71 my $cword = $ENV{CWORD
};
72 my $cur = $ENV{CUR
} || '';
73 # Adjust cword to remove progname
74 while (0 < --$cword) {
75 last if $cur eq ($args_copy[$cword] || '');
77 $self->completions($cword, @args_copy);
81 if ($opts->{version
}) {
82 my $progname = path
($0)->basename;
83 print "${progname} ${VERSION}\n";
87 pod2usage
(-exitval
=> 0, -verbose
=> 99, -sections
=> [qw(NAME SYNOPSIS OPTIONS)]);
89 if ($opts->{manual
}) {
90 pod2usage
(-exitval
=> 0, -verbose
=> 2);
92 if (defined $opts->{shell_completion
}) {
93 $self->shell_completion($opts->{shell_completion
});
97 # figure out the command (or default to "show")
98 my $command = shift @args;
99 my $command_options = $self->command_options->{$command || ''};
100 if (!$command_options) {
101 unshift @args, $command if defined $command;
103 $command_options = $self->command_options->{$command};
106 my $more_opts = $self->get_options(
108 spec
=> $command_options,
111 %$self = (%$opts, %$more_opts, command
=> $command, args
=> \
@args);
117 my $command = $self->{command
};
118 my @commands = sort keys %{$self->command_options};
119 return if not grep { $_ eq $command } @commands;
120 $command =~ s/[^a-z]/_/g;
126 return @{$self->{args
} || []};
131 $options = $options->get_options(
133 spec
=> \
@expected_options,
134 callback
=> sub { my ($arg, $results) = @_; ... },
137 Convert command-line arguments to options
, based on specified rules
.
139 Returns a hashref of options
or C
<undef> if an error occurred
.
142 * C<args> - Arguments from the caller (e.g. C<@ARGV>).
143 * C<spec> - List of L<Getopt::Long> compatible option strings.
144 * C<callback> - Optional coderef to call for non-option arguments.
145 * C<config> - Optional L<Getopt::Long> configuration string.
151 my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_};
155 while (my ($opt, $default_value) = each %{$args->{spec
}}) {
156 my ($name) = $opt =~ /^([^=:!|]+)/;
158 $results{$name} = $default_value;
159 $options{$opt} = \
$results{$name};
162 if (my $fn = $args->{callback
}) {
163 $options{'<>'} = sub {
165 $fn->($arg, \
%results);
169 my $p = Getopt
::Long
::Parser-
>new;
170 $p->configure($args->{config
} || 'default');
171 return if !$p->getoptionsfromarray($args->{args
}, %options);
176 =method shell_completion
178 $options->shell_completion($shell_type);
180 Print shell code to C
<STDOUT
> for the
given type of shell
. When
eval'd, the shell code enables
181 completion for the F<git-codeowners> command.
185 sub shell_completion {
187 my $type = lc(shift || 'bash
');
189 if ($type eq 'bash
') {
191 # git-codeowners - Bash completion
192 # To use, eval this code:
193 # eval "$(git-codeowners --shell-completion)"
194 # This will work without the bash-completion package, but handling of colons
195 # in the completion word will work better with bash-completion installed and
198 local cur words cword
199 if declare -f _get_comp_words_by_ref >/dev/null
201 _get_comp_words_by_ref -n : cur cword words
203 words=("${COMP_WORDS[@]}")
208 COMPREPLY=($(CODEOWNERS_COMPLETIONS=1 CWORD="$cword" CUR="$cur" ${words[@]}))
209 # COMPREPLY=($(${words[0]} --completions "$cword" "${words[@]}"))
212 COMPREPLY=($(compgen -A "${COMPREPLY[0]}" -- "$cur"))
214 declare -f __ltrim_colon_completions >/dev/null && \
215 __ltrim_colon_completions "$cur"
218 complete -F _git_codeowners git-codeowners
222 # TODO - Would be nice to support Zsh
223 warn "No such shell completion: $type\n";
229 $options->completions($current_arg_index, @args);
231 Print completions to C
<STDOUT
> for the
given argument list
and cursor position
, and exit.
233 May also
exit with status
9 and a compgen action printed to C
<STDOUT
> to indicate that the shell
234 should generate its own completions
.
245 my $current = $words[$cword] || '';
246 my $prev = $words[$cword - 1] || '';
250 if ($prev eq '--format
' || $prev eq '-f
') {
251 $reply = $self->_completion_formats;
253 elsif ($current =~ /^-/) {
254 $reply = $self->_completion_options;
257 if (!$self->command) {
258 $reply = [$self->commands, @{$self->_completion_options([keys %{$self->early_options}])}];
267 print grep { /^\Q$current\E/ } @$reply;
271 sub _completion_options {
273 my $opts = shift || [$self->options];
277 for my $option (@$opts) {
278 my ($names, $op, $vtype) = $option =~ /^([^=:!]+)([=:!]?)(.*)$/;
279 my @names = split(/\|/, $names);
281 for my $name (@names) {
283 push @options, "--$name", "--no-$name";
286 if (length($name) > 1) {
287 push @options, "--$name";
290 push @options, "-$name";
296 return [sort @options];
299 sub _completion_formats { [qw(csv json json:pretty tsv yaml)] }