From 4f0c8e03561507a3a0fc9aaa95f56057d7828ecf Mon Sep 17 00:00:00 2001 From: Tilka Date: Thu, 19 Apr 2012 03:06:52 +0200 Subject: [PATCH] fix multiple optional arguments Assuming the argument definition "?xy", it was previously not possible to specify only x. Also, the syntax will now be shown as "[x [y]]" instead of "[x] [y]". --- src/core/commands.c | 21 +++++++++++++-------- src/core/commands.h | 2 +- 2 files changed, 14 insertions(+), 9 deletions(-) diff --git a/src/core/commands.c b/src/core/commands.c index 311cfd8..b28a680 100644 --- a/src/core/commands.c +++ b/src/core/commands.c @@ -163,7 +163,10 @@ static enum command_parse_status command_extract_arguments(struct command_base* token = list_get_first(tokens); if (!token || !*token) { - status = (arg_code == '?' ? cmd_status_ok : cmd_status_missing_args); + if (arg_code == '?' || opt == 1) + status = cmd_status_ok; + else + status = cmd_status_missing_args; break; } @@ -257,7 +260,7 @@ static enum command_parse_status command_extract_arguments(struct command_base* { hub_free(data); data = NULL; - return cmd_status_arg_number; + status = cmd_status_arg_number; } break; @@ -360,10 +363,13 @@ void command_get_syntax(struct command_handle* handler, struct cbuffer* buf) { for (n = 0; n < strlen(handler->args); n++) { - if (n > 0 && !opt) cbuf_append(buf, " "); + if (n > 0) + cbuf_append(buf, " "); + if (opt) + cbuf_append(buf, "["); switch (handler->args[n]) { - case '?': cbuf_append(buf, "["); opt = 1; continue; + case '?': opt = 1; continue; case 'n': cbuf_append(buf, ""); break; case 'u': cbuf_append(buf, ""); break; case 'i': cbuf_append(buf, ""); break; @@ -376,11 +382,10 @@ void command_get_syntax(struct command_handle* handler, struct cbuffer* buf) case 'N': cbuf_append(buf, ""); break; } if (opt) - { - cbuf_append(buf, "]"); - opt = 0; - } + opt++; } + while (opt--) + cbuf_append(buf, "]"); } } diff --git a/src/core/commands.h b/src/core/commands.h index 6688d05..2f629b4 100644 --- a/src/core/commands.h +++ b/src/core/commands.h @@ -83,7 +83,7 @@ struct hub_command { const char* message; /**<<< "The complete message." */ char* prefix; /**<<< "The prefix extracted from the message." */ - struct linked_list* args; /**<<< "List of all parsed arguments from the message. Type depends on expectations." */ + struct linked_list* args; /**<<< "List of arguments of type struct hub_command_arg_data. Parsed from message." */ enum command_parse_status status; /**<<< "Status of the hub_command." */ command_handler handler; /**<<< "The function handler to call in order to invoke this command." */ const struct hub_user* user; /**<<< "The user who invoked this command." */