X-Git-Url: https://git.dogcows.com/gitweb?a=blobdiff_plain;f=lib%2FApp%2FCodeowners%2FOptions.pm;h=80d0a4e9fd43d62eeaa5ee308f9f444bd96d900b;hb=fd8bad44a70558edb1e643ec6f86cad1650700fa;hp=7b43b75cc5e5d0272af568bbaea4979cbfb4755c;hpb=3c7ed3d762399efb2d01121392f7115104095788;p=chaz%2Fgit-codeowners diff --git a/lib/App/Codeowners/Options.pm b/lib/App/Codeowners/Options.pm index 7b43b75..80d0a4e 100644 --- a/lib/App/Codeowners/Options.pm +++ b/lib/App/Codeowners/Options.pm @@ -1,16 +1,38 @@ 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 = '9999.999'; # 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 < (-t STDOUT ? 1 : 0), ## no critic (InputOutput::ProhibitInteractiveTest) 'format|f=s' => undef, @@ -21,7 +43,7 @@ sub early_options { }; } -sub command_options { +sub _command_options { return { 'create' => {}, 'owners' => { @@ -42,34 +64,45 @@ sub command_options { }; } -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); } +=method new + + $options = App::Codeowners::Options->new(@ARGV); + +Construct a new object. + +=cut + 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] || ''; @@ -89,10 +122,10 @@ sub new { 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}); @@ -101,31 +134,47 @@ sub new { # 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; } +=method command + + $str = $options->command; + +Get the command specified by args provided when the object was created. + +=cut + 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; } +=method args + + $args = $options->args; + +Get the args provided when the object was created. + +=cut + sub args { my $self = shift; return @{$self->{args} || []}; @@ -260,7 +309,7 @@ sub completions { } 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'; @@ -275,7 +324,7 @@ sub completions { sub _completion_options { my $self = shift; - my $opts = shift || [$self->options]; + my $opts = shift || [$self->_options]; my @options;