1 package App
::Codeowners
::Options
;
2 # ABSTRACT: Getopt and shell completion for App::Codeowners
9 use Getopt
::Long
2.39 ();
12 our $VERSION = '9999.999'; # VERSION
15 eval { require Pod
::Usage
};
17 my $ref = $VERSION eq '9999.999' ? 'master' : "v$VERSION";
18 my $exit = (@_ == 1 && $_[0] =~ /^\d+$/ && $_[0]) //
19 (@_ % 2 == 0 && {@_}->{'-exitval'}) // 2;
21 Online documentation is available at:
23 https://github.com/chazmcgarvey/git-codeowners/blob/$ref/README.md
25 Tip: To enable inline documentation, install the Pod::Usage module.
31 Pod
::Usage
::pod2usage
(@_);
37 'color|colour!' => (-t STDOUT
? 1 : 0), ## no critic (InputOutput::ProhibitInteractiveTest)
38 'format|f=s' => undef,
41 'shell-completion:s' => undef,
46 sub _command_options
{
69 my @commands = sort keys %{$self->_command_options};
76 if (my $command = $self->{command
}) {
77 @command_options = keys %{$self->_command_options->{$command} || {}};
79 return (keys %{$self->_early_options}, @command_options);
84 $options = App
::Codeowners
::Options-
>new(@ARGV);
86 Construct a new object
.
94 # assume UTF-8 args if non-ASCII
95 @args = map { decode
('UTF-8', $_) } @args if grep { /\P{ASCII}/ } @args;
97 my $self = bless {}, $class;
99 my @args_copy = @args;
101 my $opts = $self->get_options(
103 spec
=> $self->_early_options,
104 config
=> 'pass_through',
107 if ($ENV{CODEOWNERS_COMPLETIONS
}) {
108 $self->{command
} = $args[0] || '';
109 my $cword = $ENV{CWORD
};
110 my $cur = $ENV{CUR
} || '';
111 # Adjust cword to remove progname
112 while (0 < --$cword) {
113 last if $cur eq ($args_copy[$cword] || '');
115 $self->completions($cword, @args_copy);
119 if ($opts->{version
}) {
120 my $progname = path
($0)->basename;
121 print "${progname} ${VERSION}\n";
125 _pod2usage
(-exitval
=> 0, -verbose
=> 99, -sections
=> [qw(NAME SYNOPSIS OPTIONS COMMANDS)]);
127 if ($opts->{manual
}) {
128 _pod2usage
(-exitval
=> 0, -verbose
=> 2);
130 if (defined $opts->{shell_completion
}) {
131 $self->shell_completion($opts->{shell_completion
});
135 # figure out the command (or default to "show")
136 my $command = shift @args;
137 my $command_options = $self->_command_options->{$command || ''};
138 if (!$command_options) {
139 unshift @args, $command if defined $command;
141 $command_options = $self->_command_options->{$command};
144 my $more_opts = $self->get_options(
146 spec
=> $command_options,
149 %$self = (%$opts, %$more_opts, command
=> $command, args
=> \
@args);
155 $str = $options->command;
157 Get the command specified by args provided
when the object was created
.
163 my $command = $self->{command
};
164 my @commands = sort keys %{$self->_command_options};
165 return if not grep { $_ eq $command } @commands;
166 $command =~ s/[^a-z]/_/g;
172 $args = $options->args;
174 Get the args provided
when the object was created
.
180 return @{$self->{args
} || []};
185 $options = $options->get_options(
187 spec
=> \
@expected_options,
188 callback
=> sub { my ($arg, $results) = @_; ... },
191 Convert command-line arguments to options
, based on specified rules
.
193 Returns a hashref of options
or C
<undef> if an error occurred
.
196 * C<args> - Arguments from the caller (e.g. C<@ARGV>).
197 * C<spec> - List of L<Getopt::Long> compatible option strings.
198 * C<callback> - Optional coderef to call for non-option arguments.
199 * C<config> - Optional L<Getopt::Long> configuration string.
205 my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_};
209 while (my ($opt, $default_value) = each %{$args->{spec
}}) {
210 my ($name) = $opt =~ /^([^=:!|]+)/;
212 $results{$name} = $default_value;
213 $options{$opt} = \
$results{$name};
216 if (my $fn = $args->{callback
}) {
217 $options{'<>'} = sub {
219 $fn->($arg, \
%results);
223 my $p = Getopt
::Long
::Parser-
>new;
224 $p->configure($args->{config
} || 'default');
225 return if !$p->getoptionsfromarray($args->{args
}, %options);
230 =method shell_completion
232 $options->shell_completion($shell_type);
234 Print shell code to C
<STDOUT
> for the
given type of shell
. When
eval'd, the shell code enables
235 completion for the F<git-codeowners> command.
239 sub shell_completion {
241 my $type = lc(shift || 'bash
');
243 if ($type eq 'bash
') {
245 # git-codeowners - Bash completion
246 # To use, eval this code:
247 # eval "$(git-codeowners --shell-completion)"
248 # This will work without the bash-completion package, but handling of colons
249 # in the completion word will work better with bash-completion installed and
252 local cur words cword
253 if declare -f _get_comp_words_by_ref >/dev/null
255 _get_comp_words_by_ref -n : cur cword words
257 words=("${COMP_WORDS[@]}")
262 COMPREPLY=($(CODEOWNERS_COMPLETIONS=1 CWORD="$cword" CUR="$cur" ${words[@]}))
263 # COMPREPLY=($(${words[0]} --completions "$cword" "${words[@]}"))
266 COMPREPLY=($(compgen -A "${COMPREPLY[0]}" -- "$cur"))
268 declare -f __ltrim_colon_completions >/dev/null && \
269 __ltrim_colon_completions "$cur"
272 complete -F _git_codeowners git-codeowners
276 # TODO - Would be nice to support Zsh
277 warn "No such shell completion: $type\n";
283 $options->completions($current_arg_index, @args);
285 Print completions to C
<STDOUT
> for the
given argument list
and cursor position
, and exit.
287 May also
exit with status
9 and a compgen action printed to C
<STDOUT
> to indicate that the shell
288 should generate its own completions
.
299 my $current = $words[$cword] || '';
300 my $prev = $words[$cword - 1] || '';
304 if ($prev eq '--format
' || $prev eq '-f
') {
305 $reply = $self->_completion_formats;
307 elsif ($current =~ /^-/) {
308 $reply = $self->_completion_options;
311 if (!$self->command) {
312 $reply = [$self->_commands, @{$self->_completion_options([keys %{$self->_early_options}])}];
321 print grep { /^\Q$current\E/ } @$reply;
325 sub _completion_options {
327 my $opts = shift || [$self->_options];
331 for my $option (@$opts) {
332 my ($names, $op, $vtype) = $option =~ /^([^=:!]+)([=:!]?)(.*)$/;
333 my @names = split(/\|/, $names);
335 for my $name (@names) {
337 push @options, "--$name", "--no-$name";
340 if (length($name) > 1) {
341 push @options, "--$name";
344 push @options, "-$name";
350 return [sort @options];
353 sub _completion_formats { [qw(csv json json:pretty tsv yaml)] }