Skip to content

Commit 86abb4c

Browse files
committed
lightningd: use OPT_EXITS for options which exit.
Clearly, listconfigs shouldn't list these. Also, hoist the opt_hidden check since it's independent of whether there's an arg or not. Signed-off-by: Rusty Russell <[email protected]>
1 parent de148fe commit 86abb4c

File tree

8 files changed

+38
-36
lines changed

8 files changed

+38
-36
lines changed

cli/test/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ CLI_TEST_COMMON_OBJS := \
1919
common/msg_queue.o \
2020
common/setup.o \
2121
common/utils.o \
22+
common/version.o \
2223
common/type_to_string.o \
2324
common/permute_tx.o
2425

cli/test/run-human-mode.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id U
107107
/* Generated stub for towire_node_id */
108108
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
109109
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
110-
/* Generated stub for version_and_exit */
111-
char *version_and_exit(const void *unused UNNEEDED)
112-
{ fprintf(stderr, "version_and_exit called!\n"); abort(); }
113110
/* AUTOGENERATED MOCKS END */
114111

115112
int test_socket(int domain UNUSED, int type UNUSED, int protocol UNUSED)

cli/test/run-large-input.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id U
107107
/* Generated stub for towire_node_id */
108108
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
109109
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
110-
/* Generated stub for version_and_exit */
111-
char *version_and_exit(const void *unused UNNEEDED)
112-
{ fprintf(stderr, "version_and_exit called!\n"); abort(); }
113110
/* AUTOGENERATED MOCKS END */
114111

115112
int test_socket(int domain UNUSED, int type UNUSED, int protocol UNUSED)

