Getopt.pm 2.7 KB

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