X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~mdw/git/odin-cgi/blobdiff_plain/97a33b9cd6fbde6d8eac854354c6b1326fad2f8e..f0bcb39a65ff55a0cbe3fced66903c730679150a:/lib/Odin.pm diff --git a/lib/Odin.pm b/lib/Odin.pm index 52c7dde..28118c8 100644 --- a/lib/Odin.pm +++ b/lib/Odin.pm @@ -431,6 +431,50 @@ sub tidy_pastebin_content ($) { return $content; } +###-------------------------------------------------------------------------- +### Simple option parser. + +package Odin::OptParse; + +sub new { + my ($cls, @args) = @_; + return bless { + cur => "", + args => \@args, + opt => undef, + ok => 1 + }, $cls; +} + +sub get { + my ($me) = @_; + if (!length $me->{cur}) { + my $args = $me->{args}; + if (!@$args) { return undef; } + elsif ($args->[0] =~ /^[^-]|^-$/) { return undef; } + elsif ($args->[0] eq "--") { shift @$args; return undef; } + $me->{cur} = substr shift @$args, 1; + } + my $o = $me->{opt} = substr $me->{cur}, 0, 1; + $me->{cur} = substr $me->{cur}, 1; + return $o; +} + +sub arg { + my ($me) = @_; + my $a; + if (length $me->{cur}) { $a = $me->{cur}; $me->{cur} = ""; } + elsif (@{$me->{args}}) { $a = shift @{$me->{args}}; } + else { $a = undef; $me->err("option `-$me->{opt}' requires an argument"); } + return $a; +} + +sub rest { return @{$_[0]->{args}}; } +sub ok { return $_[0]->{ok}; } +sub bad { $_[0]->{ok} = 0; } +sub err { $_[0]->bad; print STDERR "$PROG: $_[1]\n"; } +sub unk { $_[0]->err("unknown option `-$_[0]->{opt}'"); } + ###----- That's all, folks -------------------------------------------------- 1;