diff --git a/CHANGELOG.md b/CHANGELOG.md index 1f9d39c8c..fda5a9a26 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c index 72b215854..f0dd270ca 100644 --- a/cli/lightning-cli.c +++ b/cli/lightning-cli.c @@ -201,6 +201,7 @@ static void human_help(char *buffer, const jsmntok_t *result) enum format { JSON, HUMAN, + HELPLIST, DEFAULT_FORMAT, RAW }; @@ -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(); @@ -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); @@ -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); diff --git a/doc/PLUGINS.md b/doc/PLUGINS.md index 83662750d..7e8a26287 100644 --- a/doc/PLUGINS.md +++ b/doc/PLUGINS.md @@ -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 diff --git a/lightningd/jsonrpc.c b/lightningd/jsonrpc.c index de70d7af8..e6765afb8 100644 --- a/lightningd/jsonrpc.c +++ b/lightningd/jsonrpc.c @@ -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); }