App-Codeowners

 view release on metacpan or  search on metacpan

lib/App/Codeowners/Options.pm  view on Meta::CPAN

# 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;

our $VERSION = '0.51'; # VERSION

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,
        'help|h|?'              => 0,
        'manual|man'            => 0,
        'shell-completion:s'    => undef,
        'version|v'             => 0,
    };
}

sub _command_options {
    return {
        'create'    => {},
        'owners'    => {
            'pattern=s' => '',
        },
        'patterns'  => {
            'owner=s'   => '',
        },
        'projects'  => {},
        'show'      => {
            'owner=s@'          => [],
            'pattern=s@'        => [],
            'project=s@'        => [],
            'patterns!'         => 0,
            'projects!'         => undef,
            'expand-aliases!'   => 0,
        },
        'update'    => {},
    };
}

sub _commands {
    my $self = shift;
    my @commands = sort keys %{$self->_command_options};
    return @commands;
}

sub _options {
    my $self = shift;
    my @command_options;
    if (my $command = $self->{command}) {
        @command_options = keys %{$self->_command_options->{$command} || {}};
    }
    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,
        config  => 'pass_through',
    ) or _pod2usage(2);

    if ($ENV{CODEOWNERS_COMPLETIONS}) {
        $self->{command} = $args[0] || '';
        my $cword = $ENV{CWORD};
        my $cur   = $ENV{CUR} || '';
        # Adjust cword to remove progname
        while (0 < --$cword) {
            last if $cur eq ($args_copy[$cword] || '');
        }
        $self->completions($cword, @args_copy);
        exit 0;
    }

    if ($opts->{version}) {
        my $progname = path($0)->basename;
        print "${progname} ${VERSION}\n";
        exit 0;
    }
    if ($opts->{help}) {
        _pod2usage(-exitval => 0, -verbose => 99, -sections => [qw(NAME SYNOPSIS OPTIONS COMMANDS)]);
    }
    if ($opts->{manual}) {
        _pod2usage(-exitval => 0, -verbose => 2);



( run in 0.720 second using v1.01-cache-2.11-cpan-5b529ec07f3 )