#!perl

our $DATE = '2014-12-27'; # DATE
our $VERSION = '0.01'; # VERSION

BEGIN { $ENV{TRACE} = 1 }

use 5.010;
use strict;
use warnings;
use Log::Any '$log';

use File::Which qw(which);
use Getopt::Long;
use Log::Any::Adapter qw(ScreenColoredLevel);

my @inc;
Getopt::Long::Configure(
    'no_ignore_case', 'bundling', 'no_permute', 'pass_through',
    'auto_help', 'auto_version');
GetOptions(
    'include|I=s' => sub {
        unshift @inc, $_[1];
    },
);

@ARGV or die "testcomp: Please specify script to run as first argument\n";
my $script = shift @ARGV;
if (!(-f $script) && $script !~ m!/!) {
    my $script_p = which($script)
        or die "testcomp: Script '$script' not found in PATH\n";
    $script = $script_p;
}

my $comp_line = "$script " . join(" ", @ARGV);
my $comp_point;
if ($comp_line =~ /\^/) {
    $comp_point = index($comp_line, '^');
    $comp_line =~ s/\^//;
} else {
    $comp_point = length($comp_line);
}

my @cmd = ($^X, (map {"-I$_"} @inc),
           "-MLog::Any::Adapter=ScreenColoredLevel",
           $script);

$log->tracef("[testcomp] COMP_LINE=<%s>, COMP_POINT=%d",
             $comp_line, $comp_point);
$ENV{COMP_LINE}  = $comp_line;
$ENV{COMP_POINT} = $comp_point;
$log->tracef("[testcomp] exec(): %s", \@cmd);
exec @cmd;

# ABSTRACT: Test your tab completion
# PODNAME: testcomp

__END__

=pod

=encoding UTF-8

=head1 NAME

testcomp - Test your tab completion

=head1 VERSION

This document describes version 0.01 of testcomp (from Perl distribution App-CompleteUtils), released on 2014-12-27.

=head1 SYNOPSIS

Usage (place caret sign (C<^>) somewhere to place cursor):

 % testcomp [testcomp-options] -- <your-script> [your-script-options]^ ...
 % testcomp [testcomp-options] -- <your-script> '[your-script-options]^ ...'

Examples:

 % testcomp -- yourscript --opt1 val --opt2
 % testcomp -Ilib -- 'yourscript --opt1^  --opt2 val'

=head1 DESCRIPTION

This utility will run your script (finding it in C<PATH> if not found in current
directory) while setting C<COMP_LINE> and C<COMP_POINT> to test how your script
will perform shell completion. In addition to that, it will also load
L<Log::Any::Adapter::ScreenColoredLevel> and set C<TRACE=1> to let you see trace
log messages.

You can place caret sign (C<^>) somewhere in the argument to place cursor (set
C<COMP_POINT>)>. If no caret sign is present, C<COMP_POINT> is put at the end of
C<COMP_LINE>.

=head1 OPTIONS

=head2 --include=dir, -I

Like Perl's C<-I>.

=head1 SEE ALSO

L<App::CompleteUtils>

=head1 HOMEPAGE

Please visit the project's homepage at L<https://metacpan.org/release/App-CompleteUtils>.

=head1 SOURCE

Source repository is at L<https://github.com/perlancar/perl-App-CompleteUtils>.

=head1 BUGS

Please report any bugs or feature requests on the bugtracker website L<https://rt.cpan.org/Public/Dist/Display.html?Name=App-CompleteUtils>

When submitting a bug or request, please include a test-file or a
patch to an existing test-file that illustrates the bug or desired
feature.

=head1 AUTHOR

perlancar <perlancar@cpan.org>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2014 by perlancar@cpan.org.

This is free software; you can redistribute it and/or modify it under
the same terms as the Perl 5 programming language system itself.

=cut
