Debian Codegraph @

lib/Autom4te/Getopt.pm

raw
1 # Copyright (C) 2012-2025 Free Software Foundation, Inc.
2
3 # This program is free software: you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation, either version 3 of the License, or
6 # (at your option) any later version.
7
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
12
13 # You should have received a copy of the GNU General Public License
14 # along with this program. If not, see <https://www.gnu.org/licenses/>.
15
16 ##################################################################
17 # The master copy of this file is in Automake's source repository.
18 # Please send updates to [email protected].
19 ##################################################################
20
21 package Autom4te::Getopt;
22
23 =head1 NAME
24
25 Autom4te::Getopt - GCS conforming parser for command line options
26
27 =head1 SYNOPSIS
28
29 use Autom4te::Getopt;
30
31 =head1 DESCRIPTION
32
33 Export a function C<parse_options>, performing parsing of command
34 line options in conformance to the GNU Coding standards.
35
36 =cut
37
38 use 5.006; use strict; use warnings;
39
40 use Carp qw (confess croak);
41 use Exporter ();
42 use Getopt::Long ();
43
44 use Autom4te::ChannelDefs qw (fatal);
45
46 our @ISA = qw (Exporter);
47 our @EXPORT = qw (getopt);
48
49 =item C<parse_options (%option)>
50
51 Wrapper around C<Getopt::Long>, trying to conform to the GNU
52 Coding Standards for error messages.
53
54 =cut
55
56 sub parse_options (%)
57 {
58 my %option = @_;
59
60 Getopt::Long::Configure ("bundling", "pass_through");
61 # Unrecognized options are passed through, so GetOption can only fail
62 # due to internal errors or misuse of options specification.
63 Getopt::Long::GetOptions (%option)
64 or confess "error in options specification (likely)";
65
66 if (@ARGV && $ARGV[0] =~ /^-./)
67 {
68 my %argopts;
69 for my $k (keys %option) # sort keys not needed
70 {
71 if ($k =~ /(.*)=s$/)
72 {
73 map { $argopts{(length ($_) == 1)
74 ? "-$_" : "--$_" } = 1; } (split (/\|/, $1));
75 }
76 }
77 if ($ARGV[0] eq '--')
78 {
79 shift @ARGV;
80 }
81 elsif (exists $argopts{$ARGV[0]})
82 {
83 fatal ("option '$ARGV[0]' requires an argument\n"
84 . "Try '$0 --help' for more information.");
85 }
86 else
87 {
88 fatal ("unrecognized option '$ARGV[0]'.\n"
89 . "Try '$0 --help' for more information.");
90 }
91 }
92 }
93
94 =back
95
96 =head1 SEE ALSO
97
98 L<Getopt::Long>
99
100 =cut
101
102 1; # for require