[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]
Re: [ID 20000125.005]
>: "Real" programs don't use getopts.
>In which case it doesn't matter if people play with it.
I forgot the smiley! It was a joke.
>Almost everyone has to play with getopts at some point in their
>existence. I admit that I was an exception.
I often find I don't use it because I can't, because the program
has to treat arguments "differently". Here's the PPT fold opt
parser, which needs to treat -72 and -w 72 as equivalent. I don't
know how to do that with getopts.
# do this by hand, because we don't like $opt_80, $opt_132, etc.
# and we want to check for dups. --tchrist
OPTION:
while (@ARGV && $ARGV[0] =~ /^-(.+)/ && (shift, ($_ = $1), 1)) {
next OPTION unless length;
if (s/^b//) {
warn "-b flag already set" if $Byte_Only++;
redo OPTION;
}
if (s/^s//) {
warn "-s flag already set" if $Space_Break++;
redo OPTION;
}
# historical practice makes -72 and -w 72 the same
if (s/^(\d.*)// || s/^w(.*)//) {
$Width = $1 || shift;
next OPTION;
}
usage("unexpected option: -$_");
}
And here's the ppt awk front end for an awk-compatible call to a2p.
Here, I have to deal with var=val issues.
while (@ARGV) {
$_ = $ARGV[0];
if (s/^-//) {
if (s/^F//) {
unless (length) { shift; $_ = $ARGV[0]; }
push(@nargs, "-F$_");
shift;
next;
}
elsif (s/^v// || /^\w+=/) {
unless (length) { shift; $_ = $ARGV[0]; }
push(@vargs, $_);
shift;
next;
}
elsif (s/^f//) {
unless (length) { shift; $_ = shift; }
push(@nargs, $_);
last;
}
elsif (s/^-//) {
if (length) { usage("Long options not supported") }
shift;
next;
}
else {
usage("unknown flag: -$_");
}
} else {
# XXX: is it a program or an expression?
if (/^\w+=/) {
push(@vargs, $_);
shift;
next;
}
else {
print TMPIN "$_\n";
shift;
push @nargs, $tmpin;
last;
}
}
}
unshift @ARGV, @vargs; # put back var=val statements
I sometimes even do something like this:
@opt{ qw/d f/ } = (0, 0);
while ($ARGV[0] =~ /^-/) {
$ARGV[0] =~ s/^-//;
for my $flag (split(//,$ARGV[0])) {
usage("unknown flag: `$flag'") unless 'df' =~ /\Q$flag/;
warn "$0: `$flag' flag already set\n" if $opt{$flag}++;
}
shift;
}
But then again, in tcgrep, I'd risk going crazy without
$optstring = "incCwsxvhe:f:l1EHurtpP:aqTF";
$zeros = 'inCwxvhelut'; # init opts to 0 (prevent warnings)
$nulls = 'pP'; # init opts to "" (prevent warnings)
@opt{ split //, $zeros } = ( 0 ) x length($zeros);
@opt{ split //, $nulls } = ( '' ) x length($nulls);
getopts($optstring, \%opt) or usage();
Hm... maybe I should FMTEYEWTK on arg processing. :-)
>I don't see much reason to reject the patch other than to be the mud
>around a stick.
I corresponded with the author, who demonstrated some issues. I
agree that the patch should be accepted. Certainly people who
have other needs don't need to use getopts, and those who do use
it expect it to work as it does in getopts(3).
--tom
[Date Prev][Date Next] [Thread Prev][Thread Next] [Date Index][Thread Index][Top&Search][Original]