chiark / gitweb /
Break out $valopt (nfc)
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 14 Aug 2015 13:04:37 +0000 (14:04 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 14 Aug 2015 17:34:58 +0000 (18:34 +0100)
dgit

diff --git a/dgit b/dgit
index b3ddb54..f068df6 100755 (executable)
--- a/dgit
+++ b/dgit
@@ -3135,6 +3135,20 @@ sub parseopts () {
     }
 
     my $oi;
+    my $val;
+    my $valopt = sub {
+       my ($what) = @_;
+       @rvalopts = ($_);
+       if (!defined $val) {
+           badusage "$what needs a value" unless length @ARGV;
+           $val = shift @ARGV;
+           push @rvalopts, $val;
+       }
+       badusage "bad value \`$val' for $what" unless
+           $val =~ m/^$oi->{Re}$(?!\n)/s;
+       $oi->{Fn}($val);
+       push @ropts, @rvalopts;
+    };
 
     while (@ARGV) {
        last unless $ARGV[0] =~ m/^-/;
@@ -3204,17 +3218,8 @@ sub parseopts () {
                push @ropts, $_;
                push @deliberatelies, $&;
            } elsif (m/^(--[-0-9a-z]+)(=|$)/ && ($oi = $valopts_long{$1})) {
-               @rvalopts = ($_);
-               my $val = $'; #';
-               if ($2 eq '') {
-                   badusage "$oi->{Long} needs a value" unless @ARGV;
-                   $val = shift @ARGV;
-                   push @rvalopts, $val;
-               }
-               badusage "bad value for $oi->{Long}" unless
-                   $val =~ m/^$oi->{Re}$(?!\n)/s;
-               $oi->{Fn}($val);
-               push @ropts, @rvalopts;
+               $val = $2 ? $' : undef; #';
+               $valopt->($oi->{Long});
            } else {
                badusage "unknown long option \`$_'";
            }
@@ -3275,17 +3280,9 @@ sub parseopts () {
                    push @ropts, $&;
                    $cleanmode = 'check';
                } elsif (m/^-[a-zA-Z]/ && ($oi = $valopts_short{$&})) {
-                   @rvalopts = ($_);
-                   my $val = $'; #';
-                   if (!length $val) {
-                       badusage "$oi->{Short} needs a value" unless @ARGV;
-                       $val = shift @ARGV;
-                       push @rvalopts, $val;
-                   }
-                   badusage "bad value for $oi->{Short}" unless
-                       $val =~ m/^$oi->{Re}$(?!\n)/s;
-                   $oi->{Fn}($val);
-                   push @ropts, @rvalopts;
+                   $val = $'; #';
+                   $val = undef unless length $val;
+                   $valopt->($oi->{Short});
                    $_ = '';
                } else {
                    badusage "unknown short option \`$_'";