]>
Dogcows Code - chaz/git-codeowners/blob - lib/App/Codeowners/Options.pm
1 package App
::Codeowners
::Options
;
2 # ABSTRACT: Getopt and shell completion for App::Codeowners
7 use Getopt
::Long
2.39 ();
11 our $VERSION = '0.42'; # 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
} || []};
137 my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_};
141 while (my ($opt, $default_value) = each %{$args->{spec
}}) {
142 my ($name) = $opt =~ /^([^=:!|]+)/;
144 $results{$name} = $default_value;
145 $options{$opt} = \
$results{$name};
148 if (my $fn = $args->{callback
}) {
149 $options{'<>'} = sub {
151 $fn->($arg, \
%results);
155 my $p = Getopt
::Long
::Parser-
>new;
156 $p->configure($args->{config
} || 'default');
157 return if !$p->getoptionsfromarray($args->{args
}, %options);
163 sub shell_completion
{
165 my $type = lc(shift || 'bash');
167 if ($type eq 'bash') {
169 # git-codeowners - Bash completion
170 # To use, eval this code:
171 # eval "$(git-codeowners --shell-completion)"
172 # This will work without the bash-completion package, but handling of colons
173 # in the completion word will work better with bash-completion installed and
176 local cur words cword
177 if declare -f _get_comp_words_by_ref >/dev/null
179 _get_comp_words_by_ref -n : cur cword words
181 words=("${COMP_WORDS[@]}")
186 COMPREPLY=($(CODEOWNERS_COMPLETIONS=1 CWORD="$cword" CUR="$cur" ${words[@]}))
187 # COMPREPLY=($(${words[0]} --completions "$cword" "${words[@]}"))
190 COMPREPLY=($(compgen -A "${COMPREPLY[0]}" -- "$cur"))
192 declare -f __ltrim_colon_completions >/dev/null && \
193 __ltrim_colon_completions "$cur"
196 complete -F _git_codeowners git-codeowners
200 # TODO - Would be nice to support Zsh
201 warn "No such shell completion: $type\n";
211 my $current = $words[$cword] || '';
212 my $prev = $words[$cword - 1] || '';
216 if ($prev eq '--format' || $prev eq '-f') {
217 $reply = $self->_completion_formats;
219 elsif ($current =~ /^-/) {
220 $reply = $self->_completion_options;
223 if (!$self->command) {
224 $reply = [$self->commands, @{$self->_completion_options([keys %{$self->early_options}])}];
233 print grep { /^\Q$current\E/ } @$reply;
237 sub _completion_options
{
239 my $opts = shift || [$self->options];
243 for my $option (@$opts) {
244 my ($names, $op, $vtype) = $option =~ /^([^=:!]+)([=:!]?)(.*)$/;
245 my @names = split(/\|/, $names);
247 for my $name (@names) {
249 push @options, "--$name", "--no-$name";
252 if (length($name) > 1) {
253 push @options, "--$name";
256 push @options, "-$name";
262 return [sort @options];
265 sub _completion_formats
{ [qw(csv json json:pretty tsv yaml)] }
277 App::Codeowners::Options - Getopt and shell completion for App::Codeowners
287 $options = $options->get_options(
289 spec => \@expected_options,
290 callback => sub { my ($arg, $results) = @_; ... },
293 Convert command-line arguments to options, based on specified rules.
295 Returns a hashref of options or C<undef> if an error occurred.
301 C<args> - Arguments from the caller (e.g. C<@ARGV>).
305 C<spec> - List of L<Getopt::Long> compatible option strings.
309 C<callback> - Optional coderef to call for non-option arguments.
313 C<config> - Optional L<Getopt::Long> configuration string.
317 =head2 shell_completion
319 $options->shell_completion($shell_type);
321 Print shell code to C<STDOUT> for the given type of shell. When eval'd, the shell code enables
322 completion for the F<git-codeowners> command.
326 $options->completions($current_arg_index, @args);
328 Print completions to C<STDOUT> for the given argument list and cursor position, and exit.
330 May also exit with status 9 and a compgen action printed to C<STDOUT> to indicate that the shell
331 should generate its own completions.
337 Please report any bugs or feature requests on the bugtracker website
338 L<https://github.com/chazmcgarvey/git-codeowners/issues>
340 When submitting a bug or request, please include a test-file or a
341 patch to an existing test-file that illustrates the bug or desired
346 Charles McGarvey <chazmcgarvey@brokenzipper.com>
348 =head1 COPYRIGHT AND LICENSE
350 This software is copyright (c) 2019 by Charles McGarvey.
352 This is free software; you can redistribute it and/or modify it under
353 the same terms as the Perl 5 programming language system itself.
This page took 0.062838 seconds and 4 git commands to generate.