The Perl Toolchain Summit 2025 Needs You: You can help 🙏 Learn more

#!/usr/bin/env perl
# -*- mode: perl; coding: utf-8 -*-
use strict;
use warnings qw(FATAL all NONFATAL misc);
use FindBin; BEGIN {do "$FindBin::RealBin/libdir.pl"}
#----------------------------------------
use 5.010;
(()
, [cf_help => doc => "show help messages"]
, [cf_script_path => doc => "script search path for yatt subcommands"]
, [cf_debug => doc => "run in perldebugger"]
);
# XXX: Should use doc strings in help message.
use YATT::Lite::Util::CmdLine qw/parse_opts/;
use List::Util qw/uniq/;
{
my MY $self = MY->new(MY->parse_opts(\@ARGV, undef, {h => "help", d => "debug"}));
my $cmd = shift // 'help';
if (my $sub = $self->can("cmd_$cmd")) {
$sub->($self, @ARGV);
} elsif (my $fn = $self->subcmd_path($cmd)) {
if ($self->{cf_debug}) {
exec $^X, "-d", $fn, @ARGV or die "couldn't exec $fn: $!";
} else {
exec $fn, @ARGV or die "couldn't exec $fn: $!";
}
} else {
$self->usage("No such command: $cmd");
}
}
sub usage {
(my MY $self, my $message) = @_;
print STDERR "$message\n" if $message;
$self->cmd_help;
}
sub cmd_help {
my MY $self = shift;
print STDERR <<END;
Usage: @{[basename $0]} [--option=value] <command> [<args>]
Available commands are:
END
print STDERR " ", join("\n ", $self->subcmd_list), "\n";
exit 1;
}
sub subcmd_list {
(my MY $self) = @_;
my $prefix = "$self->{cf_script_path}yatt";
my %hidden; $hidden{$_} = 1
for qw(backend call command dispatcher
lintany lintpm lintrc
xgettext xhftest);
grep {
not $hidden{$_}
}
uniq sort map {
my $rest = substr($_, length($prefix));
if ($rest =~ s/^\.//) {
$rest;
} elsif ($rest =~ /^-([-\w]+)/) {
$1;
} else {
();
}
}
grep {-x}
glob("$prefix*");
}
sub subcmd_path {
(my MY $self, my $subcmd) = @_;
foreach my $fnPat (qw(yatt-%s* yatt.%s)) {
my $glob = $self->{cf_script_path}. sprintf($fnPat, $subcmd);
foreach my $fn (glob($glob)) {
return $fn if -x $fn;
}
}
}
sub find_libdir {
shift;
my $pm = 'YATT/Lite.pm';
substr($INC{$pm}, 0, -1-length($pm));
}
sub after_new {
(my MY $self) = @_;
$self->{cf_script_path} //= $self->find_libdir . "/YATT/scripts/";
}