From cc4357f0a63181695309ad4a1e29c2be2a0c9f4f Mon Sep 17 00:00:00 2001 From: William Casarin Date: Mon, 8 Oct 2018 18:09:54 -0700 Subject: [PATCH] rpc: add json_add_help_command for help command objects Instead of two code paths that return different help objects, simplify things by always returning the full help object. This not only includes description and the command name, but the verbose description as well. Signed-off-by: William Casarin --- lightningd/jsonrpc.c | 64 +++++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 30 deletions(-) diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index 70e3d2423..66251f264 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -189,6 +189,36 @@ static struct json_command **get_cmdlist(void) return cmdlist; } +static void json_add_help_command(struct command *cmd, + struct json_result *response, + struct json_command *json_command) +{ + char *usage; + cmd->mode = CMD_USAGE; + json_command->dispatch(cmd, NULL, NULL); + usage = tal_fmt(cmd, "%s %s", json_command->name, cmd->usage); + + json_object_start(response, NULL); + + json_add_string(response, "command", usage); + json_add_string(response, "description", json_command->description); + + if (!json_command->verbose) { + json_add_string(response, "verbose", + "HELP! Please contribute" + " a description for this" + " json_command!"); + } else { + struct json_escaped *esc; + + esc = json_escape(NULL, json_command->verbose); + json_add_escaped_string(response, "verbose", take(esc)); + } + + json_object_end(response); + +} + static void json_help(struct command *cmd, const char *buffer, const jsmntok_t *params) { @@ -196,34 +226,16 @@ static void json_help(struct command *cmd, struct json_result *response = new_json_result(cmd); struct json_command **cmdlist = get_cmdlist(); const jsmntok_t *cmdtok; - char *usage; - /* FIXME: This is never called with a command parameter because lightning-cli - * attempts to launch the man page and then exits. */ if (!param(cmd, buffer, params, p_opt("command", json_tok_tok, &cmdtok), NULL)) return; - json_object_start(response, NULL); if (cmdtok) { for (i = 0; i < num_cmdlist; i++) { if (json_tok_streq(buffer, cmdtok, cmdlist[i]->name)) { - if (!cmdlist[i]->verbose) - json_add_string(response, - "verbose", - "HELP! Please contribute" - " a description for this" - " command!"); - else { - struct json_escaped *esc; - - esc = json_escape(NULL, - cmdlist[i]->verbose); - json_add_escaped_string(response, - "verbose", - take(esc)); - } + json_add_help_command(cmd, response, cmdlist[i]); goto done; } } @@ -234,23 +246,15 @@ static void json_help(struct command *cmd, return; } - cmd->mode = CMD_USAGE; + json_object_start(response, NULL); json_array_start(response, "help"); for (i = 0; i < num_cmdlist; i++) { - cmdlist[i]->dispatch(cmd, NULL, NULL); - usage = tal_fmt(cmd, "%s %s", cmdlist[i]->name, - cmd->usage); - json_add_object(response, - "command", JSMN_STRING, - usage, - "description", JSMN_STRING, - cmdlist[i]->description, - NULL); + json_add_help_command(cmd, response, cmdlist[i]); } json_array_end(response); + json_object_end(response); done: - json_object_end(response); command_success(cmd, response); }