| 1 | |||||||||||||||||||||||||||||
| 2 | 2 | require 5.003_96; # Time-stamp: "2004-12-29 19:27:33 AST" | |||||||||||||||||||||||||||
| 3 | # same minimal version required as constant.pm | ||||||||||||||||||||||||||||
| 4 | |||||||||||||||||||||||||||||
| 5 | package Getopt::constant; | ||||||||||||||||||||||||||||
| 6 | 2 | $VERSION = '1.03'; | |||||||||||||||||||||||||||
| 7 | 2 | ▶ | use strict; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 8 | |||||||||||||||||||||||||||||
| 9 | 1 | ▶ | sub import { | ||||||||||||||||||||||||||
|
Subroutine
import: called CC=32 SCAR=50.5
| |||||||||||||||||||||||||||||
| 10 | 1 | shift(@_); # We don't care about our own package name | |||||||||||||||||||||||||||
| 11 | |||||||||||||||||||||||||||||
| 12 | 1 | my(%h) = (@_); # Get default values from there. | |||||||||||||||||||||||||||
| 13 | 1 | my $pkg = caller; | |||||||||||||||||||||||||||
| 14 | 1 | ▶ | my $pref = delete( $h{':prefix'} ) || ''; | ||||||||||||||||||||||||||
|
Condition50%1/2
delete $h{':prefix'} || ''
MC/DC0%0/1
delete $h{':prefix'} || ''
delete $h{':prefix'}
Truth table50%1/2
A
A= delete $h{':prefix'}
| |||||||||||||||||||||||||||||
| 15 | 1 | my $usage = delete( $h{':usage' } ); # a usage message to throw at people | |||||||||||||||||||||||||||
| 16 | 1 | my $retain = delete( $h{':retain'} ); # if we should not change @ARGV | |||||||||||||||||||||||||||
| 17 | 1 | my $permissive = delete( $h{':permissive'} ); | |||||||||||||||||||||||||||
| 18 | # if we should just skip weird things | ||||||||||||||||||||||||||||
| 19 | 1 | my($x, @unknowns, @orig_ARGV); | |||||||||||||||||||||||||||
| 20 | 1 | ▶ | @orig_ARGV = @ARGV if $retain; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 21 | |||||||||||||||||||||||||||||
| 22 | 1 | ▶ | while(@ARGV) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 23 | 3 | $x = $ARGV[0]; | |||||||||||||||||||||||||||
| 24 | #print "Considering \"$x\"\n"; | ||||||||||||||||||||||||||||
| 25 | 3 | ▶ | if($x eq '-') { | ||||||||||||||||||||||||||
|
Branch: 5 branches
| |||||||||||||||||||||||||||||
| 26 | # Deceptively not part of the options list, so bail. | ||||||||||||||||||||||||||||
| 27 | 0 | last; | |||||||||||||||||||||||||||
| 28 | } elsif($x eq '--') { | ||||||||||||||||||||||||||||
| 29 | # End of options list. | ||||||||||||||||||||||||||||
| 30 | 1 | shift @ARGV; | |||||||||||||||||||||||||||
| 31 | 1 | last; | |||||||||||||||||||||||||||
| 32 | } elsif($x =~ m<--?([^=]+)=([^=]*)>s ) { # like -foo=123 or --foo=123 | ||||||||||||||||||||||||||||
| 33 | 1 | ▶ | if(!exists $h{$1}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 34 | 0 | ▶ | push @unknowns, $1 unless $permissive; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 35 | ▶ | } elsif(ref($h{$1} || '') eq 'ARRAY') { | |||||||||||||||||||||||||||
|
Condition50%1/2
$h{$1} || ''
MC/DC0%0/1
$h{$1} || ''
$h{$1}
Truth table50%1/2
A
A= $h{$1}
| |||||||||||||||||||||||||||||
| 36 | 1 | my($name) = $1; | |||||||||||||||||||||||||||
| 37 | 1 | @{$h{$name}} = split(",", $2, -1); | |||||||||||||||||||||||||||
| 38 | } else { | ||||||||||||||||||||||||||||
| 39 | 0 | $h{$1} = $2; | |||||||||||||||||||||||||||
| 40 | } | ||||||||||||||||||||||||||||
| 41 | 1 | shift @ARGV; | |||||||||||||||||||||||||||
| 42 | |||||||||||||||||||||||||||||
| 43 | } elsif($x =~ m<--?([^=]+)>s ) { # like -foo or --foo | ||||||||||||||||||||||||||||
| 44 | 1 | ▶ | if(!exists $h{$1}) { | ||||||||||||||||||||||||||
|
Branch: 2 branches
| |||||||||||||||||||||||||||||
| 45 | 0 | ▶ | push @unknowns, $1 unless $permissive; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 46 | ▶ | } elsif(ref($h{$1} || '') eq 'ARRAY') { | |||||||||||||||||||||||||||
|
Condition50%1/2
$h{$1} || ''
MC/DC0%0/1
$h{$1} || ''
$h{$1}
Truth table50%1/2
A
A= $h{$1}
| |||||||||||||||||||||||||||||
| 47 | 0 | @{$h{$1}} = (1); # guh, I guess that's right. | |||||||||||||||||||||||||||
| 48 | } else { | ||||||||||||||||||||||||||||
| 49 | 1 | $h{$1} = 1; | |||||||||||||||||||||||||||
| 50 | } | ||||||||||||||||||||||||||||
| 51 | 1 | shift @ARGV; | |||||||||||||||||||||||||||
| 52 | |||||||||||||||||||||||||||||
| 53 | ▶ | } elsif(length $x and substr($x,0,1) eq '-') { | |||||||||||||||||||||||||||
|
Condition0%0/3
length $x and substr($x, 0, 1) eq "-"
MC/DC0%0/2
length $x and substr($x, 0, 1) eq "-"
length $x
substr($x, 0, 1) eq "-"
Truth table0%0/3
A and B
A= length $xB= substr($x, 0, 1) eq "-"
| |||||||||||||||||||||||||||||
| 54 | 0 | ▶ | push @unknowns, "\"$x\"" unless $permissive; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 55 | |||||||||||||||||||||||||||||
| 56 | } else { | ||||||||||||||||||||||||||||
| 57 | 0 | last; # First non-option in @ARGV | |||||||||||||||||||||||||||
| 58 | } | ||||||||||||||||||||||||||||
| 59 | } | ||||||||||||||||||||||||||||
| 60 | |||||||||||||||||||||||||||||
| 61 | 1 | ▶ | @ARGV = @orig_ARGV if $retain; | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 62 | |||||||||||||||||||||||||||||
| 63 | 1 | ▶ | if(@unknowns) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 64 | 0 | ▶ | if(ref($usage || '') eq 'CODE') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition0%0/2
$usage || ''
MC/DC0%0/1
$usage || ''
$usage
Truth table0%0/2
A
A= $usage
| |||||||||||||||||||||||||||||
| 65 | 0 | $usage->(\@unknowns, [sort grep !m/^:/s, keys %h], \%h); | |||||||||||||||||||||||||||
| 66 | } else { | ||||||||||||||||||||||||||||
| 67 | 0 | ▶ | printf STDERR "Unknown option%s: %s\n", | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 68 | (@unknowns == 1) ? '' : 's', join(' ', @unknowns); | ||||||||||||||||||||||||||||
| 69 | 0 | ▶ | if(defined $usage) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 70 | 0 | print STDERR $usage | |||||||||||||||||||||||||||
| 71 | } else { | ||||||||||||||||||||||||||||
| 72 | 0 | my(@x) = sort grep !m/^:/s, keys %h; | |||||||||||||||||||||||||||
| 73 | 0 | ▶ | print STDERR | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 74 | (@x == 1) ? | ||||||||||||||||||||||||||||
| 75 | '(The only known option is: ' : '(The known options are: ', | ||||||||||||||||||||||||||||
| 76 | join(', ', @x), ")\n"; | ||||||||||||||||||||||||||||
| 77 | } | ||||||||||||||||||||||||||||
| 78 | } | ||||||||||||||||||||||||||||
| 79 | |||||||||||||||||||||||||||||
| 80 | 0 | ▶ | if($] <= 5.00599) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 81 | 0 | exit; | |||||||||||||||||||||||||||
| 82 | # Because if we put a 1 there, those versions of Perl will emit | ||||||||||||||||||||||||||||
| 83 | # a nasty "Callback called exit" message. Ah well. | ||||||||||||||||||||||||||||
| 84 | } else { | ||||||||||||||||||||||||||||
| 85 | 0 | exit 1; | |||||||||||||||||||||||||||
| 86 | } | ||||||||||||||||||||||||||||
| 87 | } | ||||||||||||||||||||||||||||
| 88 | |||||||||||||||||||||||||||||
| 89 | 1 | ▶ | while( my($name,$value) = each %h) { | ||||||||||||||||||||||||||
|
Branch: 1 branch
| |||||||||||||||||||||||||||||
| 90 | 3 | ▶ | next unless length($name) and substr($name,0,1) ne ':'; | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition33%1/3
length $name and substr($name, 0, 1) ne ":"
MC/DC0%0/2
length $name and substr($name, 0, 1) ne ":"
length $name
substr($name, 0, 1) ne ":"
Truth table33%1/3
A and B
A= length $nameB= substr($name, 0, 1) ne ":"
| |||||||||||||||||||||||||||||
| 91 | 2 | ▶ | no strict 'refs'; | ||||||||||||||||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 92 | 3 | ▶ | if(ref($value || '') eq 'ARRAY') { | ||||||||||||||||||||||||||
|
Branch: 1 branch
Condition50%1/2
$value || ''
MC/DC0%0/1
$value || ''
$value
Truth table50%1/2
A
A= $value
| |||||||||||||||||||||||||||||
| 93 | #print "Setting ${pkg}::$pref$name to the list (@$value)\n"; | ||||||||||||||||||||||||||||
| 94 | 1 | ▶ | *{"${pkg}::$pref$name"} = sub () { @$value }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: called CC=1 SCAR=0.0
| |||||||||||||||||||||||||||||
| 95 | } else { | ||||||||||||||||||||||||||||
| 96 | #print "Setting ${pkg}::$pref$name to $value\n"; | ||||||||||||||||||||||||||||
| 97 | 0 | ▶ | *{"${pkg}::$pref$name"} = sub () { $value }; | ||||||||||||||||||||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=0.4
| |||||||||||||||||||||||||||||
| 98 | } | ||||||||||||||||||||||||||||
| 99 | } | ||||||||||||||||||||||||||||
| 100 | |||||||||||||||||||||||||||||
| 101 | 1 | return; | |||||||||||||||||||||||||||
| 102 | } | ||||||||||||||||||||||||||||
| 103 | # Yup, that's it. | ||||||||||||||||||||||||||||
| 104 | ########################################################################### | ||||||||||||||||||||||||||||
| 105 | 2 | 1; | |||||||||||||||||||||||||||
| 106 | |||||||||||||||||||||||||||||
| 107 | __END__ | ||||||||||||||||||||||||||||