Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cli: don't crash when there's no argument #6568

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions cli/lightning-cli.c
Original file line number Diff line number Diff line change
Expand Up @@ -650,7 +650,7 @@ int main(int argc, char *argv[])
jsmntok_t *toks;
const jsmntok_t *result, *error, *id;
const tal_t *ctx = tal(NULL, char);
char *net_dir, *rpc_filename;
char *config_filename, *base_dir, *net_dir, *rpc_filename;
jsmn_parser parser;
int parserr;
enum format format = DEFAULT_FORMAT;
Expand All @@ -668,7 +668,8 @@ int main(int argc, char *argv[])
setup_option_allocators();

opt_exitcode = ERROR_USAGE;
minimal_config_opts(ctx, argc, argv, &net_dir, &rpc_filename);
minimal_config_opts(ctx, argc, argv, &config_filename, &base_dir,
&net_dir, &rpc_filename);

opt_register_noarg("--help|-h", opt_usage_and_exit,
"<command> [<params>...]", "Show this message. Use the command help (without hyphens -- \"lightning-cli help\") to get a list of all RPC commands");
Expand Down
10 changes: 6 additions & 4 deletions common/configdir.c
Original file line number Diff line number Diff line change
Expand Up @@ -281,16 +281,18 @@ static struct configvar **gather_cmdline_args(const tal_t *ctx,

void minimal_config_opts(const tal_t *ctx,
int argc, char *argv[],
char **config_filename,
char **basedir,
char **config_netdir,
char **rpc_filename)
{
char *unused_filename, *unused_basedir;

initial_config_opts(tmpctx, &argc, argv, false,
&unused_filename,
&unused_basedir,
config_filename,
basedir,
config_netdir,
rpc_filename);
tal_steal(ctx, *config_filename);
tal_steal(ctx, *basedir);
tal_steal(ctx, *config_netdir);
tal_steal(ctx, *rpc_filename);
}
Expand Down
2 changes: 2 additions & 0 deletions common/configdir.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ void setup_option_allocators(void);
/* Minimal config parsing for tools: use opt_early_parse/opt_parse after */
void minimal_config_opts(const tal_t *ctx,
int argc, char *argv[],
char **config_filename,
char **basedir,
char **config_netdir,
char **rpc_filename);

Expand Down
4 changes: 3 additions & 1 deletion devtools/checkchannels.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ static void copy_column(void *dst, size_t size,

int main(int argc, char *argv[])
{
char *config_filename, *base_dir;
char *net_dir, *rpc_filename, *hsmfile, *dbfile;
sqlite3 *sql;
sqlite3_stmt *stmt;
Expand All @@ -124,7 +125,8 @@ int main(int argc, char *argv[])

setup_option_allocators();

minimal_config_opts(top_ctx, argc, argv, &net_dir, &rpc_filename);
minimal_config_opts(top_ctx, argc, argv, &config_filename, &base_dir,
&net_dir, &rpc_filename);

opt_register_noarg("-v|--verbose", opt_set_bool, &verbose,
"Print everything");
Expand Down
7 changes: 7 additions & 0 deletions tests/test_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -1195,6 +1195,13 @@ def test_daemon_option(node_factory):
assert 'No child process' not in f.read()


def test_cli_no_argument():
"""If no arguments are provided, should display help and exit."""
out = subprocess.run(['cli/lightning-cli'], stdout=subprocess.PIPE)
assert out.returncode in [0, 2] # returns 2 if lightning-rpc not available
assert "Usage: cli/lightning-cli <command> [<params>...]" in out.stdout.decode()


@pytest.mark.developer("needs DEVELOPER=1")
def test_blockchaintrack(node_factory, bitcoind):
"""Check that we track the blockchain correctly across reorgs
Expand Down