commands: clean-up checkarg function

Consolidates logic and fixes mistake that used argc instead of val for
determining plural.
master
Ian Fan 6 years ago
parent 5364255f26
commit 000d96e525

@ -18,33 +18,28 @@
// Returns error object, or NULL if check succeeds. // Returns error object, or NULL if check succeeds.
struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) { struct cmd_results *checkarg(int argc, const char *name, enum expected_args type, int val) {
struct cmd_results *error = NULL; const char *error_name = NULL;
switch (type) { switch (type) {
case EXPECTED_AT_LEAST: case EXPECTED_AT_LEAST:
if (argc >= val) { if (argc < val) {
return NULL; error_name = "at least ";
} }
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(expected at least %d argument%s, got %d)",
name, val, (char*[2]){"s", ""}[argc==1], argc);
break; break;
case EXPECTED_LESS_THAN: case EXPECTED_LESS_THAN:
if (argc < val) { if (argc >= val) {
return NULL; error_name = "less than ";
}; }
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(expected less than %d argument%s, got %d)",
name, val, (char*[2]){"s", ""}[argc==1], argc);
break; break;
case EXPECTED_EQUAL_TO: case EXPECTED_EQUAL_TO:
if (argc == val) { if (argc != val) {
return NULL; error_name = "";
}; }
error = cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(expected %d arguments, got %d)", name, val, argc);
break;
} }
return error; return error_name ?
cmd_results_new(CMD_INVALID, name, "Invalid %s command "
"(expected %s%d argument%s, got %d)",
name, error_name, val, val != 1 ? "s" : "", argc)
: NULL;
} }
void apply_seat_config(struct seat_config *seat_config) { void apply_seat_config(struct seat_config *seat_config) {

Loading…
Cancel
Save