Blame lib/Autom4te/Getopt.pm

Packit 47b4ca
# Copyright (C) 2012 Free Software Foundation, Inc.
Packit 47b4ca
Packit 47b4ca
# This program is free software: you can redistribute it and/or modify
Packit 47b4ca
# it under the terms of the GNU General Public License as published by
Packit 47b4ca
# the Free Software Foundation, either version 3 of the License, or
Packit 47b4ca
# (at your option) any later version.
Packit 47b4ca
Packit 47b4ca
# This program is distributed in the hope that it will be useful,
Packit 47b4ca
# but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 47b4ca
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
Packit 47b4ca
# GNU General Public License for more details.
Packit 47b4ca
Packit 47b4ca
# You should have received a copy of the GNU General Public License
Packit 47b4ca
# along with this program.  If not, see <http://www.gnu.org/licenses/>.
Packit 47b4ca
Packit 47b4ca
package Autom4te::Getopt;
Packit 47b4ca
Packit 47b4ca
=head1 NAME
Packit 47b4ca
Packit 47b4ca
Autom4te::Getopt - GCS conforming parser for command line options
Packit 47b4ca
Packit 47b4ca
=head1 SYNOPSIS
Packit 47b4ca
Packit 47b4ca
  use Autom4te::Getopt;
Packit 47b4ca
Packit 47b4ca
=head1 DESCRIPTION
Packit 47b4ca
Packit 47b4ca
Export a function C<parse_options>, performing parsing of command
Packit 47b4ca
line options in conformance to the GNU Coding standards.
Packit 47b4ca
Packit 47b4ca
=cut
Packit 47b4ca
Packit 47b4ca
use 5.006;
Packit 47b4ca
use strict;
Packit 47b4ca
use warnings FATAL => 'all';
Packit 47b4ca
use Exporter ();
Packit 47b4ca
use Getopt::Long ();
Packit 47b4ca
use Autom4te::ChannelDefs qw/fatal/;
Packit 47b4ca
use Carp qw/croak confess/;
Packit 47b4ca
Packit 47b4ca
use vars qw (@ISA @EXPORT);
Packit 47b4ca
@ISA = qw (Exporter);
Packit 47b4ca
@EXPORT= qw/getopt/;
Packit 47b4ca
Packit 47b4ca
=item C<parse_options (%option)>
Packit 47b4ca
Packit 47b4ca
Wrapper around C<Getopt::Long>, trying to conform to the GNU
Packit 47b4ca
Coding Standards for error messages.
Packit 47b4ca
Packit 47b4ca
=cut
Packit 47b4ca
Packit 47b4ca
sub parse_options (%)
Packit 47b4ca
{
Packit 47b4ca
  my %option = @_;
Packit 47b4ca
Packit 47b4ca
  Getopt::Long::Configure ("bundling", "pass_through");
Packit 47b4ca
  # Unrecognized options are passed through, so GetOption can only fail
Packit 47b4ca
  # due to internal errors or misuse of options specification.
Packit 47b4ca
  Getopt::Long::GetOptions (%option)
Packit 47b4ca
    or confess "error in options specification (likely)";
Packit 47b4ca
Packit 47b4ca
  if (@ARGV && $ARGV[0] =~ /^-./)
Packit 47b4ca
    {
Packit 47b4ca
      my %argopts;
Packit 47b4ca
      for my $k (keys %option)
Packit 47b4ca
	{
Packit 47b4ca
	  if ($k =~ /(.*)=s$/)
Packit 47b4ca
	    {
Packit 47b4ca
	      map { $argopts{(length ($_) == 1)
Packit 47b4ca
			     ? "-$_" : "--$_" } = 1; } (split (/\|/, $1));
Packit 47b4ca
	    }
Packit 47b4ca
	}
Packit 47b4ca
      if ($ARGV[0] eq '--')
Packit 47b4ca
	{
Packit 47b4ca
	  shift @ARGV;
Packit 47b4ca
	}
Packit 47b4ca
      elsif (exists $argopts{$ARGV[0]})
Packit 47b4ca
	{
Packit 47b4ca
	  fatal ("option '$ARGV[0]' requires an argument\n"
Packit 47b4ca
		 . "Try '$0 --help' for more information.");
Packit 47b4ca
	}
Packit 47b4ca
      else
Packit 47b4ca
	{
Packit 47b4ca
	  fatal ("unrecognized option '$ARGV[0]'.\n"
Packit 47b4ca
		 . "Try '$0 --help' for more information.");
Packit 47b4ca
	}
Packit 47b4ca
    }
Packit 47b4ca
}
Packit 47b4ca
Packit 47b4ca
=back
Packit 47b4ca
Packit 47b4ca
=head1 SEE ALSO
Packit 47b4ca
Packit 47b4ca
L<Getopt::Long>
Packit 47b4ca
Packit 47b4ca
=cut
Packit 47b4ca
Packit 47b4ca
1; # for require
Packit 47b4ca
Packit 47b4ca
### Setup "GNU" style for perl-mode and cperl-mode.
Packit 47b4ca
## Local Variables:
Packit 47b4ca
## perl-indent-level: 2
Packit 47b4ca
## perl-continued-statement-offset: 2
Packit 47b4ca
## perl-continued-brace-offset: 0
Packit 47b4ca
## perl-brace-offset: 0
Packit 47b4ca
## perl-brace-imaginary-offset: 0
Packit 47b4ca
## perl-label-offset: -2
Packit 47b4ca
## cperl-indent-level: 2
Packit 47b4ca
## cperl-brace-offset: 0
Packit 47b4ca
## cperl-continued-brace-offset: 0
Packit 47b4ca
## cperl-label-offset: -2
Packit 47b4ca
## cperl-extra-newline-before-brace: t
Packit 47b4ca
## cperl-merge-trailing-else: nil
Packit 47b4ca
## cperl-continued-statement-offset: 2
Packit 47b4ca
## End: