Improve handling of -t for dict command

This commit is contained in:
Pragmatic Software 2015-10-15 16:07:51 -07:00
parent b23f025a12
commit 81abdbec29
1 changed files with 65 additions and 45 deletions

View File

@ -67,7 +67,7 @@ if (@ARGV > 0)
}
=cut
} else {
print "Usage: dict [-d database] [-n start from definition number] [-t first letter of word class type (n]oun, v]erb, etc)] [-search <regex> for definitions matching <regex>] <word>\n";
print "Usage: dict [-d database] [-n start from definition number] [-t abbreviation of word class type (n]oun, v]erb, adv]erb, adj]ective, etc)] [-search <regex> for definitions matching <regex>] <word>\n";
exit 0;
}
@ -115,6 +115,8 @@ sub define_word
$def_contains =~ s/\.\*/*/g;
$def_contains =~ s/\*/.*/g;
my $defined = 0;
eval {
foreach my $type (keys %$defs) {
next if $type eq 'word';
@ -126,6 +128,7 @@ sub define_word
print "$comma" unless $number == 1;
print "$number) $defs->{$type}{$number}";
$comma = ",\n\n";
$defined = 1;
}
}
};
@ -134,6 +137,23 @@ sub define_word
print "Error in -t parameter. Use v, n, *, etc.\n";
exit 0;
}
if (not $defined && $def_type ne '*') {
my $types = '';
$comma = '';
foreach my $type (sort keys %$defs) {
next if $type eq 'word';
$types .= "$comma$type";
$comma = ', ';
}
if (length $types) {
print "no `$def_type` definition found; available definitions: $types.\n";
} else {
print "no definition found.\n";
}
} elsif (not $defined) {
print "no definition found.\n";
}
}
}
}
@ -155,7 +175,7 @@ sub dict_hash {
$line =~ s/\s+$//;
$line =~ s/\s+/ /g;
if($line =~ m/^([a-z]) (\d+): (.*)/i) {
if($line =~ m/^([a-z]+) (\d+): (.*)/i) {
($type, $number, $text) = ($1, $2, $3);
}
elsif($line =~ m/^(\d+): (.*)/i) {
@ -354,7 +374,7 @@ sub initialise
=cut
if(not $config->args(\@ARGV)) {
print "Usage : dict [-d database] [-n start from definition number] [-t first letter of word class type (n]oun, v]erb, etc)] [-search <regex> for definitions matching <regex>] <word>\n";
print "Usage : dict [-d database] [-n start from definition number] [-t abbreviation of word class type (n]oun, v]erb, adv]erb, adj]ective, etc)] [-search <regex> for definitions matching <regex>] <word>\n";
exit;
}