common/param: support renaming options using "|<deprecatedname>".

This is *much* easier to do inside parsing than in the caller!

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
This commit is contained in:
Rusty Russell 2022-06-19 16:45:11 +09:30
parent c5b032598e
commit 6e2a775ef2
3 changed files with 43 additions and 6 deletions

View file

@ -1,6 +1,8 @@
#include "config.h"
#include <ccan/asort/asort.h>
#include <ccan/mem/mem.h>
#include <ccan/tal/str/str.h>
#include <common/configdir.h>
#include <common/json_command.h>
#include <common/json_tok.h>
#include <common/param.h>
@ -106,9 +108,15 @@ static struct param *find_param(struct param *params, const char *start,
struct param *last = first + tal_count(params);
while (first != last) {
if (strncmp(first->name, start, n) == 0)
if (strlen(first->name) == n)
return first;
size_t arglen = strcspn(first->name, "|");
if (memeq(first->name, arglen, start, n))
return first;
if (deprecated_apis
&& first->name[arglen]
&& memeq(first->name + arglen + 1,
strlen(first->name + arglen + 1),
start, n))
return first;
first++;
}
return NULL;
@ -237,12 +245,14 @@ static char *param_usage(const tal_t *ctx,
{
char *usage = tal_strdup(ctx, "");
for (size_t i = 0; i < tal_count(params); i++) {
/* Don't print |deprecated part! */
int len = strcspn(params[i].name, "|");
if (i != 0)
tal_append_fmt(&usage, " ");
if (params[i].style == PARAM_REQUIRED)
tal_append_fmt(&usage, "%s", params[i].name);
tal_append_fmt(&usage, "%.*s", len, params[i].name);
else
tal_append_fmt(&usage, "[%s]", params[i].name);
tal_append_fmt(&usage, "[%.*s]", len, params[i].name);
}
return usage;
}

View file

@ -19,7 +19,7 @@
*
* if (!param(cmd, buffer, params,
* p_req("cltv", json_tok_number, &cltv),
* p_opt("msatoshi", json_tok_u64, &msatoshi),
* p_opt("amount_msat|msatoshi", json_tok_u64, &msatoshi),
* p_opt("note", json_tok_tok, &note),
* p_opt_def("expiry", json_tok_u64, &expiry, 3600),
* NULL))
@ -76,6 +76,7 @@ enum param_style {
/*
* Add a required parameter.
* name can be <normal>|<deprecated> if it's been renamed.
*/
#define p_req(name, cbx, arg) \
name"", \
@ -89,6 +90,7 @@ enum param_style {
/*
* Add an optional parameter. *arg is set to NULL if it isn't found.
* name can be <normal>|<deprecated> if it's been renamed.
*/
#define p_opt(name, cbx, arg) \
name"", \
@ -103,6 +105,7 @@ enum param_style {
/*
* Add an optional parameter. *arg is set to @def if it isn't found.
* name can be <normal>|<deprecated> if it's been renamed.
*/
#define p_opt_def(name, cbx, arg, def) \
name"", \

View file

@ -34,6 +34,8 @@ struct command_result *command_fail(struct command *cmd,
}
/* AUTOGENERATED MOCKS START */
/* Generated stub for deprecated_apis */
bool deprecated_apis;
/* Generated stub for fromwire_tlv */
bool fromwire_tlv(const u8 **cursor UNNEEDED, size_t *max UNNEEDED,
const struct tlv_record_type *types UNNEEDED, size_t num_types UNNEEDED,
@ -457,6 +459,27 @@ static void sendpay(void)
assert(*msatoshi == 547);
}
static void deprecated_rename(void)
{
struct json *j = json_parse(cmd, "{ 'u64': 42 }");
u64 *u64;
assert(param(cmd, j->buffer, j->toks,
p_req("u64|old_u64", param_u64, &u64),
NULL));
assert(*u64 == 42);
deprecated_apis = true;
j = json_parse(cmd, "{ 'old_u64': 42 }");
assert(param(cmd, j->buffer, j->toks,
p_req("u64|old_u64", param_u64, &u64),
NULL));
deprecated_apis = false;
assert(!param(cmd, j->buffer, j->toks,
p_req("u64|old_u64", param_u64, &u64),
NULL));
}
static void sendpay_nulltok(void)
{
struct json *j = json_parse(cmd, "[ 'A', '123']");
@ -632,6 +655,7 @@ int main(int argc, char *argv[])
advanced_fail();
param_tests();
usage();
deprecated_rename();
printf("run-params ok\n");
common_shutdown();