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,
47 my @commands = sort keys %{$self->command_options};
54 if (my $command = $self->{command
}) {
55 @command_options = keys %{$self->command_options->{$command} || {}};
57 return (keys %{$self->early_options}, @command_options);
64 my $self = bless {}, $class;
66 my @args_copy = @args;
68 my $opts = $self->get_options(
70 spec
=> $self->early_options,
71 config
=> 'pass_through',
74 if ($ENV{CODEOWNERS_COMPLETIONS
}) {
75 $self->{command
} = $args[0] || '';
76 my $cword = $ENV{CWORD
};
77 my $cur = $ENV{CUR
} || '';
78 # Adjust cword to remove progname
79 while (0 < --$cword) {
80 last if $cur eq ($args_copy[$cword] || '');
82 $self->completions($cword, @args_copy);
86 if ($opts->{version
}) {
87 my $progname = path
($0)->basename;
88 print "${progname} ${VERSION}\n";
92 pod2usage
(-exitval
=> 0, -verbose
=> 99, -sections
=> [qw(NAME SYNOPSIS OPTIONS COMMANDS)]);
94 if ($opts->{manual
}) {
95 pod2usage
(-exitval
=> 0, -verbose
=> 2);
97 if (defined $opts->{shell_completion
}) {
98 $self->shell_completion($opts->{shell_completion
});
102 # figure out the command (or default to "show")
103 my $command = shift @args;
104 my $command_options = $self->command_options->{$command || ''};
105 if (!$command_options) {
106 unshift @args, $command if defined $command;
108 $command_options = $self->command_options->{$command};
111 my $more_opts = $self->get_options(
113 spec
=> $command_options,
116 %$self = (%$opts, %$more_opts, command
=> $command, args
=> \
@args);
122 my $command = $self->{command
};
123 my @commands = sort keys %{$self->command_options};
124 return if not grep { $_ eq $command } @commands;
125 $command =~ s/[^a-z]/_/g;
131 return @{$self->{args
} || []};
136 $options = $options->get_options(
138 spec
=> \
@expected_options,
139 callback
=> sub { my ($arg, $results) = @_; ... },
142 Convert command-line arguments to options
, based on specified rules
.
144 Returns a hashref of options
or C
<undef> if an error occurred
.
147 * C<args> - Arguments from the caller (e.g. C<@ARGV>).
148 * C<spec> - List of L<Getopt::Long> compatible option strings.
149 * C<callback> - Optional coderef to call for non-option arguments.
150 * C<config> - Optional L<Getopt::Long> configuration string.
156 my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_};
160 while (my ($opt, $default_value) = each %{$args->{spec
}}) {
161 my ($name) = $opt =~ /^([^=:!|]+)/;
163 $results{$name} = $default_value;
164 $options{$opt} = \
$results{$name};
167 if (my $fn = $args->{callback
}) {
168 $options{'<>'} = sub {
170 $fn->($arg, \
%results);
174 my $p = Getopt
::Long
::Parser-
>new;
175 $p->configure($args->{config
} || 'default');
176 return if !$p->getoptionsfromarray($args->{args
}, %options);
181 =method shell_completion
183 $options->shell_completion($shell_type);
185 Print shell code to C
<STDOUT
> for the
given type of shell
. When
eval'd, the shell code enables
186 completion for the F<git-codeowners> command.
190 sub shell_completion {
192 my $type = lc(shift || 'bash
');
194 if ($type eq 'bash
') {
196 # git-codeowners - Bash completion
197 # To use, eval this code:
198 # eval "$(git-codeowners --shell-completion)"
199 # This will work without the bash-completion package, but handling of colons
200 # in the completion word will work better with bash-completion installed and
203 local cur words cword
204 if declare -f _get_comp_words_by_ref >/dev/null
206 _get_comp_words_by_ref -n : cur cword words
208 words=("${COMP_WORDS[@]}")
213 COMPREPLY=($(CODEOWNERS_COMPLETIONS=1 CWORD="$cword" CUR="$cur" ${words[@]}))
214 # COMPREPLY=($(${words[0]} --completions "$cword" "${words[@]}"))
217 COMPREPLY=($(compgen -A "${COMPREPLY[0]}" -- "$cur"))
219 declare -f __ltrim_colon_completions >/dev/null && \
220 __ltrim_colon_completions "$cur"
223 complete -F _git_codeowners git-codeowners
227 # TODO - Would be nice to support Zsh
228 warn "No such shell completion: $type\n";
234 $options->completions($current_arg_index, @args);
236 Print completions to C
<STDOUT
> for the
given argument list
and cursor position
, and exit.
238 May also
exit with status
9 and a compgen action printed to C
<STDOUT
> to indicate that the shell
239 should generate its own completions
.
250 my $current = $words[$cword] || '';
251 my $prev = $words[$cword - 1] || '';
255 if ($prev eq '--format
' || $prev eq '-f
') {
256 $reply = $self->_completion_formats;
258 elsif ($current =~ /^-/) {
259 $reply = $self->_completion_options;
262 if (!$self->command) {
263 $reply = [$self->commands, @{$self->_completion_options([keys %{$self->early_options}])}];
272 print grep { /^\Q$current\E/ } @$reply;
276 sub _completion_options {
278 my $opts = shift || [$self->options];
282 for my $option (@$opts) {
283 my ($names, $op, $vtype) = $option =~ /^([^=:!]+)([=:!]?)(.*)$/;
284 my @names = split(/\|/, $names);
286 for my $name (@names) {
288 push @options, "--$name", "--no-$name";
291 if (length($name) > 1) {
292 push @options, "--$name";
295 push @options, "-$name";
301 return [sort @options];
304 sub _completion_formats { [qw(csv json json:pretty tsv yaml)] }