Browse Source

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 <rusty@rustcorp.com.au>
nifty/pset-pre
Rusty Russell 5 years ago
committed by Christian Decker
parent
commit
679d3494b4
  1. 30
      cli/lightning-cli.c
  2. 11
      tests/test_misc.py

30
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;
}

11
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):
"""

Loading…
Cancel
Save