3
0
mirror of https://github.com/pragma-/pbot.git synced 2025-07-21 12:57:28 +02:00

applets/cdecl.pl: show usage when command is invalid

This commit is contained in:
Pragmatic Software 2025-07-20 07:39:20 -07:00
parent 675a723d57
commit 27db494f85
No known key found for this signature in database
GPG Key ID: CC916B6E3C84ECCE
2 changed files with 48 additions and 15 deletions

61
applets/cdecl.pl vendored
View File

@ -1,27 +1,60 @@
#!/usr/bin/perl -w
#!/usr/bin/perl
# SPDX-FileCopyrightText: 2009-2023 Pragmatic Software <pragma78@gmail.com>
# SPDX-License-Identifier: MIT
# quick and dirty by :pragma
use warnings;
use strict;
use Getopt::Long qw/GetOptionsFromString/;
my $usage = "Usage: cdecl <explain|declare|cast|set|...> <code>, see http://linux.die.net/man/1/cdecl (Don't use this command. Use `english` instead.)\n";
my $command = join(' ', @ARGV);
my @args = split(' ', $command); # because @ARGV may be one quoted argument
if (@args < 2) {
print "Usage: cdecl <explain|declare|cast|set|...> <code>, see http://linux.die.net/man/1/cdecl (Don't use this command. Use `english` instead.)\n";
die;
}
$command = quotemeta($command);
$command =~ s/\\ / /g;
$command =~ s/^\s+|\s+$//g;
$command =~ s/\s+--\s+/ /g;
{
my $opt_err;
local $SIG{__WARN__} = sub {
$opt_err = shift;
chomp $opt_err;
};
Getopt::Long::Configure('no_auto_abbrev', 'no_ignore_case');
my %h;
my @allowed = qw/language=s x=s version v/;
my ($ret, $rest) = GetOptionsFromString($command, \%h, @allowed);
if ($opt_err) {
print "$opt_err\n";
exit 1;
}
if ($ret != 1) {
print "Error parsing options.\n";
exit 1;
}
if (not @$rest) {
print $usage;
exit 1;
}
my @commands = qw/cast declare expand define explain enum show/;
push @commands, '#define';
if (!grep { $rest->[0] eq $_ } @commands) {
print $usage;
exit 1;
}
}
my $result = `cdecl $command`;
chomp $result;
$result =~ s/\n/, /g;
print $result;
print " (Don't use this command. It can only handle C90 declarations -- poorly. Use `english` instead, which can translate any complete C11 code.)"
if $result =~ m/^declare/;
print "\n";
print "$result (Don't use this command. It can only handle C90 declarations -- poorly. Use `english` instead, which can translate any complete C11 code.)\n"

View File

@ -25,7 +25,7 @@ use PBot::Imports;
# These are set by the /misc/update_version script
use constant {
BUILD_NAME => "PBot",
BUILD_REVISION => 4877,
BUILD_REVISION => 4878,
BUILD_DATE => "2025-07-20",
};