chiark / gitweb /
support genopts
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 Sep 2013 22:29:24 +0000 (23:29 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 4 Sep 2013 22:29:24 +0000 (23:29 +0100)
service

diff --git a/service b/service
index f11894e6aee02ecc97cc1147149d4e3e036ebd97..9739cbce34edfdb563c6e2fc0a2899625a591803 100755 (executable)
--- a/service
+++ b/service
@@ -11,8 +11,8 @@ options
 END
 our $usage2 = <<'END';
 actions
-  create [REDIRECT] [#COMMENT]    (default for REDIRECT is your username)
-  choose [REDIRECT] [#COMMENT]    (interactively see and allocate suggestions)
+  create [GENOPTS] [REDIRECT] [#COMMENT]  (default for REDIRECT is username)
+  choose [GENOPTS] [REDIRECT] [#COMMENT]  (generate and interactively allocate)
   update ADDR [REDIRECT] [#COMMENT]
   show ADDR
   list
@@ -33,7 +33,10 @@ use strict;
 use DBI;
 use POSIX;
 
-our $randlength = 6;
+our $minrandlength = 6;
+our $randlength;
+our $maxrandlength = 50;
+
 our $maxperuser = 10000;
 our $qualdom;
 our $dbh;
@@ -192,7 +195,33 @@ sub prepare_create () {
     binmode R;
 }
 
+sub genopt_alphanum {
+    local ($_) = @_;
+    if (m/^-l(\d+)$/) {
+       $randlength = $1;
+       die "length out of range $minrandlength..$maxrandlength\n"
+           unless ($minrandlength<=$randlength &&
+                   $randlength<=$maxrandlength);
+    } else {
+       die "unknown alphanumeric generation option\n";
+    }
+}
+
+sub gendefaults_alphanum {
+    $randlength ||= $minrandlength;
+}
+
+sub genopts {
+    while (@ARGV && $ARGV[0] =~ m/^-/) {
+       my $arg = shift @ARGV;
+       last if $arg =~ m/^--?$/;
+       { no strict qw(refs); &{"genopt_$genmethod"}($arg); }
+    }
+    { no strict qw(refs); &{"gendefaults_$genmethod"}(); }
+}
+
 sub action_create {
+    genopts;
     my $newrow = rhsargs({'redirect'=>$user, 'comment'=>''});
     prepare_create();
     $newrow->{'user'} = $user;
@@ -203,6 +232,7 @@ sub action_create {
 }
 
 sub action_choose {
+    genopts;
     my $template = rhsargs({'redirect'=>$user, 'comment'=>''});
     $template->{'user'} = $user;
     prepare_create();
@@ -362,7 +392,7 @@ while (@ARGV) {
     for (;;) {
        last unless m/^-./;
        if (s/^-l(\d+)$//) {
-           $randlength = $1;
+           $minrandlength = $1;
        } elsif (s/^-m(\d+)$//) {
            $maxperuser = $1;
        } elsif (s/^-d(\S+)$//) {