Skip to content

Commit 4fdf8af

Browse files
authored
Merge pull request #1733 from promansk/fix-cli-help-text
crun: fix the binary name displayed in the Usage info for commands.
2 parents 486c5d1 + 99c7b95 commit 4fdf8af

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

src/crun.c

+16-2
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,16 @@ fill_handler_from_argv0 (char *argv0, struct crun_global_arguments *args)
427427
args->handler = b + 5;
428428
}
429429

430+
static char **
431+
copy_args (char **argv, int argc)
432+
{
433+
char **buff = xmalloc0 ((argc + 1) * sizeof (char *));
434+
for (int i = 0; i < argc; i++)
435+
buff[i] = argv[i];
436+
437+
return buff;
438+
}
439+
430440
int
431441
main (int argc, char **argv)
432442
{
@@ -448,15 +458,19 @@ main (int argc, char **argv)
448458
#endif
449459

450460
fill_handler_from_argv0 (argv[0], &arguments);
451-
452461
argp_parse (&argp, argc, argv, ARGP_IN_ORDER, &first_argument, &arguments);
453462

454463
command = get_command (argv[first_argument]);
455464
if (command == NULL)
456465
libcrun_fail_with_error (0, "unknown command %s", argv[first_argument]);
457466

458-
ret = command->handler (&arguments, argc - first_argument, argv + first_argument, &err);
467+
int command_argc = argc - first_argument;
468+
cleanup_free char **command_argv = copy_args (argv + first_argument, command_argc);
469+
command_argv[0] = argv[0];
470+
471+
ret = command->handler (&arguments, command_argc, command_argv, &err);
459472
if (ret && err)
460473
libcrun_fail_with_error (err->status, "%s", err->msg);
474+
461475
return ret;
462476
}

tests/test_delete.py

+7
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,17 @@ def test_multiple_containers_delete():
9696
return -1
9797
return 0
9898

99+
def test_help_delete():
100+
out = run_crun_command(["delete", "--help"])
101+
if "Usage: crun [OPTION...] delete CONTAINER" not in out:
102+
return -1
103+
104+
return 0
99105

100106
all_tests = {
101107
"test_simple_delete" : test_simple_delete,
102108
"test_multiple_containers_delete" : test_multiple_containers_delete,
109+
"test_help_delete": test_help_delete,
103110
}
104111

105112
if __name__ == "__main__":

tests/test_exec.py

+8
Original file line numberDiff line numberDiff line change
@@ -464,6 +464,13 @@ def test_exec_getpgrp():
464464
run_crun_command(["delete", "-f", cid])
465465
return 0
466466

467+
def test_exec_help():
468+
out = run_crun_command(["exec", "--help"])
469+
if "Usage: crun [OPTION...] exec CONTAINER cmd" not in out:
470+
return -1
471+
472+
return 0
473+
467474
all_tests = {
468475
"exec" : test_exec,
469476
"exec-not-exists" : test_exec_not_exists,
@@ -479,6 +486,7 @@ def test_exec_getpgrp():
479486
"exec-test-uid-tty": test_uid_tty,
480487
"exec-cpu-affinity": test_exec_cpu_affinity,
481488
"exec-getpgrp": test_exec_getpgrp,
489+
"exec-help" : test_exec_help,
482490
}
483491

484492
if __name__ == "__main__":

tests/test_mounts.py

+8
Original file line numberDiff line numberDiff line change
@@ -669,6 +669,13 @@ def check_test_file(expected):
669669
shutil.rmtree(bind_dir)
670670
return 0
671671

672+
def test_mount_help():
673+
out = run_crun_command(["mounts", "--help"])
674+
if "Usage: crun [OPTION...] mounts [add|remove] CONTAINER FILE" not in out:
675+
return -1
676+
677+
return 0
678+
672679
all_tests = {
673680
"mount-ro" : test_mount_ro,
674681
"mount-rro" : test_mount_rro,
@@ -698,6 +705,7 @@ def check_test_file(expected):
698705
"mount-copy-symlink": test_copy_symlink,
699706
"mount-tmpfs-permissions": test_mount_tmpfs_permissions,
700707
"mount-add-remove-mounts": test_add_remove_mounts,
708+
"mount-help": test_mount_help,
701709
}
702710

703711
if __name__ == "__main__":

tests/test_start.py

+8
Original file line numberDiff line numberDiff line change
@@ -521,6 +521,13 @@ def test_home_unknown_id():
521521
return -1
522522
return 0
523523

524+
def test_start_help():
525+
out = run_crun_command(["start", "--help"])
526+
if "Usage: crun [OPTION...] start CONTAINER" not in out:
527+
return -1
528+
529+
return 0
530+
524531
all_tests = {
525532
"start" : test_start,
526533
"start-override-config" : test_start_override_config,
@@ -541,6 +548,7 @@ def test_home_unknown_id():
541548
"run-keep": test_run_keep,
542549
"invalid-id": test_invalid_id,
543550
"home-unknown-id": test_home_unknown_id,
551+
"help": test_start_help,
544552
}
545553

546554
if __name__ == "__main__":

tests/test_update.py

+7
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,16 @@ def test_update():
4949
shutil.rmtree(temp_dir)
5050
return 1
5151

52+
def test_update_help():
53+
out = run_crun_command(["update", "--help"])
54+
if "Usage: crun [OPTION...] update [OPTION]... CONTAINER" not in out:
55+
return -1
56+
57+
return 0
5258

5359
all_tests = {
5460
"test-update" : test_update,
61+
"test-update-help": test_update_help,
5562
}
5663

5764
if __name__ == "__main__":

0 commit comments

Comments
 (0)