#!/usr/bin/env perl

use strict;
use warnings;

use Getopt::Long;

Getopt::Long::Configure('bundling');

my %opts = ( verbosity => 1 );
GetOptions( \%opts, 'help|h', 'leading|l', 'trailing|t', ) or usage();
usage() if $opts{help};

sub usage {
    warn @_, "\n" if @_;

    ( my $cmd_name = $0 ) =~ s,.*/,,;

    die <<EOUSAGE;
Usage: $cmd_name [options]
Trims leading/trailing whitespace from each line.

Options:
  -l, --leading        Only trim leading whitespace
  -t, --trailing       Only trim trailing whitespace
  -h, --help           Show this help
EOUSAGE
}

if ( !$opts{leading} && !$opts{trailing} ) {

    # Default to trimming both if no options specified.
    $opts{leading} = $opts{trailing} = 1;
}

my $to_trim = qr/[ \t]+/;

while (<>) {
    s/^$to_trim// if $opts{leading};
    s/$to_trim$// if $opts{trailing};
    print;
}
