App-BoolFindGrep

 view release on metacpan or  search on metacpan

lib/App/BoolFindGrep/CLI.pm  view on Meta::CPAN

package App::BoolFindGrep::CLI;

use common::sense;
use charnames q(:full);
use English qw[-no_match_vars];
use Moo;
use App::BoolFindGrep;

our $VERSION = '0.06'; # VERSION

has args   => ( is => q(rw), default => sub { {}; }, );
has result => ( is => q(rw), default => sub { []; }, );
has files_with_matches => ( is => q(rw) );

sub process {
    my $self = shift;

    my $controller = App::BoolFindGrep->new( $self->args() );
    local $EVAL_ERROR;
    eval { $controller->process(); };
    if ($EVAL_ERROR) {
        if ($EVAL_ERROR =~    #
            m{\A(?<msg>.*?)
              \sat\s
              (?<module>.*?)
              \sline\s
              (?<line>\d+)\.\s*\z
             }msx
            )
        {
            say STDERR sprintf q(%s: %s), $PROGRAM_NAME,
                $LAST_PAREN_MATCH{msg};
        }
        return;
    } ## end if ($EVAL_ERROR)

    if ( defined $self->args->{match_expr} ) {
        my ( %result, $num_max );
        foreach my $file ( sort @{ $controller->greped_files() } ) {
            next unless exists $controller->grep->content_found->{$file};
            foreach my $number (
                keys %{ $controller->grep->content_found->{$file} } )
            {
                if ( !( defined $num_max ) || $number > $num_max ) {
                    $num_max = $number;
                }
                $result{$file}{$number}
                    = $controller->grep->content_found->{$file}{$number};
            }
        }
        foreach my $file ( sort keys %result ) {
            if ( $self->files_with_matches() ) {
                push @{ $self->result() }, $file;
                next;
            }
            foreach my $number ( sort { $a <=> $b } keys %{ $result{$file} } )
            {
                my $string = $result{$file}{$number};
                my $result = sprintf q(%s:%.*d:%s),     #
                    $file, length $num_max, $number, $string;
                push @{ $self->result() }, $result;
            }
        }

    } ## end if ( defined $self->args...)
    else { @{ $self->result() } = @{ $controller->found_files() }; }

    @{ $self->result() } = sort @{ $self->result() };

    return 1;
} ## end sub process

sub args_checker {
    my $self = shift;
    my %arg  = splice @_;

    $self->_args_translator(%arg);

    my %rule = (
        q(are mutually exclusive.) => [
            [ qw[file_expr files_from], ],
            [ qw[line_regexp word_regexp], ],
            [ qw[directory files_from], ],
            [ qw[fixed_strings glob_regexp], ],
        ],
        q(an empty value was given.) =>
            [qw[file_delim file_expr files_from match_expr directory]],
        q(implies) => [
            { find_type          => [q(file_expr)] },
            { find_ignore_case   => [q(file_expr)] },
            { files_delim        => [q(files_from)] },

lib/App/BoolFindGrep/CLI.pm  view on Meta::CPAN

        } ## end if ( !/\A(?:-|stdin)\z/i)
    } ## end foreach ($value)

    if ( defined $msg ) { $self->_msg($msg); return; }

    $self->args->{files_delim} = qq(\N{LINE FEED});

    return 1;
} ## end sub _files_from_checker

sub _find_type_checker {
    my $self  = shift;
    my $value = shift;

    my $parameter = ( split m{::_(\S+)_checker\z}msx, ( caller 0 )[3] )[-1];
    my %type = (
        glob    => 1,    #
        literal => 1,    #
        regexp  => 1,    #
    );

    unless ( exists $type{$value} ) {
        $self->_msg( sprintf q('--%s' => argument invalid '%s'.),
            $parameter, $value );
        return;
    }

    return 1;
} ## end sub _find_type_checker

sub _directory_checker {
    my $self   = shift;
    my $values = shift;

    my $parameter = ( split m{::_(\S+)_checker\z}msx, ( caller 0 )[3] )[-1];

    my $msg;
    foreach ( @{$values} ) {
        if ( !-e ) {
            $msg = sprintf q('--%s' => nonexistent directory '%s'.),
                $parameter, $_;
        }
        elsif ( !-d ) {
            $msg = sprintf q('--%s' => non-directory argument '%s'.),
                $parameter, $_;
        }
        elsif ( !-r ) {
            $msg = sprintf q('--%s' => unreadable directory '%s'.),
                $parameter, $_;
        }
        if ( defined $msg ) { $self->_msg($msg); return; }
    } ## end foreach ( @{$values} )

    return 1;
} ## end sub _directory_checker

sub _msg {
    my $self = shift;
    my $msg  = shift;

    say STDERR sprintf q(%s: %s), $PROGRAM_NAME, $msg;
    say STDERR sprintf q(Try '%s --help' for more information.),
        $PROGRAM_NAME;

    return 1;
}

no Moo;
__PACKAGE__->meta->make_immutable;

1;

__END__

=pod

=encoding utf8

=head1 NAME

App::BoolFindGrep::CLI - Command Line Interface functions.

=head1 VERSION

version 0.06

=head1 SYNOPSIS

=head1 DESCRIPTION

=head1 METHODS

=head2 args

Hash reference with command line arguments.

=head2 args_checker

Process, transform and validade arguments.

=head2 files

Array reference with searching results.

=head2 process

Does the work.

=head1 OPTIONS

=head1 ERRORS

=head1 DIAGNOSTICS

=head1 EXAMPLES

=head1 ENVIRONMENT

=head1 FILES

=head1 CAVEATS



( run in 3.422 seconds using v1.01-cache-2.11-cpan-d7a12ab2c7f )