From 679d3494b4d8f114c7710a673013e18902e5b009 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Mon, 11 May 2020 10:11:16 +0930 Subject: [PATCH] cli: tweak -H output to remove format-hint fields. -H removes the top-level if there's only one, and 'format-hint' breaks this heuristic, so we end up with: ``` help=command=autocleaninvoice [cycle_seconds] [expired_by] category=plugin description=Set up autoclean of expired invoices. verbose=Perform cleanup every {cycle_seconds} (default 3600), or disable autoclean if 0. Clean up expired invoices that have expired for {expired_by} seconds (default 86400). command=check command_to_check ``` Signed-off-by: Rusty Russell --- cli/lightning-cli.c | 30 +++++++++++++++--------------- tests/test_misc.py | 11 +++++++++++ 2 files changed, 26 insertions(+), 15 deletions(-) diff --git a/cli/lightning-cli.c b/cli/lightning-cli.c index 5812fb4e9..0f6187049 100644 --- a/cli/lightning-cli.c +++ b/cli/lightning-cli.c @@ -375,13 +375,15 @@ static void tal_error(const char *msg) abort(); } -static enum format delete_format_hint(const char *resp, - jsmntok_t **toks, - jsmntok_t *result) +static enum format delete_format_hint(const char *resp, jsmntok_t **toks) { + const jsmntok_t *result = json_get_member(resp, *toks, "result"); const jsmntok_t *hint; enum format format = JSON; + if (!result) + return format; + hint = json_get_member(resp, result, "format-hint"); if (!hint) return format; @@ -390,7 +392,8 @@ static enum format delete_format_hint(const char *resp, format = HUMAN; /* Don't let hint appear in the output! */ - json_tok_remove(toks, result, hint-1, 1); + /* Note the aritmetic on *toks for const-washing */ + json_tok_remove(toks, *toks + (result - *toks), hint-1, 1); return format; } @@ -401,22 +404,19 @@ static enum format choose_format(const char *resp, enum format format) { /* If they specify a format, that's what we use. */ - if (format != DEFAULT_FORMAT) + if (format != DEFAULT_FORMAT) { + /* But humans don't want to see the format hint! */ + if (format == HUMAN) + delete_format_hint(resp, toks); return format; + } /* This works best when we order it. */ if (streq(method, "help") && command == NULL) format = HELPLIST; - else { - const jsmntok_t *result = json_get_member(resp, *toks, "result"); - if (result) - /* Use offset of result to get non-const ptr */ - format = delete_format_hint(resp, toks, - /* const-washing */ - *toks + (result - *toks)); - else - format = JSON; - } + else + format = delete_format_hint(resp, toks); + return format; } diff --git a/tests/test_misc.py b/tests/test_misc.py index 155eec7c7..ba584a247 100644 --- a/tests/test_misc.py +++ b/tests/test_misc.py @@ -1052,6 +1052,17 @@ def test_cli(node_factory): ' ]', '}'] + # Make sure we omit top-levels and don't include format hint, when -H forced + out = subprocess.check_output(['cli/lightning-cli', + '--network={}'.format(TEST_NETWORK), + '--lightning-dir={}' + .format(l1.daemon.lightning_dir), + '-H', + 'help']).decode('utf-8') + lines = out.splitlines() + assert [l for l in lines if l.startswith('help=')] == [] + assert [l for l in lines if l.startswith('format-hint=')] == [] + def test_daemon_option(node_factory): """