| 1 | # ABSTRACT: Very quick and simple and unobtrusive option parser | ||
| 2 | package Opt::Imistic; | ||
| 3 | use strict; | ||
| 4 | use warnings; | ||
| 5 | |||
| 6 | our $VERSION = 0.04; | ||
| 7 | |||
| 8 | package Opt::Imistic::Option { | ||
| 9 | use overload | ||
| 10 | '""' => sub { $_[0]->[-1] }, | ||
| 11 | 'bool' => sub { 1 } | ||
| 12 | } | ||
| 13 | |||
| 14 | my $putback = 0; | ||
| 15 | my %hints; | ||
| 16 | |||
| 17 | sub import { | ||
| 18 | my $package = shift; | ||
| 19 | |||
| 20 | { | ||
| 21 | no warnings qw(uninitialized numeric); | ||
| 22 | if (@_ and int $_[0] eq $_[0]) { | ||
| 23 | $putback = shift; | ||
| 24 | } | ||
| 25 | } | ||
| 26 | |||
| 27 | %hints = @_; | ||
| 28 | |||
| 29 | if ($putback and @ARGV < $putback) { | ||
| 30 | _not_enough_args(); | ||
| 31 | } | ||
| 32 | |||
| 33 | # we alter @ARGV on purpose. | ||
| 34 | while (my $arg = shift @ARGV) { | ||
| 35 | last if $arg eq '--'; | ||
| 36 | |||
| 37 | if (substr($arg, 0, 2) eq '--') { | ||
| 38 | # Double dash (Mario Kart) - long opt! | ||
| 39 | substr $arg, 0, 2, ''; | ||
| 40 | |||
| 41 | my $val; | ||
| 42 | |||
| 43 | if (index($arg, '=') > -1) { | ||
| 44 | ($arg, $val) = split /=/, $arg; | ||
| 45 | $val = [ split /,/, $val ] if $val =~ /,/; | ||
| 46 | } | ||
| 47 | else { | ||
| 48 | $val = _can_has_value(); | ||
| 49 | } | ||
| 50 | |||
| 51 | if (exists $hints{needs_val}{$arg} || exists $hints{demand}{$arg}) { | ||
| 52 | my $die_message = "%s: value required but none given\n"; | ||
| 53 | $die_message .= "\n" . $hints{usage} if exists $hints{usage}; | ||
| 54 | |||
| 55 | die sprintf $die_message, $arg; | ||
| 56 | } | ||
| 57 | |||
| 58 | _store($arg, $val); | ||
| 59 | } | ||
| 60 | elsif (substr($arg, 0, 1) eq '-') { | ||
| 61 | # single-letter opts | ||
| 62 | substr $arg, 0, 1, ''; | ||
| 63 | my @opts = split //, $arg; | ||
| 64 | |||
| 65 | if (defined(my $val = _can_has_value())) { | ||
| 66 | _store(pop @opts, $val); | ||
| 67 | } | ||
| 68 | elsif (exists $hints{needs_val}{$arg} || exists $hints{demand}{$arg}) { | ||
| 69 | my $die_message = "%s: value required but none given\n"; | ||
| 70 | $die_message .= "\n" . $hints{usage} if exists $hints{usage}; | ||
| 71 | |||
| 72 | die sprintf $die_message, $arg; | ||
| 73 | } | ||
| 74 | |||
| 75 | _store($_) for @opts; | ||
| 76 | } | ||
| 77 | else { | ||
| 78 | # Put it back if options have ended. | ||
| 79 | unshift @ARGV, $arg; | ||
| 80 | last; | ||
| 81 | } | ||
| 82 | } | ||
| 83 | |||
| 84 | for my $o ( keys %ARGV ) { | ||
| 85 | # All args are arrayrefs now. This implements tm604's suggestion that we assume | ||
| 86 | # it's a countable option if it appears several times, but never with a value. | ||
| 87 | $ARGV{$o} = [ scalar @{ $ARGV{$o} } ] unless grep defined, @{ $ARGV{$o} }; | ||
| 88 | } | ||
| 89 | |||
| 90 | _store('-', $_) for @ARGV; | ||
| 91 | |||
| 92 | if (exists $hints{demand}) { | ||
| 93 | my $die_message = "Missing option: %s\n"; | ||
| 94 | $die_message .= "\n" . $hints{usage} if exists $hints{usage}; | ||
| 95 | |||
| 96 | for (keys %{ $hints{demand} }) { | ||
| 97 | die sprintf($die_message, $_) unless exists $ARGV{$_}; | ||
| 98 | } | ||
| 99 | } | ||
| 100 | } | ||
| 101 | |||
| 102 | # Stores repeated options in an array. | ||
| 103 | sub _store { | ||
| 104 | my ($arg, $val) = @_; | ||
| 105 | |||
| 106 | # tm604 suggested that, to accommodate an occurence such as: | ||
| 107 | # script --opt --opt --opt=foo --opt=123 | ||
| 108 | # we create opt => [ undef, undef, 'foo', '123' ]. | ||
| 109 | # Then we can collapse undef-only arrayrefs into counts later. So we don't | ||
| 110 | # care if the val is undef. yay! | ||
| 111 | |||
| 112 | $ARGV{$arg} //= bless [], "Opt::Imistic::Option"; | ||
| 113 | push @{ $ARGV{$arg} }, $val; | ||
| 114 | } | ||
| 115 | |||
| 116 | # Checks to see whether the next @ARGV is a value and returns it if so. | ||
| 117 | # shifts @ARGV so we skip it in the outer while loop. This is naughty but | ||
| 118 | # shut up :( | ||
| 119 | sub _can_has_value { | ||
| 120 | my $val = $ARGV[0]; | ||
| 121 | |||
| 122 | return if not defined $val; | ||
| 123 | |||
| 124 | if (index($val, '-') == 0 and $val ne '-') { | ||
| 125 | # starts with - but isn't - means option. (Includes --) | ||
| 126 | return; | ||
| 127 | } | ||
| 128 | |||
| 129 | if ($putback == @ARGV) { | ||
| 130 | # next thing is not an option; we keep this many. | ||
| 131 | return; | ||
| 132 | } | ||
| 133 | |||
| 134 | if ($putback > @ARGV) { | ||
| 135 | # next thing is not an option; not enough things left. | ||
| 136 | _not_enough_args(); | ||
| 137 | } | ||
| 138 | |||
| 139 | # Next thing is an option and there are *more than* $putback args - we shift. | ||
| 140 | return shift @ARGV; | ||
| 141 | } | ||
| 142 | |||
| 143 | # We couldn't satisfy $putback | ||
| 144 | sub _not_enough_args { | ||
| 145 | my $die = "Expected $putback arguments; " . @ARGV . " given\n"; | ||
| 146 | |||
| 147 | if ($hints{usage}) { | ||
| 148 | $die .= "\n" . $hints{usage}; | ||
| 149 | } | ||
| 150 | |||
| 151 | die $die; | ||
| 152 | } | ||
| 153 | |||
| 154 | 1; | ||
| 155 | |||
| 156 | __END__ |