From: Ilya Zakharevich Date: Mon, 7 Apr 2003 17:07:15 +0000 (-0700) Subject: Implement support for --help and --version in Getopt::Std X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=commitdiff_plain;h=294d099eb0fb837b82ab70fe7f81a0b70a4fa5c9;p=p5sagit%2Fp5-mst-13.2.git Implement support for --help and --version in Getopt::Std Subject: Re: [PATCH 5.8.1 @19053] Getopt::Std Message-ID: <20030408000714.GA953@math.berkeley.edu> p4raw-id: //depot/perl@19171 --- diff --git a/lib/Getopt/Std.pm b/lib/Getopt/Std.pm index 4599ec5..99e9269 100644 --- a/lib/Getopt/Std.pm +++ b/lib/Getopt/Std.pm @@ -46,11 +46,33 @@ To allow programs to process arguments that look like switches, but aren't, both functions will stop processing switches when they see the argument C<-->. The C<--> will be removed from @ARGV. +=head1 C<--help> and C<--version> + +If C<-> is not a recognized switch letter, getopts() supports arguments +C<--help> and C<--version>. If C and/or +C are defined, they are called; the arguments are +the output file handle, the name of option-processing package, its version, +and the switches string. If the subroutines are not defined, an attempt is +made to generate intelligent messages; for best results, define $main::VERSION. + +Note that due to excessive paranoia, if $Getopt::Std::STANDARD_HELP_VERSION +isn't true (the default is false), then the messages are printed on STDERR, +and the processing continues after the messages are printed. This being +the opposite of the standard-conforming behaviour, it is strongly recommended +to set $Getopt::Std::STANDARD_HELP_VERSION to true. + +One can change the output file handle of the messages by setting +$Getopt::Std::OUTPUT_HELP_VERSION. One can print the messages of C<--help> +(without the C line) and C<--version> by calling functions help_mess() +and version_mess() with the switches string as an argument. + =cut @ISA = qw(Exporter); @EXPORT = qw(getopt getopts); -$VERSION = '1.03'; +$VERSION = '1.04'; +# uncomment the next line to disable 1.03-backward compatibility paranoia +# $STANDARD_HELP_VERSION = 1; # Process single-character switches with switch clustering. Pass one argument # which is a string containing all switches that take an argument. For each @@ -112,25 +134,86 @@ sub getopt (;$$) { } } +sub output_h () { + return $OUTPUT_HELP_VERSION if defined $OUTPUT_HELP_VERSION; + return \*STDOUT if $STANDARD_HELP_VERSION; + return \*STDERR; +} + +sub try_exit () { + exit 0 if $STANDARD_HELP_VERSION; + my $p = __PACKAGE__; + print {output_h()} <= 5.006; + print $h <= 0) { if (defined($args[$pos+1]) and ($args[$pos+1] eq ':')) { shift(@ARGV); @@ -163,6 +246,18 @@ sub getopts ($;$) { } } else { + if ($first eq '-' and $rest eq 'help') { + version_mess($argumentative, 'main'); + help_mess($argumentative, 'main'); + try_exit(); + shift(@ARGV); + next; + } elsif ($first eq '-' and $rest eq 'version') { + version_mess($argumentative, 'main'); + try_exit(); + shift(@ARGV); + next; + } warn "Unknown option: $first\n"; ++$errs; if ($rest ne '') {