- char *msg = xasprintf("\"%s\" is not a valid command", name);
- unixctl_command_reply(conn, 400, msg);
- free(msg);
+ const char *name = argv.names[0];
+ char *error;
+
+ command = shash_find_data(&commands, name);
+ if (!command) {
+ error = xasprintf("\"%s\" is not a valid command", name);
+ } else if (argv.n - 1 < command->min_args) {
+ error = xasprintf("\"%s\" command requires at least %d arguments",
+ name, command->min_args);
+ } else if (argv.n - 1 > command->max_args) {
+ error = xasprintf("\"%s\" command takes at most %d arguments",
+ name, command->max_args);
+ } else {
+ error = NULL;
+ command->cb(conn, argv.n, (const char **) argv.names,
+ command->aux);
+ }
+
+ if (error) {
+ unixctl_command_reply(conn, 400, error);
+ free(error);
+ }