summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorcos <cos>2015-08-04 19:56:25 +0200
committercos <cos>2015-08-04 19:56:25 +0200
commit8da4fc1b825748208d5de50a9250ddd4314c139d (patch)
tree40e3d081d139f9abef5693404b4fac09a552046d
parent8e9c0e38bab593f3354efe8eaf62948d6adbcd11 (diff)
downloadmat-8da4fc1b825748208d5de50a9250ddd4314c139d.zip
Improve readability of main.
-rwxr-xr-xmat77
1 files changed, 35 insertions, 42 deletions
diff --git a/mat b/mat
index 70e7bf5..b0a309d 100755
--- a/mat
+++ b/mat
@@ -946,49 +946,42 @@ sub cmd_shoppinglist {
read_recipe_db;
+my $commands = {
+ "help" => { func => \&cmd_help, min => 0, max => 0 },
+ "addrecipe" => { func => \&cmd_addrecipe, min => 0, max => 0 },
+ "searchrecipes" => { func => \&cmd_searchrecipes, min => 1, max => 1 },
+ "editrecipe" => { func => \&cmd_editrecipe, min => 1, max => 1 },
+ "showrecipe" => { func => \&cmd_showrecipe, min => 1, max => 2 },
+ "movemeal" => { func => \&cmd_movemeal, min => 2, max => 2 },
+ "setmeal" => { func => \&cmd_setmeal, min => 3, max => 3 },
+ "setcomment" => { func => \&cmd_setcomment, min => 3, max => 3 },
+ "randmeal" => { func => \&cmd_randmeal, min => 1, max => 1 },
+ "showplan" => { func => \&cmd_showplan, min => 0, max => 2 },
+ "postpone" => { func => \&cmd_postpone, min => 2, max => 2 },
+ "inventory" => { func => \&cmd_inventory, min => 1, max => 1 },
+ "storeportion" => { func => \&cmd_storeportion, min => 1, max => 3 },
+ "reprintlabel" => { func => \&cmd_reprintlabel, min => 1, max => 1 },
+ "relocate" => { func => \&cmd_relocate, min => 2, max => 2 },
+ "queueadd" => { func => \&cmd_queueadd, min => 2, max => 2 },
+ "queuerm" => { func => \&cmd_queuerm, min => 1, max => 1 },
+ "queuemv" => { func => \&cmd_queuemv, min => 2, max => 2 },
+ "queueshow" => { func => \&cmd_queueshow, min => 0, max => 0 },
+ "shoppinglist" => { func => \&cmd_shoppinglist, min => 0, max => 1 }
+};
+
if ($ARGV[0]) {
- if ( $ARGV[0] eq "help") {
- print "Command failed!\n" unless (cmd_help >= 0);
- } elsif ( $ARGV[0] eq "addrecipe") {
- print "Command failed!\n" unless (cmd_addrecipe() >= 0);
- } elsif ( $ARGV[0] eq "searchrecipes") {
- print "Command failed!\n" unless (cmd_searchrecipes($ARGV[1]) >= 0);
- } elsif ( $ARGV[0] eq "editrecipe") {
- print "Command failed!\n" unless (cmd_editrecipe($ARGV[1]) >= 0);
- } elsif ( $ARGV[0] eq "showrecipe") {
- print "Command failed!\n" unless (cmd_showrecipe($ARGV[1], $ARGV[2]) >= 0);
- } elsif ( $ARGV[0] eq "movemeal") {
- print "Command failed!\n" unless (cmd_movemeal($ARGV[1], $ARGV[2]) >= 0);
- } elsif ( $ARGV[0] eq "setmeal") {
- print "Command failed!\n" unless (cmd_setmeal($ARGV[1], $ARGV[2], $ARGV[3]) >= 0);
- } elsif ( $ARGV[0] eq "setcomment") {
- print "Command failed!\n" unless (cmd_setcomment($ARGV[1], $ARGV[2], $ARGV[3]) >= 0);
- } elsif ( $ARGV[0] eq "randmeal") {
- print "Command failed!\n" unless (cmd_randmeal($ARGV[1]) >= 0);
- } elsif ( $ARGV[0] eq "showplan") {
- print "Command failed!\n" unless (cmd_showplan($ARGV[1], $ARGV[2]) >= 0);
- } elsif ( $ARGV[0] eq "postpone") {
- print "Command failed!\n" unless (cmd_postpone($ARGV[1]) >= 0);
- } elsif ( $ARGV[0] eq "inventory") {
- print "Command failed!\n" unless (cmd_inventory($ARGV[1]) >= 0);
- } elsif ( $ARGV[0] eq "storeportion") {
- print "Command failed!\n" unless (cmd_storeportion($ARGV[1], $ARGV[2], $ARGV[3]) >= 0);
- } elsif ( $ARGV[0] eq "labb") {
- print "Command failed!\n" unless (cmd_labb($ARGV[1], $ARGV[2], $ARGV[3]) >= 0);
- } elsif ( $ARGV[0] eq "reprintlabel") {
- print "Command failed!\n" unless (cmd_reprintlabel($ARGV[1]) >= 0);
- } elsif ( $ARGV[0] eq "relocate") {
- print "Command failed!\n" unless (cmd_relocate($ARGV[1], $ARGV[2]) >= 0);
- } elsif ( $ARGV[0] eq "queueadd") {
- print "Command failed!\n" unless (cmd_queueadd($ARGV[1], $ARGV[2]) >= 0);
- } elsif ( $ARGV[0] eq "queuerm") {
- print "Command failed!\n" unless (cmd_queuerm($ARGV[1], $ARGV[2]) >= 0);
- } elsif ( $ARGV[0] eq "queuemv") {
- print "Command failed!\n" unless (cmd_queuemv($ARGV[1], $ARGV[2]) >= 0);
- } elsif ( $ARGV[0] eq "queueshow") {
- print "Command failed!\n" unless (cmd_queueshow());
- } elsif ( $ARGV[0] eq "shoppinglist") {
- print "Command failed!\n" unless (cmd_shoppinglist($ARGV[1]) >= 0);
+ my $command = $commands->{$ARGV[0]};
+ my $argcount = $#ARGV;
+ if (defined($command)) {
+ if ($argcount < $command->{'min'}) {
+ print "Too few arguments for command $ARGV[0].\n";
+ exit 1;
+ }
+ if ($argcount > $command->{'max'}) {
+ print "Too many arguments for command $ARGV[0].\n";
+ exit 1;
+ }
+ print "Command failed!\n" unless($command->{'func'}(splice @ARGV, 1) >= 0);
} else {
print "Unknown command $ARGV[0]\n";
exit 1;