| 1 | #!/usr/bin/env perl | ||||||||||||||
| 2 | |||||||||||||||
| 3 | =head1 NAME | ||||||||||||||
| 4 | |||||||||||||||
| 5 | Getopt::WonderBra - Lift and Separate Command Line Options | ||||||||||||||
| 6 | |||||||||||||||
| 7 | =head1 SYNOPSIS | ||||||||||||||
| 8 | |||||||||||||||
| 9 | use Getopt::WonderBra; | ||||||||||||||
| 10 | @ARGV = getopt( 'opts:-:', @ARGV ); | ||||||||||||||
| 11 | |||||||||||||||
| 12 | sub help() { print "Useless help message"; }; | ||||||||||||||
| 13 | sub version() { print "Useless version message"; }; | ||||||||||||||
| 14 | while ( ( $_ = shift ) ne '--' ) { | ||||||||||||||
| 15 | if (/^-o$/) { $opt_o++ } | ||||||||||||||
| 16 | elsif (/^-p$/) { $opt_p++ } | ||||||||||||||
| 17 | elsif (/^-t$/) { $opt_t++ } | ||||||||||||||
| 18 | elsif (/^-s$/) { push( @opt_s, shift ); } | ||||||||||||||
| 19 | elsif (/^--/) { push( @opt_long, $_ ); } | ||||||||||||||
| 20 | else { die 'I do not grok -', $_; } | ||||||||||||||
| 21 | } | ||||||||||||||
| 22 | print "-o given $opt_o times" if $opt_o; | ||||||||||||||
| 23 | print "-p given $opt_p times" if $opt_p; | ||||||||||||||
| 24 | print "-t given $opt_t times" if $opt_t; | ||||||||||||||
| 25 | print "-s given with arg $_" for @opt_s; | ||||||||||||||
| 26 | print "long opt $_ given" for @opt_long; | ||||||||||||||
| 27 | print ""; | ||||||||||||||
| 28 | print " param: $_" for @ARGV; | ||||||||||||||
| 29 | |||||||||||||||
| 30 | =head1 REQUIRES | ||||||||||||||
| 31 | |||||||||||||||
| 32 | perl5.008006, Carp, Exporter | ||||||||||||||
| 33 | |||||||||||||||
| 34 | =head1 EXPORTS | ||||||||||||||
| 35 | |||||||||||||||
| 36 | getopt($@) | ||||||||||||||
| 37 | |||||||||||||||
| 38 | =head1 DESCRIPTION | ||||||||||||||
| 39 | |||||||||||||||
| 40 | See eg/WonderBra.pl for an example of usage. | ||||||||||||||
| 41 | |||||||||||||||
| 42 | There just weren't enough command line processessing modules, so I had | ||||||||||||||
| 43 | to write my own. Actually, it exists because it made it easy to port | ||||||||||||||
| 44 | shell scripts to perl: it acts just like the getopt program. Oddly, | ||||||||||||||
| 45 | none of the modules that are actually named after it do. (Though some | ||||||||||||||
| 46 | act like the C function) The following sequence chops your args up and | ||||||||||||||
| 47 | gives 'em to you straight: | ||||||||||||||
| 48 | |||||||||||||||
| 49 | =head1 HELP | ||||||||||||||
| 50 | |||||||||||||||
| 51 | main::help() must exist prior to calling getopt(). It is wrapped by | ||||||||||||||
| 52 | this module. This is done to ensure correct behavior for programs that | ||||||||||||||
| 53 | use getopt. (e.g. error messages to stdout if --help in specified, | ||||||||||||||
| 54 | so $ foo --help | less has the desired results) | ||||||||||||||
| 55 | |||||||||||||||
| 56 | main::help() is replaced by a wrapper that will exit the program. | ||||||||||||||
| 57 | If it gets args, it will select STDERR, call your help function, print | ||||||||||||||
| 58 | the passed args, and exit non-zero. | ||||||||||||||
| 59 | |||||||||||||||
| 60 | Otherwise, it will select STDOUT, call your help function, and exit non-zero. | ||||||||||||||
| 61 | |||||||||||||||
| 62 | Note that the program will exit if you call help after calling getopt, as | ||||||||||||||
| 63 | well. This is not a bug. It's for issuing error messages while handling | ||||||||||||||
| 64 | the parsed args. | ||||||||||||||
| 65 | |||||||||||||||
| 66 | The wrapper sub never returns. | ||||||||||||||
| 67 | |||||||||||||||
| 68 | =head1 VERSION | ||||||||||||||
| 69 | |||||||||||||||
| 70 | If you define a main::version() sub, it will be called if the | ||||||||||||||
| 71 | user specified --version, and the program will terminate. | ||||||||||||||
| 72 | |||||||||||||||
| 73 | STDOUT will always be selected. | ||||||||||||||
| 74 | |||||||||||||||
| 75 | =cut | ||||||||||||||
| 76 | |||||||||||||||
| 77 | package Getopt::WonderBra; | ||||||||||||||
| 78 | 10 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 79 | 10 | our($VERSION)="1.04"; | |||||||||||||
| 80 | |||||||||||||||
| 81 | |||||||||||||||
| 82 | 10 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 83 | 10 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 84 | 10 | ▶ | use Carp qw(confess); | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 85 | sub import { | ||||||||||||||
| 86 | 10 | ▶ | *{main::getopt}=\&getopt; | ||||||||||||
|
Subroutine
import: called CC=1 SCAR=0.0
| |||||||||||||||
| 87 | }; | ||||||||||||||
| 88 | 10 | our (%switches, @arg, @noarg, $res); | |||||||||||||
| 89 | 10 | my $mainhelp; | |||||||||||||
| 90 | my $mainver; | ||||||||||||||
| 91 | sub version { | ||||||||||||||
| 92 | 0 | ▶ | select STDERR if ( @_ ); | ||||||||||||
|
Subroutine
version: not called CC=3 SCAR=24.8
Pod
undocumented
Branch: 1 branch
| |||||||||||||||
| 93 | 0 | $mainver->(); | |||||||||||||
| 94 | 0 | ▶ | if ( @_ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 95 | 0 | print "\n ERROR: @_\n"; | |||||||||||||
| 96 | }; | ||||||||||||||
| 97 | 0 | exit @_ != 0; | |||||||||||||
| 98 | }; | ||||||||||||||
| 99 | sub help { | ||||||||||||||
| 100 | 1 | ▶ | select STDERR if ( @_ ); | ||||||||||||
|
Subroutine
help: called CC=3 SCAR=11.3
Pod
undocumented
Branch: 1 branch
| |||||||||||||||
| 101 | 1 | $mainhelp->(@_); | |||||||||||||
| 102 | 1 | ▶ | if ( @_ ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 103 | 1 | print "\n ERROR: @_\n"; | |||||||||||||
| 104 | }; | ||||||||||||||
| 105 | 1 | exit @_ != 0; | |||||||||||||
| 106 | }; | ||||||||||||||
| 107 | sub rep_funcs { | ||||||||||||||
| 108 | 8 | ▶ | die "missing main::help" unless exists &main::help; | ||||||||||||
|
Subroutine
rep_funcs: called CC=5 SCAR=16.4
Pod
undocumented
Branch: 1 branch
| |||||||||||||||
| 109 | 8 | ▶ | die "missing main::version" unless exists &main::version; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 110 | 8 | ▶ | unless (defined($mainhelp)){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 111 | 8 | $mainhelp = \&main::help; | |||||||||||||
| 112 | 10 | ▶ | no warnings 'redefine'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 113 | 8 | *main::help=\&Getopt::WonderBra::help; | |||||||||||||
| 114 | }; | ||||||||||||||
| 115 | 8 | ▶ | unless (defined($mainver)){ | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 116 | 8 | $mainver = \&main::version; | |||||||||||||
| 117 | 10 | ▶ | no warnings 'redefine'; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 118 | 8 | *main::version=\&Getopt::WonderBra::version; | |||||||||||||
| 119 | }; | ||||||||||||||
| 120 | }; | ||||||||||||||
| 121 | sub parsefmt($){ | ||||||||||||||
| 122 | 8 | ▶ | local $_ = shift; | ||||||||||||
|
Subroutine
parsefmt: called CC=8 SCAR=22.8
Pod
undocumented
| |||||||||||||||
| 123 | 8 | ▶ | while(length) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 124 | 7 | my ($switch,$colons); | |||||||||||||
| 125 | 7 | ($switch,$colons,$_) = m/^(.)(:?:?)(.*)/; | |||||||||||||
| 126 | 7 | ▶ | confess "no optional args" if ( $colons eq '::' ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 127 | 7 | ▶ | confess ": is not a legal switch" if ( $switch eq ':' ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 128 | 7 | ▶ | confess "$switch repeated" if ( $switches{$switch} ); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 129 | 7 | ▶ | if ( $colons ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 130 | 0 | push(@arg, $switch); | |||||||||||||
| 131 | 0 | $switches{$switch}='arg'; | |||||||||||||
| 132 | } else { | ||||||||||||||
| 133 | 7 | push(@noarg, $switch); | |||||||||||||
| 134 | 7 | $switches{$switch}='noarg'; | |||||||||||||
| 135 | }; | ||||||||||||||
| 136 | } | ||||||||||||||
| 137 | 8 | ▶ | $switches{'-'} = 'arg' if defined $switches{'-'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 138 | 8 | ▶ | if ( defined($ENV{GETOPT_WONDERBRA_DUMP_FMT}) ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 139 | 0 | eval 'use Data::Dumper;'; | |||||||||||||
| 140 | 0 | print STDERR Dumper(\%switches); | |||||||||||||
| 141 | }; | ||||||||||||||
| 142 | } | ||||||||||||||
| 143 | |||||||||||||||
| 144 | sub singleopt($\@){ | ||||||||||||||
| 145 | 2 | ▶ | my $text = 'single: "'.join('","',@{$_[$#_]}).'"'; | ||||||||||||
|
Subroutine
singleopt: called CC=7 SCAR=25.7
Pod
undocumented
| |||||||||||||||
| 146 | 2 | local $_ = shift; | |||||||||||||
| 147 | 2 | my $arg = shift; | |||||||||||||
| 148 | 2 | my ($s, @res,$t); | |||||||||||||
| 149 | 2 | ▶ | while(length) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 150 | 2 | ( $s, $_ ) = m/^(.)(.*)/; | |||||||||||||
| 151 | 2 | ▶ | if ( !exists $switches{$s} ) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 152 | 0 | help("illegal switch: $s (part of $s$_)"); | |||||||||||||
| 153 | } | ||||||||||||||
| 154 | 2 | my $type = $switches{$s}; | |||||||||||||
| 155 | 2 | push(@res,"-$s"); | |||||||||||||
| 156 | 2 | ▶ | if ( $type eq 'noarg' ) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 157 | 2 | next; | |||||||||||||
| 158 | } elsif ( $type eq 'arg' ) { | ||||||||||||||
| 159 | 0 | ▶ | if ( length ) { push(@res, $_);last; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 160 | 0 | ▶ | if ( @$arg ) { push(@res, shift @$arg);last; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 161 | 0 | help("switch $s missing required arg"); | |||||||||||||
| 162 | } else { | ||||||||||||||
| 163 | 0 | confess "Internal Error: $type"; | |||||||||||||
| 164 | }; | ||||||||||||||
| 165 | } | ||||||||||||||
| 166 | 2 | return ( @res ); | |||||||||||||
| 167 | }; | ||||||||||||||
| 168 | sub doubleopt($\@){ | ||||||||||||||
| 169 | 2 | ▶ | return help() if $_[0] eq 'help'; | ||||||||||||
|
Subroutine
doubleopt: called CC=4 SCAR=14.2
Pod
undocumented
Branch: 1 branch
| |||||||||||||||
| 170 | 2 | ▶ | return version() if $_[0] eq 'version'; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 171 | help("not accepting long opts, but got --$_[0]") | ||||||||||||||
| 172 | 2 | ▶ | unless defined $switches{'-'}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 173 | 1 | return "--".$_[0]; | |||||||||||||
| 174 | } | ||||||||||||||
| 175 | |||||||||||||||
| 176 | sub getopt($\@) { | ||||||||||||||
| 177 | 8 | ▶ | rep_funcs; | ||||||||||||
|
Subroutine
getopt: called CC=8 SCAR=21.0
Pod
undocumented
| |||||||||||||||
| 178 | 8 | my ($opts,$args) = @_; | |||||||||||||
| 179 | 8 | ▶ | confess "Internal Error: Missing switch specifiers" unless @_; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 180 | 8 | parsefmt($opts); | |||||||||||||
| 181 | 8 | local *_ = $args; | |||||||||||||
| 182 | 8 | my @nonopts; | |||||||||||||
| 183 | my @opts; | ||||||||||||||
| 184 | 8 | ▶ | while(@_) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 185 | 7 | ▶ | confess "undef amongst the args?" unless defined($_ = shift); | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 186 | 7 | ▶ | if ( !s/^-// ) { push(@nonopts,$_); next; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 187 | 6 | ▶ | if ( !length ) { push(@nonopts,'-'); next; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 188 | 6 | ▶ | if ( !s/^-// ) { push(@opts,singleopt $_, @_);next; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 189 | 4 | ▶ | if ( length ) { push(@opts,doubleopt $_, @_);next; } | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 190 | 2 | last; | |||||||||||||
| 191 | }; | ||||||||||||||
| 192 | 7 | return @opts, '--', @nonopts, @_; | |||||||||||||
| 193 | } | ||||||||||||||
| 194 | 10 | 1; | |||||||||||||
| 195 | |||||||||||||||