cli/test/run-remove-hint.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -110,9 +110,6 @@ void towire_channel_id(u8 **pptr UNNEEDED, const struct channel_id *channel_id U
110110
/* Generated stub for towire_node_id */
111111
void towire_node_id(u8 **pptr UNNEEDED, const struct node_id *id UNNEEDED)
112112
{ fprintf(stderr, "towire_node_id called!\n"); abort(); }
113-
/* Generated stub for version_and_exit */
114-
char *version_and_exit(const void *unused UNNEEDED)
115-
{ fprintf(stderr, "version_and_exit called!\n"); abort(); }
116113
/* AUTOGENERATED MOCKS END */
117114

118115
int test_socket(int domain UNUSED, int type UNUSED, int protocol UNUSED)

common/configvar.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,8 +50,10 @@ struct configvar {
5050
#define OPT_MULTI (1 << OPT_USER_START)
5151
/* Set if developer-only */
5252
#define OPT_DEV (1 << (OPT_USER_START+1))
53+
/* Doesn't return, so don't show in listconfigs */
54+
#define OPT_EXITS (1 << (OPT_USER_START+2))
5355

54-
/* Use this instead of opt_register_*_arg if you want OPT_MULTI/OPT_DEV */
56+
/* Use this instead of opt_register_*_arg if you want OPT_* from above */
5557
#define clnopt_witharg(names, type, cb, show, arg, desc) \
5658
_opt_register((names), \
5759
OPT_CB_ARG((cb), (type), (show), (arg)), \

common/version.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include "config.h"
22
#include <ccan/compiler/compiler.h>
3+
#include <common/configvar.h>
34
#include <common/version.h>
45
#include <stdio.h>
56
#include <stdlib.h>
@@ -13,7 +14,7 @@ const char *version(void)
1314
return VERSION;
1415
}
1516

16-
char *version_and_exit(const void *unused UNUSED)
17+
static char *version_and_exit(const void *unused UNUSED)
1718
{
1819
printf("%s\n", VERSION);
1920
if (BUILD_FEATURES[0]) {
@@ -22,6 +23,13 @@ char *version_and_exit(const void *unused UNUSED)
2223
exit(0);
2324
}
2425

26+
void opt_register_version(void)
27+
{
28+
clnopt_noarg("--version|-V", OPT_EARLY|OPT_EXITS,
29+
version_and_exit, NULL,
30+
"Print version and exit");
31+
}
32+
2533
static bool cmp_release_version(const char *version) {
2634
if (version[0] != 'v')
2735
return false;

common/version.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@
33
#include "config.h"
44
#include <stdbool.h>
55

6-
char *version_and_exit(const void *unused);
6+
/* Add --version|-V option */
7+
void opt_register_version(void);
8+
79
const char *version(void);
810
/* check if the current version is a release version.
911
*
1012
* Released versions are of form v[year].[month]?(.patch)* */
1113
bool is_released_version(void);
1214

13-
#define opt_register_version() \
14-
opt_register_early_noarg("--version|-V", version_and_exit, NULL, \
15-
"Print version and exit")
1615

1716
#endif /* LIGHTNING_COMMON_VERSION_H */

lightningd/options.c

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1212,9 +1212,10 @@ static char *opt_set_announce_dns(const char *optarg, struct lightningd *ld)
12121212
static void register_opts(struct lightningd *ld)
12131213
{
12141214
/* This happens before plugins started */
1215-
opt_register_early_noarg("--test-daemons-only",
1216-
test_subdaemons_and_exit,
1217-
ld, opt_hidden);
1215+
clnopt_noarg("--test-daemons-only", OPT_EARLY|OPT_EXITS,
1216+
test_subdaemons_and_exit,
1217+
ld,
1218+
"Test that subdaemons can be run, then exit immediately");
12181219
/* Register plugins as an early args, so we can initialize them and have
12191220
* them register more command line options */
12201221
clnopt_witharg("--plugin", OPT_MULTI|OPT_EARLY,
@@ -1283,8 +1284,8 @@ static void register_opts(struct lightningd *ld)
12831284
opt_set_announce_dns, NULL,
12841285
ld, opt_hidden);
12851286

1286-
opt_register_noarg("--help|-h", opt_lightningd_usage, ld,
1287-
"Print this message.");
1287+
clnopt_noarg("--help|-h", OPT_EXITS,
1288+
opt_lightningd_usage, ld, "Print this message.");
12881289
opt_register_arg("--rgb", opt_set_rgb, opt_show_rgb, ld,
12891290
"RRGGBB hex color for node");
12901291
opt_register_arg("--alias", opt_set_alias, opt_show_alias, ld,
@@ -1550,9 +1551,9 @@ void handle_early_opts(struct lightningd *ld, int argc, char *argv[])
15501551
setup_option_allocators();
15511552

15521553
/*~ List features immediately, before doing anything interesting */
1553-
opt_register_early_noarg("--list-features-only",
1554-
list_features_and_exit,
1555-
ld, opt_hidden);
1554+
clnopt_noarg("--list-features-only", OPT_EARLY|OPT_EXITS,
1555+
list_features_and_exit,
1556+
ld, "List the features configured, and exit immediately");
15561557

15571558
/*~ This does enough parsing to get us the base configuration options */
15581559
ld->configvars = initial_config_opts(ld, &argc, argv, true,
@@ -1710,17 +1711,19 @@ static void add_config(struct lightningd *ld,
17101711
if (opt->type & OPT_DEV)
17111712
return;
17121713

1714+
/* Ignore things which just exit */
1715+
if (opt->type & OPT_EXITS)
1716+
return;
1717+
1718+
/* Ignore hidden options (deprecated) */
1719+
if (opt->desc == opt_hidden)
1720+
return;
1721+
17131722
if (opt->type & OPT_NOARG) {
1714-
if (opt->desc == opt_hidden) {
1715-
/* Ignore hidden options (deprecated) */
1716-
} else if (opt->cb == (void *)opt_usage_and_exit
1717-
|| opt->cb == (void *)version_and_exit
1718-
|| is_restricted_ignored(opt->cb)
1719-
|| opt->cb == (void *)opt_lightningd_usage
1720-
|| opt->cb == (void *)test_subdaemons_and_exit
1721-
/* FIXME: we can't recover this. */
1722-
|| opt->cb == (void *)opt_clear_plugins) {
1723-
/* These are not important */
1723+
if (opt->cb == (void *)opt_clear_plugins) {
1724+
/* FIXME: we can't recover this. */
1725+
} else if (is_restricted_ignored(opt->cb)) {
1726+
/* --testnet etc, turned into --network=. */
17241727
} else if (opt->cb == (void *)opt_set_bool) {
17251728
const bool *b = opt->u.carg;
17261729
json_add_bool(response, name0, *b);
@@ -1775,9 +1778,7 @@ static void add_config(struct lightningd *ld,
17751778
errx(1, "Unknown decode for %s", opt->names);
17761779
}
17771780
} else if (opt->type & OPT_HASARG) {
1778-
if (opt->desc == opt_hidden) {
1779-
/* Ignore hidden options (deprecated) */
1780-
} else if (opt->show == (void *)opt_show_charp) {
1781+
if (opt->show == (void *)opt_show_charp) {
17811782
if (*(char **)opt->u.carg)
17821783
/* Don't truncate or quote! */
17831784
answer = tal_strdup(tmpctx,

0 commit comments

Comments
 (0)