From 000d96e52539fd6b8ecf6f6d0d399a1bedbbb9d9 Mon Sep 17 00:00:00 2001 From: Ian Fan Date: Tue, 23 Oct 2018 10:32:05 +0100 Subject: [PATCH] commands: clean-up checkarg function Consolidates logic and fixes mistake that used argc instead of val for determining plural. --- sway/commands.c | 33 ++++++++++++++------------------- 1 file changed, 14 insertions(+), 19 deletions(-) diff --git a/sway/commands.c b/sway/commands.c index 4c1b34d5..6a5eb3dd 100644 --- a/sway/commands.c +++ b/sway/commands.c @@ -18,33 +18,28 @@ // 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 *error = NULL; + const char *error_name = NULL; switch (type) { case EXPECTED_AT_LEAST: - if (argc >= val) { - return NULL; + if (argc < val) { + 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; case EXPECTED_LESS_THAN: - if (argc < val) { - return NULL; - }; - 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); + if (argc >= val) { + error_name = "less than "; + } break; case EXPECTED_EQUAL_TO: - if (argc == val) { - return NULL; - }; - error = cmd_results_new(CMD_INVALID, name, "Invalid %s command " - "(expected %d arguments, got %d)", name, val, argc); - break; + if (argc != val) { + error_name = ""; + } } - 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) {