From: Ian Jackson Date: Fri, 14 Aug 2015 12:31:04 +0000 (+0100) Subject: Introduce defvalopt, new approach for options taking values X-Git-Tag: debian/1.2~17 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?p=dgit.git;a=commitdiff_plain;h=3b2ac8fff57ddcca1e38472dafc55d93c430038a Introduce defvalopt, new approach for options taking values --- diff --git a/debian/changelog b/debian/changelog index c577e024..59bcf793 100644 --- a/debian/changelog +++ b/debian/changelog @@ -7,6 +7,7 @@ dgit (1.2~~) UNRELEASED; urgency=low * Correct manpage cross-reference to point to browse.d.d.o. * Honour *.clean-mode configuration setting for --clean= mode. * In manpage move dgit.default.* to main CONFIGURATION section. + * WIP improvements to option handling of options with values -- diff --git a/dgit b/dgit index 83969717..b3ddb548 100755 --- a/dgit +++ b/dgit @@ -3108,6 +3108,23 @@ sub cmd_version { exit 0; } +our (%valopts_long, %valopts_short); +our @rvalopts; + +sub defvalopt ($$$&) { + my ($long,$short,$val_re,$fn) = @_; + my $oi = { Long => $long, Short => $short, Re => $val_re, Fn => $fn }; + $valopts_long{$long} = $oi; + $valopts_short{$short} = $oi; + # $fn subref should: + # do whatever assignemnt or thing it likes with $_[0] + # if the option should not be passed on to remote, @rvalopts=() +} + +defvalopt '--since-version', '-v', '[^_]+|_', sub { + ($changes_since_version) = @_; +}; + sub parseopts () { my $om; @@ -3117,6 +3134,8 @@ sub parseopts () { @ssh = ($ENV{'GIT_SSH'}); } + my $oi; + while (@ARGV) { last unless $ARGV[0] =~ m/^-/; $_ = shift @ARGV; @@ -3138,9 +3157,6 @@ sub parseopts () { } elsif (m/^--new$/) { push @ropts, $_; $new_package=1; - } elsif (m/^--since-version=([^_]+|_)$/) { - push @ropts, $_; - $changes_since_version = $1; } elsif (m/^--([-0-9a-z]+)=(.+)/s && ($om = $opts_opt_map{$1}) && length $om->[0]) { @@ -3187,6 +3203,18 @@ sub parseopts () { } elsif (m/^--deliberately-($deliberately_re)$/s) { 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; } else { badusage "unknown long option \`$_'"; } @@ -3207,9 +3235,6 @@ sub parseopts () { } elsif (s/^-N/-/) { push @ropts, $&; $new_package=1; - } elsif (s/^-v([^_]+|_)$//s) { - push @ropts, $&; - $changes_since_version = $1; } elsif (m/^-m/) { push @ropts, $&; push @changesopts, $_; @@ -3228,7 +3253,7 @@ sub parseopts () { } } elsif (s/^-k(.+)//s) { $keyid=$1; - } elsif (m/^-[vdCk]$/) { + } elsif (m/^-[dCk]$/) { badusage "option \`$_' requires an argument (and no space before the argument)"; } elsif (s/^-wn$//s) { @@ -3249,6 +3274,19 @@ sub parseopts () { } elsif (s/^-wc$//s) { 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; + $_ = ''; } else { badusage "unknown short option \`$_'"; }