Skip to content

Commit

Permalink
cli: help command now also prints usage
Browse files Browse the repository at this point in the history
The help command now adds command usage to its output by calling each
command handler in CMD_USAGE mode.

Instead of seeing, for example:

	decodepay
	    Decode {bolt11}, using {description} if necessary

we see:

	decodepay bolt11 [description]
	    Decode {bolt11}, using {description} if necessary

Signed-off-by: Mark Beckwith <[email protected]>
  • Loading branch information
wythe authored and cdecker committed Sep 25, 2018
1 parent 1a4f355 commit cbde3e2
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 5 deletions.
11 changes: 9 additions & 2 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ static void json_help(struct command *cmd,
static const struct json_command help_command = {
"help",
json_help,
"List available commands, or give verbose help on one command.",
"List available commands, or give verbose help on one {command}.",

.verbose = "help [command]\n"
"Without [command]:\n"
Expand Down Expand Up @@ -196,7 +196,10 @@ 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))
Expand Down Expand Up @@ -231,11 +234,15 @@ static void json_help(struct command *cmd,
return;
}

cmd->mode = CMD_USAGE;
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,
cmdlist[i]->name,
usage,
"description", JSMN_STRING,
cmdlist[i]->description,
NULL);
Expand Down
5 changes: 3 additions & 2 deletions lightningd/param.c
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ static bool param_arr(struct command *cmd, const char *buffer,
{
#if DEVELOPER
if (!check_params(params)) {
command_fail(cmd, PARAM_DEV_ERROR, "developer error");
command_fail(cmd, PARAM_DEV_ERROR, "developer error: check_params");
return false;
}
#endif
Expand All @@ -271,7 +271,8 @@ bool param(struct command *cmd, const char *buffer,
param_cbx cbx = va_arg(ap, param_cbx);
void *arg = va_arg(ap, void *);
if (!param_add(&params, name, required, cbx, arg)) {
command_fail(cmd, PARAM_DEV_ERROR, "developer error");
command_fail(cmd, PARAM_DEV_ERROR,
"developer error: param_add %s", name);
va_end(ap);
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -622,7 +622,7 @@ def test_cli(node_factory):
.format(l1.daemon.lightning_dir),
'help']).decode('utf-8')
# Test some known output.
assert 'help\n List available commands, or give verbose help on one command' in out
assert 'help [command]\n List available commands, or give verbose help on one {command}' in out

# Test JSON output.
out = subprocess.check_output(['cli/lightning-cli',
Expand Down

0 comments on commit cbde3e2

Please sign in to comment.