lib/Odin.pm, mason/common/autohandler: Track time of the current job.
[odin-cgi] / lib / Odin.pm
index 52c7dde..a9bff5a 100644 (file)
@@ -50,6 +50,10 @@ our $PASTEBIN = "$BASEURL$PASTEBIN_PATH";
 ###--------------------------------------------------------------------------
 ### Miscellaneous utilities.
 
+our $NOW;
+sub update_now () { $NOW = time; }
+update_now;
+
 (our $PROG = $0) =~ s:^.*/::;
 
 sub fail_cmdline ($$%) {
@@ -431,6 +435,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;