diff options
author | David Härdeman <david@hardeman.nu> | 2020-06-26 00:36:38 +0200 |
---|---|---|
committer | David Härdeman <david@hardeman.nu> | 2020-06-26 00:36:38 +0200 |
commit | 0993c40ec87095c0a1e29b48ba03bdadc1fdb102 (patch) | |
tree | 86a62794a6775bf8c938e2e691c268be5a43e1c2 | |
parent | 30e19ad39ccc099b1b41f4df9be21d8d25cb8f45 (diff) |
Bring some more sanity to cmd parsing
-rw-r--r-- | minecctl/minecctl.c | 62 |
1 files changed, 23 insertions, 39 deletions
diff --git a/minecctl/minecctl.c b/minecctl/minecctl.c index 6d60a32..969b04d 100644 --- a/minecctl/minecctl.c +++ b/minecctl/minecctl.c @@ -451,7 +451,7 @@ get_optional_server_arg(char * const **argv, bool more) { if (!cfg->addrstr) { if (!**argv) { - error("Missing argument"); + error("Missing arguments"); usage(false); } set_server(**argv); @@ -465,16 +465,16 @@ get_optional_server_arg(char * const **argv, bool more) } static void -parse_command(unsigned argc, char * const *argv) +parse_command(char * const *argv) { - enum commands cmd; + enum commands cmd = CMD_INVALID; assert_die(argv, "invalid arguments"); /* Shorthand for console if address is set */ - if (argc == 0) { + if (!*argv) { if (!cfg->addrstr) { - error("Missing options"); + error("Missing arguments"); usage(false); } @@ -482,7 +482,6 @@ parse_command(unsigned argc, char * const *argv) return; } - cmd = CMD_INVALID; for (unsigned i = 0; command_list[i].name; i++) { if (streq(*argv, command_list[i].name)) { cmd = command_list[i].cmd; @@ -492,32 +491,16 @@ parse_command(unsigned argc, char * const *argv) } switch (cmd) { - case CMD_INVALID: - /* shorthand notation? */ - if (!cfg->addrstr) - /* SERVER [CMD...] */ - set_server(*argv++); - - /* !CMD = console */ - if (!*argv) { - cfg->cmd = do_console; - break; - } - - /* CMD... */ - cfg->cmdstr = strv_join(argv); - cfg->cmd = do_command; - break; case CMD_LIST: if (*argv) { - error("Too many options"); + error("Too many arguments"); usage(false); } cfg->cmd = do_list; break; case CMD_STOPALL: if (*argv) { - error("Too many options"); + error("Too many arguments"); usage(false); } cfg->cmd = do_list; @@ -543,22 +526,29 @@ parse_command(unsigned argc, char * const *argv) cfg->cmd = do_console; break; case CMD_COMMAND: - if (!*argv) { - error("Missing options"); - usage(false); - } - - if (!cfg->addrstr) - set_server(*argv++); + get_optional_server_arg(&argv, true); if (!*argv) { - error("Missing options"); + error("Missing arguments"); usage(false); } cfg->cmdstr = strv_join(argv); cfg->cmd = do_command; break; + case CMD_INVALID: + /* shorthand notation */ + get_optional_server_arg(&argv, true); + + if (!*argv) { + /* !CMD = console */ + cfg->cmd = do_console; + } else { + /* CMD... */ + cfg->cmdstr = strv_join(argv); + cfg->cmd = do_command; + } + break; default: die("Unreachable"); } @@ -660,16 +650,10 @@ main(int argc, char **argv) list_init(&cfg->known_servers); parse_cmdline(argc, argv); - info("cmdline parsed"); - dump_config(); get_known_servers(); - info("known servers loaded"); - dump_config(); - parse_command(argc - optind, &argv[optind]); - info("command parsed"); - dump_config(); + parse_command(&argv[optind]); if (!cfg->cmd) die("Command not parsed correctly"); |