#!/usr/bin/env perl
package Test::Cool;
use Getopt::App -complete;
use File::Spec::Functions qw(catdir catfile rel2abs updir);
use File::Basename qw(dirname);

# getopt_subcommands() is called by Getopt::App
sub getopt_subcommands {
  my $app = shift;

  # Can also use File::Share or $INC{'My/Module.pm'} to locate commands
  my $dir = catdir dirname(__FILE__), updir, qw(lib Test Cool commands);

  opendir(my ($DH), $dir) or die $!;
  return [map { [s!\.pl$!!r, rel2abs(catfile $dir, $_), "Try $_"] }
      sort grep {/\.pl$/} readdir $DH];
}

run(
  'h                 # Print help',
  'completion-script # Print autocomplete script',
  sub {
    my ($app, @args) = @_;
    return print generate_completion_script() if $app->{'completion-script'};
    return print extract_usage()              if $app->{h};
    say __FILE__;
    return 10;
  }
);
