Skip to content

Commit

Permalink
rpc: add json_add_help_command for help command objects
Browse files Browse the repository at this point in the history
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 <[email protected]>
  • Loading branch information
jb55 authored and rustyrussell committed Oct 10, 2018
1 parent 762c795 commit cc4357f
Showing 1 changed file with 34 additions and 30 deletions.
64 changes: 34 additions & 30 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -189,41 +189,53 @@ 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)
{
unsigned int i;
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;
}
}
Expand All @@ -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);
}

Expand Down

0 comments on commit cc4357f

Please sign in to comment.