diff options
-rw-r--r-- | aclocal.in | 54 | ||||
-rw-r--r-- | automake.in | 56 | ||||
-rw-r--r-- | lib/Automake/Getopt.pm | 115 | ||||
-rw-r--r-- | lib/Automake/Makefile.am | 1 | ||||
-rwxr-xr-x | tests/getopt.test | 39 | ||||
-rw-r--r-- | tests/list-of-tests.mk | 1 |
6 files changed, 125 insertions, 141 deletions
diff --git a/aclocal.in b/aclocal.in index 93ed5eb71..8cffe7116 100644 --- a/aclocal.in +++ b/aclocal.in @@ -944,6 +944,8 @@ sub parse_arguments () my %cli_options = ( + 'help' => sub { usage(0); }, + 'version' => \&version, 'acdir=s' => \&handle_acdir_option, 'system-acdir=s' => sub { shift; @system_includes = @_; }, 'automake-acdir=s' => sub { shift; @automake_includes = @_; }, @@ -957,55 +959,9 @@ sub parse_arguments () 'verbose' => sub { setup_channel 'verb', silent => 0; }, 'W|warnings=s' => \&parse_warnings, ); - use Getopt::Long; - Getopt::Long::config ("bundling", "pass_through"); - - # See if --version or --help is used. We want to process these before - # anything else because the GNU Coding Standards require us to - # `exit 0' after processing these options, and we can't guarantee this - # if we treat other options first. (Handling other options first - # could produce error diagnostics, and in this condition it is - # confusing if aclocal does `exit 0'.) - my %cli_options_1st_pass = - ( - 'version' => \&version, - 'help' => sub { usage(0); }, - # Recognize all other options (and their arguments) but do nothing. - map { $_ => sub {} } (keys %cli_options) - ); - my @ARGV_backup = @ARGV; - Getopt::Long::GetOptions %cli_options_1st_pass - or exit 1; - @ARGV = @ARGV_backup; - - # Now *really* process the options. This time we know that --help - # and --version are not present, but we specify them nonetheless so - # that ambiguous abbreviation are diagnosed. - Getopt::Long::GetOptions %cli_options, 'version' => sub {}, 'help' => sub {} - or exit 1; - - if (@ARGV) - { - my %argopts; - for my $k (keys %cli_options) - { - if ($k =~ /(.*)=s$/) - { - map { $argopts{(length ($_) == 1) - ? "-$_" : "--$_" } = 1; } (split (/\|/, $1)); - } - } - if (exists $argopts{$ARGV[0]}) - { - fatal ("option `$ARGV[0]' requires an argument.\n" - . "Try `$0 --help' for more information"); - } - else - { - fatal ("unrecognized option `$ARGV[0]'\n" - . "Try `$0 --help' for more information"); - } - } + + use Automake::Getopt (); + Automake::Getopt::parse_options %cli_options; if ($print_and_exit) { diff --git a/automake.in b/automake.in index bf9f0c064..df502386c 100644 --- a/automake.in +++ b/automake.in @@ -8331,6 +8331,8 @@ sub parse_arguments () my %cli_options = ( + 'version' => \&version, + 'help' => \&usage, 'libdir=s' => \$libdir, 'gnu' => sub { $strict = 'gnu'; }, 'gnits' => sub { $strict = 'gnits'; }, @@ -8345,32 +8347,9 @@ sub parse_arguments () 'v|verbose' => sub { setup_channel 'verb', silent => 0; }, 'W|warnings=s' => \@warnings, ); - use Getopt::Long; - Getopt::Long::config ("bundling", "pass_through"); - - # See if --version or --help is used. We want to process these before - # anything else because the GNU Coding Standards require us to - # `exit 0' after processing these options, and we can't guarantee this - # if we treat other options first. (Handling other options first - # could produce error diagnostics, and in this condition it is - # confusing if Automake does `exit 0'.) - my %cli_options_1st_pass = - ( - 'version' => \&version, - 'help' => \&usage, - # Recognize all other options (and their arguments) but do nothing. - map { $_ => sub {} } (keys %cli_options) - ); - my @ARGV_backup = @ARGV; - Getopt::Long::GetOptions %cli_options_1st_pass - or exit 1; - @ARGV = @ARGV_backup; - # Now *really* process the options. This time we know that --help - # and --version are not present, but we specify them nonetheless so - # that ambiguous abbreviation are diagnosed. - Getopt::Long::GetOptions %cli_options, 'version' => sub {}, 'help' => sub {} - or exit 1; + use Automake::Getopt (); + Automake::Getopt::parse_options %cli_options; set_strictness ($strict); my $cli_where = new Automake::Location; @@ -8383,33 +8362,6 @@ sub parse_arguments () return unless @ARGV; - if ($ARGV[0] =~ /^-./) - { - my %argopts; - for my $k (keys %cli_options) - { - if ($k =~ /(.*)=s$/) - { - map { $argopts{(length ($_) == 1) - ? "-$_" : "--$_" } = 1; } (split (/\|/, $1)); - } - } - if ($ARGV[0] eq '--') - { - shift @ARGV; - } - elsif (exists $argopts{$ARGV[0]}) - { - fatal ("option `$ARGV[0]' requires an argument.\n" - . "Try `$0 --help' for more information"); - } - else - { - fatal ("unrecognized option `$ARGV[0]'.\n" - . "Try `$0 --help' for more information"); - } - } - my $errspec = 0; foreach my $arg (@ARGV) { diff --git a/lib/Automake/Getopt.pm b/lib/Automake/Getopt.pm new file mode 100644 index 000000000..84cee5e4a --- /dev/null +++ b/lib/Automake/Getopt.pm @@ -0,0 +1,115 @@ +# Copyright (C) 2012 Free Software Foundation, Inc. + +# This program is free software: you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation, either version 3 of the License, or +# (at your option) any later version. + +# This program is distributed in the hope that it will be useful, +# but WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. + +# You should have received a copy of the GNU General Public License +# along with this program. If not, see <http://www.gnu.org/licenses/>. + +package Automake::Getopt; + +=head1 NAME + +Automake::Getopt - GCS conforming parser for command line options + +=head1 SYNOPSIS + + use Automake::Getopt; + +=head1 DESCRIPTION + +Export a function C<parse_options>, performing parsing of command +line options in conformance to the GNU Coding standards. + +=cut + +use 5.006_002; +use strict; +use warnings FATAL => 'all'; +use Exporter (); +use Getopt::Long (); +use Automake::ChannelDefs qw/fatal/; +use Carp qw/croak confess/; + +use vars qw (@ISA @EXPORT); +@ISA = qw (Exporter); +@EXPORT= qw/getopt/; + +=item C<parse_options (%option)> + +Wrapper around C<Getopt::Long>, trying to conform to the GNU +Coding Standards for error messages. + +=cut + +sub parse_options (%) +{ + my %option = @_; + + Getopt::Long::Configure ("bundling", "pass_through"); + # Unrecognized options are passed through, so GetOption can only fail + # due to internal errors or misuse of options specification. + Getopt::Long::GetOptions (%option) + or confess "error in options specification (likely)"; + + if (@ARGV && $ARGV[0] =~ /^-./) + { + my %argopts; + for my $k (keys %option) + { + if ($k =~ /(.*)=s$/) + { + map { $argopts{(length ($_) == 1) + ? "-$_" : "--$_" } = 1; } (split (/\|/, $1)); + } + } + if ($ARGV[0] eq '--') + { + shift @ARGV; + } + elsif (exists $argopts{$ARGV[0]}) + { + fatal ("option `$ARGV[0]' requires an argument\n" + . "Try `$0 --help' for more information."); + } + else + { + fatal ("unrecognized option `$ARGV[0]'.\n" + . "Try `$0 --help' for more information."); + } + } +} + +=back + +=head1 SEE ALSO + +L<Getopt::Long> + +=cut + +1; # for require + +### Setup "GNU" style for perl-mode and cperl-mode. +## Local Variables: +## perl-indent-level: 2 +## perl-continued-statement-offset: 2 +## perl-continued-brace-offset: 0 +## perl-brace-offset: 0 +## perl-brace-imaginary-offset: 0 +## perl-label-offset: -2 +## cperl-indent-level: 2 +## cperl-brace-offset: 0 +## cperl-continued-brace-offset: 0 +## cperl-label-offset: -2 +## cperl-extra-newline-before-brace: t +## cperl-merge-trailing-else: nil +## cperl-continued-statement-offset: 2 +## End: diff --git a/lib/Automake/Makefile.am b/lib/Automake/Makefile.am index 980502462..ac9356a49 100644 --- a/lib/Automake/Makefile.am +++ b/lib/Automake/Makefile.am @@ -25,6 +25,7 @@ dist_perllib_DATA = \ DisjConditions.pm \ FileUtils.pm \ General.pm \ + Getopt.pm \ Item.pm \ ItemDef.pm \ Location.pm \ diff --git a/tests/getopt.test b/tests/getopt.test deleted file mode 100755 index 41aca0ca3..000000000 --- a/tests/getopt.test +++ /dev/null @@ -1,39 +0,0 @@ -#! /bin/sh -# Copyright (C) 2002, 2003, 2008, 2011 Free Software Foundation, Inc. -# -# This program is free software; you can redistribute it and/or modify -# it under the terms of the GNU General Public License as published by -# the Free Software Foundation; either version 2, or (at your option) -# any later version. -# -# This program is distributed in the hope that it will be useful, -# but WITHOUT ANY WARRANTY; without even the implied warranty of -# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -# GNU General Public License for more details. -# -# You should have received a copy of the GNU General Public License -# along with this program. If not, see <http://www.gnu.org/licenses/>. - -# Automake --help, and --version should have priority over any other option -# so that their `Exit 0' is coherent. - -. ./defs || Exit 1 - -# This is expected to fail ... -AUTOMAKE_fails -Wnonexistent -grep ':.*nonexistent' stderr - -# ... but this should not. -AUTOMAKE_run -Wnonexistent --help -grep ':.*nonexistent' stderr && Exit 1 - - -# Similarly, this should fail ... -AUTOMAKE_fails --nonexistent -grep ':.*nonexistent' stderr - -# ... but this should not. -AUTOMAKE_run --nonexistent --help -grep ':.*nonexistent' stderr && Exit 1 - -: diff --git a/tests/list-of-tests.mk b/tests/list-of-tests.mk index 8f7c4905f..31d70a76b 100644 --- a/tests/list-of-tests.mk +++ b/tests/list-of-tests.mk @@ -437,7 +437,6 @@ gcj3.test \ gcj4.test \ gcj5.test \ gcj6.test \ -getopt.test \ gettext.test \ gettext2.test \ gettext3.test \ |