Skip to content

Commit

Permalink
lightning-cli: change default printing in response to "format-hint": …
Browse files Browse the repository at this point in the history
…"simple".

And set it for 'help <command>'.

Signed-off-by: Rusty Russell <[email protected]>
  • Loading branch information
rustyrussell committed Jul 28, 2019
1 parent b03369e commit 6da420a
Show file tree
Hide file tree
Showing 4 changed files with 53 additions and 16 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Lightningd: add support for `signet` networks using the `--network=signet` or `--signet` startup option
- JSON API: `listfunds` now returns also `funding_output` for `channels`
- plugins: plugins can now suggest `lightning-cli` default to -H for responses.

### Changed

Expand Down
61 changes: 45 additions & 16 deletions cli/lightning-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,7 @@ static void human_help(char *buffer, const jsmntok_t *result)
enum format {
JSON,
HUMAN,
HELPLIST,
DEFAULT_FORMAT,
RAW
};
Expand Down Expand Up @@ -392,6 +393,25 @@ static void tal_error(const char *msg)
abort();
}

static enum format delete_format_hint(const char *resp,
jsmntok_t **toks,
jsmntok_t *result)
{
const jsmntok_t *hint;
enum format format = JSON;

hint = json_get_member(resp, result, "format-hint");
if (!hint)
return format;

if (json_tok_streq(resp, hint, "simple"))
format = HUMAN;

/* Don't let hint appear in the output! */
json_tok_remove(toks, result, hint, 1);
return format;
}

int main(int argc, char *argv[])
{
setup_locale();
Expand Down Expand Up @@ -453,16 +473,9 @@ int main(int argc, char *argv[])
method = "help";
}

if (format == DEFAULT_FORMAT) {
if (streq(method, "help"))
format = HUMAN;
else
format = JSON;
}

/* Launch a manpage if we have a help command with an argument. We do
* not need to have lightningd running in this case. */
if (streq(method, "help") && format == HUMAN && argc >= 3) {
if (streq(method, "help") && format == DEFAULT_FORMAT && argc >= 3) {
command = argv[2];
char *page = tal_fmt(ctx, "lightning-%s", command);

Expand Down Expand Up @@ -591,19 +604,35 @@ int main(int argc, char *argv[])
json_tok_full_len(id), json_tok_full(resp, id));

if (!error || json_tok_is_null(resp, error)) {
// if we have specific help command
if (format == HUMAN)
if (format == DEFAULT_FORMAT) {
/* This works best when we order it. */
if (streq(method, "help") && command == NULL)
human_help(resp, result);
format = HELPLIST;
else
human_readable(resp, result, '\n');
else if (format == RAW)
/* Use offset of result to get non-const ptr */
format = delete_format_hint(resp, &toks,
/* const-washing */
toks + (result - toks));
}

switch (format) {
case HELPLIST:
human_help(resp, result);
break;
case HUMAN:
human_readable(resp, result, '\n');
break;
case JSON:
print_json(resp, result, "");
printf("\n");
break;
case RAW:
printf("%.*s\n",
json_tok_full_len(result),
json_tok_full(resp, result));
else {
print_json(resp, result, "");
printf("\n");
break;
default:
abort();
}
tal_free(lightning_dir);
tal_free(rpc_filename);
Expand Down
4 changes: 4 additions & 0 deletions doc/PLUGINS.md
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,10 @@ the JSON-RPC call `id`, which is internally remapped to a unique
integer instead, in order to avoid collisions. When passing the result
back the `id` field is restored to its original value.

Note that if your `result` for an RPC call includes `"format-hint":
"simple"`, then `lightning-cli` will default to printing your output
in "human-readable" flat form.

## Event notifications

Event notifications allow a plugin to subscribe to events in
Expand Down
3 changes: 3 additions & 0 deletions lightningd/jsonrpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,9 @@ static struct command_result *json_help(struct command *cmd,
}
json_array_end(response);

/* Tell cli this is simple enough to be formatted flat for humans */
json_add_string(response, "format-hint", "simple");

return command_success(cmd, response);
}

Expand Down

0 comments on commit 6da420a

Please sign in to comment.