package App::Codeowners::Options;
# ABSTRACT: Getopt and shell completion for App::Codeowners
+use v5.10.1;
use warnings;
use strict;
+use Encode qw(decode);
use Getopt::Long 2.39 ();
use Path::Tiny;
-use Pod::Usage;
-our $VERSION = '0.42'; # VERSION
+our $VERSION = '0.50'; # VERSION
-sub early_options {
+sub _pod2usage {
+ eval { require Pod::Usage };
+ if ($@) {
+ my $ref = $VERSION eq '9999.999' ? 'master' : "v$VERSION";
+ my $exit = (@_ == 1 && $_[0] =~ /^\d+$/ && $_[0]) //
+ (@_ % 2 == 0 && {@_}->{'-exitval'}) // 2;
+ print STDERR <<END;
+Online documentation is available at:
+
+ https://github.com/chazmcgarvey/git-codeowners/blob/$ref/README.md
+
+Tip: To enable inline documentation, install the Pod::Usage module.
+
+END
+ exit $exit;
+ }
+ else {
+ Pod::Usage::pod2usage(@_);
+ }
+}
+
+sub _early_options {
return {
'color|colour!' => (-t STDOUT ? 1 : 0), ## no critic (InputOutput::ProhibitInteractiveTest)
'format|f=s' => undef,
};
}
-sub command_options {
+sub _command_options {
return {
'create' => {},
'owners' => {
};
}
-sub commands {
+sub _commands {
my $self = shift;
- my @commands = sort keys %{$self->command_options};
+ my @commands = sort keys %{$self->_command_options};
return @commands;
}
-sub options {
+sub _options {
my $self = shift;
my @command_options;
if (my $command = $self->{command}) {
- @command_options = keys %{$self->command_options->{$command} || {}};
+ @command_options = keys %{$self->_command_options->{$command} || {}};
}
- return (keys %{$self->early_options}, @command_options);
+ return (keys %{$self->_early_options}, @command_options);
}
+
sub new {
my $class = shift;
my @args = @_;
+ # assume UTF-8 args if non-ASCII
+ @args = map { decode('UTF-8', $_) } @args if grep { /\P{ASCII}/ } @args;
+
my $self = bless {}, $class;
my @args_copy = @args;
my $opts = $self->get_options(
args => \@args,
- spec => $self->early_options,
+ spec => $self->_early_options,
config => 'pass_through',
- ) or pod2usage(2);
+ ) or _pod2usage(2);
if ($ENV{CODEOWNERS_COMPLETIONS}) {
$self->{command} = $args[0] || '';
exit 0;
}
if ($opts->{help}) {
- pod2usage(-exitval => 0, -verbose => 99, -sections => [qw(NAME SYNOPSIS OPTIONS COMMANDS)]);
+ _pod2usage(-exitval => 0, -verbose => 99, -sections => [qw(NAME SYNOPSIS OPTIONS COMMANDS)]);
}
if ($opts->{manual}) {
- pod2usage(-exitval => 0, -verbose => 2);
+ _pod2usage(-exitval => 0, -verbose => 2);
}
if (defined $opts->{shell_completion}) {
$self->shell_completion($opts->{shell_completion});
# figure out the command (or default to "show")
my $command = shift @args;
- my $command_options = $self->command_options->{$command || ''};
+ my $command_options = $self->_command_options->{$command || ''};
if (!$command_options) {
unshift @args, $command if defined $command;
$command = 'show';
- $command_options = $self->command_options->{$command};
+ $command_options = $self->_command_options->{$command};
}
my $more_opts = $self->get_options(
args => \@args,
spec => $command_options,
- ) or pod2usage(2);
+ ) or _pod2usage(2);
%$self = (%$opts, %$more_opts, command => $command, args => \@args);
return $self;
}
+
sub command {
my $self = shift;
my $command = $self->{command};
- my @commands = sort keys %{$self->command_options};
+ my @commands = sort keys %{$self->_command_options};
return if not grep { $_ eq $command } @commands;
$command =~ s/[^a-z]/_/g;
return $command;
}
+
sub args {
my $self = shift;
return @{$self->{args} || []};
}
else {
if (!$self->command) {
- $reply = [$self->commands, @{$self->_completion_options([keys %{$self->early_options}])}];
+ $reply = [$self->_commands, @{$self->_completion_options([keys %{$self->_early_options}])}];
}
else {
print 'file';
sub _completion_options {
my $self = shift;
- my $opts = shift || [$self->options];
+ my $opts = shift || [$self->_options];
my @options;
=head1 VERSION
-version 0.42
+version 0.50
=head1 METHODS
+=head2 new
+
+ $options = App::Codeowners::Options->new(@ARGV);
+
+Construct a new object.
+
+=head2 command
+
+ $str = $options->command;
+
+Get the command specified by args provided when the object was created.
+
+=head2 args
+
+ $args = $options->args;
+
+Get the args provided when the object was created.
+
=head2 get_options
$options = $options->get_options(
=head1 COPYRIGHT AND LICENSE
-This software is copyright (c) 2019 by Charles McGarvey.
+This software is copyright (c) 2021 by Charles McGarvey.
This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.