btrfs-progs: help: don't print usage on wrong argument counts

The error message about the unsatisfied argument count is scrolled away
by the full usage string dump. This is not considered a good usability
practice.

This commit switches all direct usage -> return patterns, where the
argument check has no other constraint, eg. dependency on an option.

Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
David Sterba 2019-03-04 14:49:15 +01:00
parent 4ac44631a4
commit 4ac63ccbd8
23 changed files with 66 additions and 69 deletions

View file

@ -1235,7 +1235,7 @@ int main(int argc, char **argv)
}
set_argv0(argv);
if (check_argc_min(argc - optind, 1))
print_usage(1);
return 1;
dev = argv[optind];
radix_tree_init();

View file

@ -70,13 +70,13 @@ int main(int argc, char **argv)
if (!loop) {
if (check_argc_exact(argc - optind, 1))
print_usage(255);
return 1;
printf("%12u - %s\n", crc32c(~1, str, strlen(str)), str);
return 0;
}
if (check_argc_exact(argc - optind, 0))
print_usage(255);
return 1;
buf = malloc(length);
if (!buf)

View file

@ -442,7 +442,7 @@ int main(int argc, char **argv)
set_argv0(argv);
if (check_argc_min(argc - optind, 1))
fragments_usage();
return 1;
path = argv[optind++];

View file

@ -252,7 +252,7 @@ int main(int argc, char **argv)
}
set_argv0(argv);
if (check_argc_min(argc - optind, 1))
print_usage();
return 1;
if (logical == 0)
print_usage();

View file

