summaryrefslogtreecommitdiff
path: root/cmds-check.c
diff options
context:
space:
mode:
authorHugo Mills <hugo@carfax.org.uk>2015-01-27 15:05:52 +0000
committerDavid Sterba <dsterba@suse.cz>2015-01-27 16:41:06 +0100
commit7c126f7afc33d0aca7fc3053c7f1348df6feb8c8 (patch)
treee7e1a3007b6ae34612e4e2fe051289903c3ddee0 /cmds-check.c
parent1c93eb00891ecd2d012cec6bb5f592bae3566599 (diff)
btrfs-progs: Make option parsing more robust to code modifications
The current approach to option parsing, where long-only options are selected on the basis of their position in the long_options array is fragile and painful to modify if options are to be inserted into the list, rather than appended. Instead, use the last field of struct option to return a value which cannot be a char (and hence a short option), and simply switch on those within the case statement. Signed-off-by: Hugo Mills <hugo@carfax.org.uk> Signed-off-by: David Sterba <dsterba@suse.cz>
Diffstat (limited to 'cmds-check.c')
-rw-r--r--cmds-check.c49
1 files changed, 27 insertions, 22 deletions
diff --git a/cmds-check.c b/cmds-check.c
index 8000f15b..c24e4e04 100644
--- a/cmds-check.c
+++ b/cmds-check.c
@@ -8435,13 +8435,15 @@ int cmd_check(int argc, char **argv)
while(1) {
int c;
int option_index = 0;
+ enum { OPT_REPAIR = 257, OPT_INIT_CSUM, OPT_INIT_EXTENT,
+ OPT_CHECK_CSUM };
static const struct option long_options[] = {
{ "super", 1, NULL, 's' },
- { "repair", 0, NULL, 0 },
- { "init-csum-tree", 0, NULL, 0 },
- { "init-extent-tree", 0, NULL, 0 },
- { "check-data-csum", 0, NULL, 0 },
- { "backup", 0, NULL, 0 },
+ { "repair", 0, NULL, OPT_REPAIR },
+ { "init-csum-tree", 0, NULL, OPT_INIT_CSUM },
+ { "init-extent-tree", 0, NULL, OPT_INIT_EXTENT },
+ { "check-data-csum", 0, NULL, OPT_CHECK_CSUM },
+ { "backup", 0, NULL, 'b' },
{ "subvol-extents", 1, NULL, 'E' },
{ "qgroup-report", 0, NULL, 'Q' },
{ "tree-root", 1, NULL, 'r' },
@@ -8481,23 +8483,26 @@ int cmd_check(int argc, char **argv)
case '?':
case 'h':
usage(cmd_check_usage);
- }
- if (option_index == 1) {
- printf("enabling repair mode\n");
- repair = 1;
- ctree_flags |= OPEN_CTREE_WRITES;
- } else if (option_index == 2) {
- printf("Creating a new CRC tree\n");
- init_csum_tree = 1;
- repair = 1;
- ctree_flags |= OPEN_CTREE_WRITES;
- } else if (option_index == 3) {
- init_extent_tree = 1;
- ctree_flags |= (OPEN_CTREE_WRITES |
- OPEN_CTREE_NO_BLOCK_GROUPS);
- repair = 1;
- } else if (option_index == 4) {
- check_data_csum = 1;
+ case OPT_REPAIR:
+ printf("enabling repair mode\n");
+ repair = 1;
+ ctree_flags |= OPEN_CTREE_WRITES;
+ break;
+ case OPT_INIT_CSUM:
+ printf("Creating a new CRC tree\n");
+ init_csum_tree = 1;
+ repair = 1;
+ ctree_flags |= OPEN_CTREE_WRITES;
+ break;
+ case OPT_INIT_EXTENT:
+ init_extent_tree = 1;
+ ctree_flags |= (OPEN_CTREE_WRITES |
+ OPEN_CTREE_NO_BLOCK_GROUPS);
+ repair = 1;
+ break;
+ case OPT_CHECK_CSUM:
+ check_data_csum = 1;
+ break;
}
}
argc = argc - optind;