#!perl

our $DATE = '2015-10-07'; # DATE
our $VERSION = '0.01'; # VERSION

use 5.010001;
use strict 'subs', 'vars';
use warnings;

use Getopt::Panjang qw(get_options);
use Sort::Sub ();

my %opts = (
    ignore_case => 0,
    reverse => 0,
);
my $res = get_options(
    spec => {
        'ignore_case|f' => sub { $opts{ignore_case} = 1 },
        'reverse|r'     => sub { $opts{reverse} = 1 },
        'help|h'        => sub {
            print <<'_';
sortsub - Sort lines of text using Sort::Sub routines

Usage:
  % subsort [OPTIONS] <ROUTINE> [FILE]...
  % subsort --help (or -h)
  % subsort --list (or -l)
  % subsort --version (or -v)

Options:
  --help, -h         Show this help message and exit.
  --version, -v      Show version and exit.
  --list, -l         List available Sort::Sub routines.
  --ignore-case, -f  Do a case-insensitive sort.
  --reverse, -r      Do a reverse sort.
_
            exit 0;
        },
        'version|v'     => sub {
            no warnings 'once';
            say "subsort version ", ($main::VERSION // 'dev'),
                " (", ($main::DATE // 'no date'), ")";
            exit 0;
        },
        'list|l'        => sub {
            require PERLANCAR::Module::List;
            my $mods = PERLANCAR::Module::List::list_modules(
                "Sort::Sub::", {list_modules=>1});
            for (sort keys %$mods) {
                s/.+:://;
                say $_;
            }
            exit 0;
        },
    },
);
die "$res->[1]\n" unless $res->[0] == 200;

@ARGV = @{ $res->[3]{'func.remaining_argv'} };
if (!@ARGV) {
    die "Please specify routine to use\n";
}
my $routine = shift @ARGV;

Sort::Sub->import("$routine<".
                      ($opts{ignore_case} ? "i":"").
                      ($opts{reverse} ? "r":"").
                      ">");
for (sort {&{$routine}} map {chomp;$_} <>) {
    say $_;
}

# ABSTRACT: Sort lines of text using Sort::Sub routines
# PODNAME: subsort

__END__

=pod

=encoding UTF-8

=head1 NAME

subsort - Sort lines of text using Sort::Sub routines

=head1 VERSION

This document describes version 0.01 of subsort (from Perl distribution App-subsort), released on 2015-10-07.

=head1 SYNOPSIS

 % subsort [OPTIONS] <ROUTINE> [FILE]...

To list all available routines:

 % subsort -l

=head1 DESCRIPTION

This program is like the Unix command B<sort>, but it uses routines from
L<Sub::Sort>.

=head1 OPTIONS

  --help, -h         Show this help message and exit.
  --version, -v      Show version and exit.
  --list, -l         List available Sort::Sub routines.
  --ignore-case, -f  Do a case-insensitive sort.
  --reverse, -r      Do a reverse sort.

=head1 SEE ALSO

The B<sort> Unix command.

L<App::psort>

=head1 HOMEPAGE

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

=head1 SOURCE

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

=head1 BUGS

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

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) 2015 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
