]>
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.41'; # 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
} || []};
132 my $args = {@_ == 1 && ref $_[0] eq 'HASH' ? %{$_[0]} : @_};
136 while (my ($opt, $default_value) = each %{$args->{spec
}}) {
137 my ($name) = $opt =~ /^([^=:!|]+)/;
139 $results{$name} = $default_value;
140 $options{$opt} = \
$results{$name};
143 if (my $fn = $args->{callback
}) {
144 $options{'<>'} = sub {
146 $fn->($arg, \
%results);
150 my $p = Getopt
::Long
::Parser-
>new;
151 $p->configure($args->{config
} || 'default');
152 return if !$p->getoptionsfromarray($args->{args
}, %options);
158 sub shell_completion
{
160 my $type = lc(shift || 'bash');
162 if ($type eq 'bash') {
164 # git-codeowners - Bash completion
165 # To use, eval this code:
166 # eval "$(git-codeowners --shell-completion)"
167 # This will work without the bash-completion package, but handling of colons
168 # in the completion word will work better with bash-completion installed and
171 local cur words cword
172 if declare -f _get_comp_words_by_ref >/dev/null
174 _get_comp_words_by_ref -n : cur cword words
176 words=("${COMP_WORDS[@]}")
181 COMPREPLY=($(CODEOWNERS_COMPLETIONS=1 CWORD="$cword" CUR="$cur" ${words[@]}))
182 # COMPREPLY=($(${words[0]} --completions "$cword" "${words[@]}"))
185 COMPREPLY=($(compgen -A "${COMPREPLY[0]}" -- "$cur"))
187 declare -f __ltrim_colon_completions >/dev/null && \
188 __ltrim_colon_completions "$cur"
191 complete -F _git_codeowners git-codeowners
195 # TODO - Would be nice to support Zsh
196 warn "No such shell completion: $type\n";
206 my $current = $words[$cword] || '';
207 my $prev = $words[$cword - 1] || '';
211 if ($prev eq '--format' || $prev eq '-f') {
212 $reply = $self->_completion_formats;
214 elsif ($current =~ /^-/) {
215 $reply = $self->_completion_options;
218 if (!$self->command) {
219 $reply = [$self->commands, @{$self->_completion_options([keys %{$self->early_options}])}];
228 print grep { /^\Q$current\E/ } @$reply;
232 sub _completion_options
{
234 my $opts = shift || [$self->options];
238 for my $option (@$opts) {
239 my ($names, $op, $vtype) = $option =~ /^([^=:!]+)([=:!]?)(.*)$/;
240 my @names = split(/\|/, $names);
242 for my $name (@names) {
244 push @options, "--$name", "--no-$name";
247 if (length($name) > 1) {
248 push @options, "--$name";
251 push @options, "-$name";
257 return [sort @options];
260 sub _completion_formats
{ [qw(csv json json:pretty tsv yaml)] }
272 App::Codeowners::Options - Getopt and shell completion for App::Codeowners
282 $options = $options->get_options(
284 spec => \@expected_options,
285 callback => sub { my ($arg, $results) = @_; ... },
288 Convert command-line arguments to options, based on specified rules.
290 Returns a hashref of options or C<undef> if an error occurred.
296 C<args> - Arguments from the caller (e.g. C<@ARGV>).
300 C<spec> - List of L<Getopt::Long> compatible option strings.
304 C<callback> - Optional coderef to call for non-option arguments.
308 C<config> - Optional L<Getopt::Long> configuration string.
312 =head2 shell_completion
314 $options->shell_completion($shell_type);
316 Print shell code to C<STDOUT> for the given type of shell. When eval'd, the shell code enables
317 completion for the F<git-codeowners> command.
321 $options->completions($current_arg_index, @args);
323 Print completions to C<STDOUT> for the given argument list and cursor position, and exit.
325 May also exit with status 9 and a compgen action printed to C<STDOUT> to indicate that the shell
326 should generate its own completions.
332 Please report any bugs or feature requests on the bugtracker website
333 L<https://github.com/chazmcgarvey/git-codeowners/issues>
335 When submitting a bug or request, please include a test-file or a
336 patch to an existing test-file that illustrates the bug or desired
341 Charles McGarvey <chazmcgarvey@brokenzipper.com>
343 =head1 COPYRIGHT AND LICENSE
345 This software is copyright (c) 2019 by Charles McGarvey.
347 This is free software; you can redistribute it and/or modify it under
348 the same terms as the Perl 5 programming language system itself.
This page took 0.050429 seconds and 4 git commands to generate.