@ -67,7 +67,7 @@ int main(int argc, char **argv)
}
set_argv0(argv);
if (check_argc_exact(argc - optind, 1))
print_usage();
return 1;
if (bytenr == 0) {
fprintf(stderr, "Please select the super copy with -s\n");

View file

@ -547,10 +547,8 @@ int main(int argc, char *argv[])
set_argv0(argv);
device = argv[optind];
if (check_argc_exact(argc - optind, 1)) {
print_usage();
if (check_argc_exact(argc - optind, 1))
return 1;
}
if (random_fsid && new_fsid_str) {
error("random fsid can't be used with specified fsid");

View file

@ -591,7 +591,7 @@ static int cmd_balance_start(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_balance_start_usage);
return 1;
/*
* allow -s only under --force, otherwise do with system chunks
@ -691,7 +691,7 @@ static int cmd_balance_pause(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_balance_pause_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_balance_pause_usage);
return 1;
path = argv[optind];
@ -729,7 +729,7 @@ static int cmd_balance_cancel(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_balance_cancel_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_balance_cancel_usage);
return 1;
path = argv[optind];
@ -768,7 +768,7 @@ static int cmd_balance_resume(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_balance_resume_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_balance_resume_usage);
return 1;
path = argv[optind];
@ -855,7 +855,7 @@ static int cmd_balance_status(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_balance_status_usage);
return 1;
path = argv[optind];

View file

@ -83,7 +83,7 @@ static int cmd_device_add(int argc, char **argv)
}
if (check_argc_min(argc - optind, 2))
usage(cmd_device_add_usage);
return 1;
last_dev = argc - 1;
mntpnt = argv[last_dev];
@ -153,7 +153,7 @@ static int _cmd_device_remove(int argc, char **argv,
clean_args_no_options(argc, argv, usagestr);
if (check_argc_min(argc - optind, 2))
usage(usagestr);
return 1;
mntpnt = argv[argc - 1];
@ -405,7 +405,7 @@ static int cmd_device_ready(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_device_ready_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_device_ready_usage);
return 1;
fd = open("/dev/btrfs-control", O_RDWR);
if (fd < 0) {
@ -492,7 +492,7 @@ static int cmd_device_stats(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_device_stats_usage);
return 1;
dev_path = argv[optind];
@ -629,7 +629,7 @@ static int cmd_device_usage(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_device_usage_usage);
if (check_argc_min(argc - optind, 1))
usage(cmd_device_usage_usage);
return 1;
for (i = optind; i < argc; i++) {
int fd;

View file

@ -586,7 +586,7 @@ int cmd_filesystem_du(int argc, char **argv)
}
if (check_argc_min(argc - optind, 1))
usage(cmd_filesystem_du_usage);
return 1;
kernel_version = get_running_kernel_version();

View file

@ -992,7 +992,7 @@ int cmd_filesystem_usage(int argc, char **argv)
}
if (check_argc_min(argc - optind, 1))
usage(cmd_filesystem_usage_usage);
return 1;
for (i = optind; i < argc; i++) {
int fd;

View file

@ -131,7 +131,7 @@ static int cmd_filesystem_df(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_filesystem_df_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_filesystem_df_usage);
return 1;
path = argv[optind];
@ -713,7 +713,7 @@ static int cmd_filesystem_show(int argc, char **argv)
}
if (check_argc_max(argc, optind + 1))
usage(cmd_filesystem_show_usage);
return 1;
if (argc > optind) {
search = argv[optind];
@ -824,7 +824,7 @@ static int cmd_filesystem_sync(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_filesystem_sync_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_filesystem_sync_usage);
return 1;
err = btrfs_util_sync(argv[optind]);
if (err) {
@ -970,7 +970,7 @@ static int cmd_filesystem_defrag(int argc, char **argv)
}
if (check_argc_min(argc - optind, 1))
usage(cmd_filesystem_defrag_usage);
return 1;
memset(&defrag_global_range, 0, sizeof(defrag_global_range));
defrag_global_range.start = start;
@ -1094,7 +1094,7 @@ static int cmd_filesystem_resize(int argc, char **argv)
clean_args_no_options_relaxed(argc, argv);
if (check_argc_exact(argc - optind, 2))
usage(cmd_filesystem_resize_usage);
return 1;
amount = argv[optind];
path = argv[optind + 1];
@ -1166,7 +1166,7 @@ static int cmd_filesystem_label(int argc, char **argv)
if (check_argc_min(argc - optind, 1) ||
check_argc_max(argc - optind, 2))
usage(cmd_filesystem_label_usage);
return 1;
if (argc - optind > 1) {
return set_label(argv[optind], argv[optind + 1]);

View file

@ -596,7 +596,7 @@ int cmd_inspect_dump_super(int argc, char **argv)
}
if (check_argc_min(argc - optind, 1))
usage(cmd_inspect_dump_super_usage);
return 1;
for (i = optind; i < argc; i++) {
filename = argv[i];

View file

@ -319,7 +319,7 @@ int cmd_inspect_dump_tree(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_inspect_dump_tree_usage);
return 1;
ret = check_arg_type(argv[optind]);
if (ret != BTRFS_ARG_BLKDEV && ret != BTRFS_ARG_REG) {

View file

@ -448,9 +448,8 @@ int cmd_inspect_tree_stats(int argc, char **argv)
}
}
if (check_argc_exact(argc - optind, 1)) {
usage(cmd_inspect_tree_stats_usage);
}
if (check_argc_exact(argc - optind, 1))
return 1;
ret = check_mounted(argv[optind]);
if (ret < 0) {

View file

@ -111,7 +111,7 @@ static int cmd_inspect_inode_resolve(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 2))
usage(cmd_inspect_inode_resolve_usage);
return 1;
fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
if (fd < 0)
@ -173,7 +173,7 @@ static int cmd_inspect_logical_resolve(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 2))
usage(cmd_inspect_logical_resolve_usage);
return 1;
size = min(size, (u64)SZ_64K);
inodes = malloc(size);
@ -281,7 +281,7 @@ static int cmd_inspect_subvolid_resolve(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_inspect_subvolid_resolve_usage);
if (check_argc_exact(argc - optind, 2))
usage(cmd_inspect_subvolid_resolve_usage);
return 1;
fd = btrfs_open_dir(argv[optind + 1], &dirstream, 1);
if (fd < 0) {
@ -322,7 +322,7 @@ static int cmd_inspect_rootid(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_inspect_rootid_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_inspect_rootid_usage);
return 1;
fd = btrfs_open_file_or_dir(argv[optind], &dirstream, 1);
if (fd < 0) {
@ -622,7 +622,7 @@ static int cmd_inspect_min_dev_size(int argc, char **argv)
}
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_inspect_min_dev_size_usage);
return 1;
fd = btrfs_open_dir(argv[optind], &dirstream, 1);
if (fd < 0) {

View file

@ -289,7 +289,7 @@ static void parse_args(int argc, char **argv,
if (check_argc_min(argc - optind, min_nonopt_args) ||
check_argc_max(argc - optind, max_nonopt_args))
usage(usage_str);
exit(1);
*types = 0;
if (type_str) {

View file

@ -77,7 +77,7 @@ static int _cmd_qgroup_assign(int assign, int argc, char **argv,
}
if (check_argc_exact(argc - optind, 3))
usage(usage_str);
return 1;
memset(&args, 0, sizeof(args));
args.assign = assign;
@ -367,7 +367,7 @@ static int cmd_qgroup_show(int argc, char **argv)
btrfs_qgroup_setup_units(unit_mode);
if (check_argc_exact(argc - optind, 1))
usage(cmd_qgroup_show_usage);
return 1;
path = argv[optind];
fd = btrfs_open_dir(path, &dirstream, 1);
@ -449,7 +449,7 @@ static int cmd_qgroup_limit(int argc, char **argv)
}
if (check_argc_min(argc - optind, 2))
usage(cmd_qgroup_limit_usage);
return 1;
if (!parse_limit(argv[optind], &size)) {
error("invalid size argument: %s", argv[optind]);

View file

@ -142,7 +142,7 @@ static int cmd_quota_rescan(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_quota_rescan_usage);
return 1;
memset(&args, 0, sizeof(args));

View file

@ -156,7 +156,7 @@ static int cmd_replace_start(int argc, char **argv)
BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_AVOID :
BTRFS_IOCTL_DEV_REPLACE_CONT_READING_FROM_SRCDEV_MODE_ALWAYS;
if (check_argc_exact(argc - optind, 3))
usage(cmd_replace_start_usage);
return 1;
path = argv[optind + 2];
fdmnt = open_path_or_dev_mnt(path, &dirstream, 1);
@ -350,7 +350,7 @@ static int cmd_replace_status(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_replace_status_usage);
return 1;
path = argv[optind];
fd = btrfs_open_dir(path, &dirstream, 1);
@ -516,7 +516,7 @@ static int cmd_replace_cancel(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_replace_cancel_usage);
return 1;
path = argv[optind];
fd = btrfs_open_dir(path, &dirstream, 1);

View file

@ -69,7 +69,7 @@ static int cmd_rescue_chunk_recover(int argc, char *argv[])
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_rescue_chunk_recover_usage);
return 1;
file = argv[optind];
@ -137,7 +137,7 @@ static int cmd_rescue_super_recover(int argc, char **argv)
}
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_rescue_super_recover_usage);
return 1;
dname = argv[optind];
ret = check_mounted(dname);
@ -171,7 +171,7 @@ static int cmd_rescue_zero_log(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_rescue_zero_log_usage);
if (check_argc_exact(argc, 2))
usage(cmd_rescue_zero_log_usage);
return 1;
devname = argv[optind];
ret = check_mounted(devname);
@ -223,7 +223,7 @@ static int cmd_rescue_fix_device_size(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_rescue_fix_device_size_usage);
if (check_argc_exact(argc, 2))
usage(cmd_rescue_fix_device_size_usage);
return 1;
devname = argv[optind];
ret = check_mounted(devname);

View file

@ -1180,10 +1180,8 @@ static int scrub_start(int argc, char **argv, int resume)
/* try to catch most error cases before forking */
if (check_argc_exact(argc - optind, 1)) {
usage(resume ? cmd_scrub_resume_usage :
cmd_scrub_start_usage);
}
if (check_argc_exact(argc - optind, 1))
return 1;
spc.progress = NULL;
if (do_quiet && do_print)
@ -1606,7 +1604,7 @@ static int cmd_scrub_cancel(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_scrub_cancel_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_scrub_cancel_usage);
return 1;
path = argv[optind];
@ -1701,7 +1699,7 @@ static int cmd_scrub_status(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_scrub_status_usage);
return 1;
path = argv[optind];

View file

@ -608,7 +608,7 @@ int cmd_send(int argc, char **argv)
}
if (check_argc_min(argc - optind, 1))
usage(cmd_send_usage);
return 1;
if (outname[0]) {
int tmpfd;

View file

@ -128,8 +128,10 @@ static int cmd_subvol_create(int argc, char **argv)
}
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_subvol_create_usage);
if (check_argc_exact(argc - optind, 1)) {
retval = 1;
goto out;
}
dst = argv[optind];
@ -280,7 +282,7 @@ static int cmd_subvol_delete(int argc, char **argv)
}
if (check_argc_min(argc - optind, 1))
usage(cmd_subvol_delete_usage);
return 1;
if (verbose > 0) {
printf("Transaction commit: %s\n",
@ -561,10 +563,8 @@ static int cmd_subvol_list(int argc, char **argv)
}
}
if (check_argc_exact(argc - optind, 1)) {
uerr = 1;
if (check_argc_exact(argc - optind, 1))
goto out;
}
subvol = argv[optind];
fd = btrfs_open_dir(subvol, &dirstream, 1);
@ -676,8 +676,10 @@ static int cmd_subvol_snapshot(int argc, char **argv)
}
}
if (check_argc_exact(argc - optind, 2))
usage(cmd_subvol_snapshot_usage);
if (check_argc_exact(argc - optind, 2)) {
retval = 1;
goto out;
}
subvol = argv[optind];
dst = argv[optind + 1];
@ -785,7 +787,7 @@ static int cmd_subvol_get_default(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_subvol_get_default_usage);
if (check_argc_exact(argc - optind, 1))
usage(cmd_subvol_get_default_usage);
return 1;
fd = btrfs_open_dir(argv[1], &dirstream, 1);
if (fd < 0)
@ -846,7 +848,7 @@ static int cmd_subvol_set_default(int argc, char **argv)
if (check_argc_min(argc - optind, 1) ||
check_argc_max(argc - optind, 2))
usage(cmd_subvol_set_default_usage);
return 1;
if (argc - optind == 1) {
/* path to the subvolume is specified */
@ -884,7 +886,7 @@ static int cmd_subvol_find_new(int argc, char **argv)
clean_args_no_options(argc, argv, cmd_subvol_find_new_usage);
if (check_argc_exact(argc - optind, 2))
usage(cmd_subvol_find_new_usage);
return 1;
subvol = argv[optind];
last_gen = arg_strtou64(argv[optind + 1]);
@ -967,7 +969,7 @@ static int cmd_subvol_show(int argc, char **argv)
}
if (check_argc_exact(argc - optind, 1))
usage(cmd_subvol_show_usage);
return 1;
if (by_rootid && by_uuid) {
error(
@ -1161,7 +1163,7 @@ static int cmd_subvol_sync(int argc, char **argv)
}
if (check_argc_min(argc - optind, 1))
usage(cmd_subvol_sync_usage);
return 1;
fd = btrfs_open_dir(argv[optind], &dirstream, 1);
if (fd < 0) {