From 21bf04fe3a7444ff6e3c4736bf3cf9d6f45dcfec Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 4 Sep 2013 23:29:24 +0100 Subject: [PATCH] support genopts --- service | 38 ++++++++++++++++++++++++++++++++++---- 1 file changed, 34 insertions(+), 4 deletions(-) diff --git a/service b/service index f11894e..9739cbc 100755 --- 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+)$//) { -- 2.30.2