]>
Dogcows Code - chaz/git-codeowners/blob - lib/App/Codeowners/Options.pm
54d1fc007b4d0beca056173716f5ca1e593388bc
1 package App
::Codeowners
::Options
;
2 # ABSTRACT: Getopt and shell completion for App::Codeowners
9 use Getopt
::Long
2.39 ();
12 our $VERSION = '0.50'; # 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);
87 # assume UTF-8 args if non-ASCII
88 @args = map { decode
('UTF-8', $_) } @args if grep { /\P{ASCII}/ } @args;
90 my $self = bless {}, $class;
92 my @args_copy = @args;
94 my $opts = $self->get_options(
96 spec
=> $self->_early_options,
97 config
=> 'pass_through',
100 if ($ENV{CODEOWNERS_COMPLETIONS
}) {
101 $self->{command
} = $args[0] || '';
102 my $cword = $ENV{CWORD
};
103 my $cur = $ENV{CUR
} || '';
104 # Adjust cword to remove progname
105 while (0 < --$cword) {
106 last if $cur eq ($args_copy[$cword] || '');
108 $self->completions($cword, @args_copy);
112 if ($opts->{version
}) {
113 my $progname = path
($0)->basename;
114 print "${progname} ${VERSION}\n";
118 _pod2usage
(-exitval
=> 0, -verbose
=> 99, -sections
=> [qw(NAME SYNOPSIS OPTIONS COMMANDS)]);
120 if ($opts->{manual
}) {
121 _pod2usage
(-exitval
=> 0, -verbose
=> 2);
123 if (defined $opts->{shell_completion
}) {
124 $self->shell_completion($opts->{shell_completion
});
128 # figure out the command (or default to "show")
129 my $command = shift @args;
130 my $command_options = $self->_command_options->{$command || ''};
131 if (!$command_options) {
132 unshift @args, $command if defined $command;
134 $command_options = $self->_command_options->{$command};
137 my $more_opts = $self->get_options(
139 spec
=> $command_options,
142 %$self = (%$opts, %$more_opts, command
=> $command, args
=> \
@args);
149 my $command = $self->{command
};
150 my @commands = sort keys %{$self->_command_options};
151 return if not grep { $_ eq $command } @commands;
152 $command =~ s/[^a-z]/_/g;
159 return @{$self->{args
} || []};
165 my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_};
169 while (my ($opt, $default_value) = each %{$args->{spec
}}) {
170 my ($name) = $opt =~ /^([^=:!|]+)/;
172 $results{$name} = $default_value;
173 $options{$opt} = \
$results{$name};
176 if (my $fn = $args->{callback
}) {
177 $options{'<>'} = sub {
179 $fn->($arg, \
%results);
183 my $p = Getopt
::Long
::Parser-
>new;
184 $p->configure($args->{config
} || 'default');
185 return if !$p->getoptionsfromarray($args->{args
}, %options);
191 sub shell_completion
{
193 my $type = lc(shift || 'bash');
195 if ($type eq 'bash') {
197 # git-codeowners - Bash completion
198 # To use, eval this code:
199 # eval "$(git-codeowners --shell-completion)"
200 # This will work without the bash-completion package, but handling of colons
201 # in the completion word will work better with bash-completion installed and
204 local cur words cword
205 if declare -f _get_comp_words_by_ref >/dev/null
207 _get_comp_words_by_ref -n : cur cword words
209 words=("${COMP_WORDS[@]}")
214 COMPREPLY=($(CODEOWNERS_COMPLETIONS=1 CWORD="$cword" CUR="$cur" ${words[@]}))
215 # COMPREPLY=($(${words[0]} --completions "$cword" "${words[@]}"))
218 COMPREPLY=($(compgen -A "${COMPREPLY[0]}" -- "$cur"))
220 declare -f __ltrim_colon_completions >/dev/null && \
221 __ltrim_colon_completions "$cur"
224 complete -F _git_codeowners git-codeowners
228 # TODO - Would be nice to support Zsh
229 warn "No such shell completion: $type\n";
239 my $current = $words[$cword] || '';
240 my $prev = $words[$cword - 1] || '';
244 if ($prev eq '--format' || $prev eq '-f') {
245 $reply = $self->_completion_formats;
247 elsif ($current =~ /^-/) {
248 $reply = $self->_completion_options;
251 if (!$self->command) {
252 $reply = [$self->_commands, @{$self->_completion_options([keys %{$self->_early_options}])}];
261 print grep { /^\Q$current\E/ } @$reply;
265 sub _completion_options
{
267 my $opts = shift || [$self->_options];
271 for my $option (@$opts) {
272 my ($names, $op, $vtype) = $option =~ /^([^=:!]+)([=:!]?)(.*)$/;
273 my @names = split(/\|/, $names);
275 for my $name (@names) {
277 push @options, "--$name", "--no-$name";
280 if (length($name) > 1) {
281 push @options, "--$name";
284 push @options, "-$name";
290 return [sort @options];
293 sub _completion_formats
{ [qw(csv json json:pretty tsv yaml)] }
305 App::Codeowners::Options - Getopt and shell completion for App::Codeowners
315 $options = App::Codeowners::Options->new(@ARGV);
317 Construct a new object.
321 $str = $options->command;
323 Get the command specified by args provided when the object was created.
327 $args = $options->args;
329 Get the args provided when the object was created.
333 $options = $options->get_options(
335 spec => \@expected_options,
336 callback => sub { my ($arg, $results) = @_; ... },
339 Convert command-line arguments to options, based on specified rules.
341 Returns a hashref of options or C<undef> if an error occurred.
347 C<args> - Arguments from the caller (e.g. C<@ARGV>).
351 C<spec> - List of L<Getopt::Long> compatible option strings.
355 C<callback> - Optional coderef to call for non-option arguments.
359 C<config> - Optional L<Getopt::Long> configuration string.
363 =head2 shell_completion
365 $options->shell_completion($shell_type);
367 Print shell code to C<STDOUT> for the given type of shell. When eval'd, the shell code enables
368 completion for the F<git-codeowners> command.
372 $options->completions($current_arg_index, @args);
374 Print completions to C<STDOUT> for the given argument list and cursor position, and exit.
376 May also exit with status 9 and a compgen action printed to C<STDOUT> to indicate that the shell
377 should generate its own completions.
383 Please report any bugs or feature requests on the bugtracker website
384 L<https://github.com/chazmcgarvey/git-codeowners/issues>
386 When submitting a bug or request, please include a test-file or a
387 patch to an existing test-file that illustrates the bug or desired
392 Charles McGarvey <chazmcgarvey@brokenzipper.com>
394 =head1 COPYRIGHT AND LICENSE
396 This software is copyright (c) 2021 by Charles McGarvey.
398 This is free software; you can redistribute it and/or modify it under
399 the same terms as the Perl 5 programming language system itself.
This page took 0.058846 seconds and 4 git commands to generate.