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 <jb55@jb55.com>
This commit is contained in:
William Casarin 2018-10-08 18:09:54 -07:00 committed by Rusty Russell
parent 762c795c9b
commit cc4357f0a6

View file

@ -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);
}