#!/usr/bin/perl

use 5.010;
use strict;
use warnings;

package Perinci::CmdLine::dux;
use Moo;
extends 'Perinci::CmdLine';

sub run_subcommand {
    my $self = shift;

    # supply in and out first from STDIN & STDOUT. XXX currently doesn't stream
    # at all. we need to create a tied array which backends to filehandle.

    my $in  = [<>];
    my $out = [];
    $self->{_args}{in}  = $in;
    $self->{_args}{out} = $out;

    $self->SUPER::run_subcommand(@_);
}

package main;
use Getopt::Long;


our $VERSION = '0.01'; # VERSION

my %opts = (
    library => [],
    subcommands => [],
);
Getopt::Long::Configure('pass_through', 'no_permute');
GetOptions(
    'library|I=s' => $opts{library},
    'help|h|?' => \$opts{help},
);

my $me = $0; $me =~ s!.+/!!;

if ($opts{help} || !@ARGV) {
    print <<USAGE;
$me - Run dux function on the command-line

Usage:
  $me --help
  $me [common options] <dux function> [function options]

*Common options* include: '--library' ('-I') to add directory to Perl search dir
(a la Perl's '-I'), can be specified multiple times.

Examples:
  Show usage for a dux function:
    % $me head --help

  Run dux function:
    % ls -l | $me head -n 3

USAGE
    exit 0;
}

for my $dir (@{ $opts{library} }) { require lib; lib->import($dir) }

my $cmd = Perinci::CmdLine::dux->new;

my $pkg = shift @ARGV;
$pkg =~ s!::!/!g;
my $func = $pkg; $func =~ s!.+/!!;
my $url  = "/Data/Unixish/$pkg/$func";
$cmd->url($url);
$cmd->program_name($func);
$cmd->run;

#ABSTRACT: Run dux function on the command-line
#PODNAME: dux


__END__
=pod

=head1 NAME

dux - Run dux function on the command-line

=head1 VERSION

version 0.01

=head1 SYNOPSIS

Type C<dux --help> for more help.

=head1 SEE ALSO

L<Data::Unixish>

=head1 AUTHOR

Steven Haryanto <stevenharyanto@gmail.com>

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 by Steven Haryanto.

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

