From patchwork Tue Jan 27 15:05:52 2015 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Hugo Mills X-Patchwork-Id: 5718361 Return-Path: X-Original-To: patchwork-linux-btrfs@patchwork.kernel.org Delivered-To: patchwork-parsemail@patchwork2.web.kernel.org Received: from mail.kernel.org (mail.kernel.org [198.145.29.136]) by patchwork2.web.kernel.org (Postfix) with ESMTP id DBD61C058E for ; Tue, 27 Jan 2015 15:06:12 +0000 (UTC) Received: from mail.kernel.org (localhost [127.0.0.1]) by mail.kernel.org (Postfix) with ESMTP id 067C820218 for ; Tue, 27 Jan 2015 15:06:12 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 1C9CB20225 for ; Tue, 27 Jan 2015 15:06:11 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1758520AbbA0PGE (ORCPT ); Tue, 27 Jan 2015 10:06:04 -0500 Received: from frost.carfax.org.uk ([85.119.82.111]:37924 "EHLO frost.carfax.org.uk" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1753027AbbA0PGD (ORCPT ); Tue, 27 Jan 2015 10:06:03 -0500 Received: from hrm by frost.carfax.org.uk with local (Exim 4.80) (envelope-from ) id 1YG7iK-0004WV-Na; Tue, 27 Jan 2015 15:06:00 +0000 From: Hugo Mills To: linux-btrfs@vger.kernel.org, dsterba@suse.cz Cc: Hugo Mills Subject: [PATCH 1/2] btrfs-progs: Make option parsing more robust to code modifications Date: Tue, 27 Jan 2015 15:05:52 +0000 Message-Id: <1422371153-17355-1-git-send-email-hugo@carfax.org.uk> X-Mailer: git-send-email 1.7.10.4 Sender: linux-btrfs-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-btrfs@vger.kernel.org X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, T_RP_MATCHES_RCVD, UNPARSEABLE_RELAY autolearn=ham version=3.3.1 X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on mail.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP 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 --- cmds-check.c | 49 +++++++++++++++++++++++++++---------------------- 1 file changed, 27 insertions(+), 22 deletions(-) diff --git a/cmds-check.c b/cmds-check.c index f06e029..a1226c6 100644 --- a/cmds-check.c +++ b/cmds-check.c @@ -8403,13 +8403,15 @@ out: return bad_roots; } +enum { OPT_REPAIR = 257, OPT_INIT_CSUM, OPT_INIT_EXTENT, OPT_CHECK_CSUM }; + static 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' }, @@ -8483,23 +8485,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;