diff options
author | Ian Fan <ianfan0@gmail.com> | 2018-10-23 10:32:05 +0100 |
---|---|---|
committer | Ian Fan <ianfan0@gmail.com> | 2018-10-23 10:51:08 +0100 |
commit | 000d96e52539fd6b8ecf6f6d0d399a1bedbbb9d9 (patch) | |
tree | 57529ef7a6f82b377069c469bd66d40b5bb8b19f | |
parent | 5364255f265da2e324f65369037d5828688d08cd (diff) |
commands: clean-up checkarg function
Consolidates logic and fixes mistake that used argc instead of val for
determining plural.
-rw-r--r-- | sway/commands.c | 33 |
1 files 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) { |