From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mails.dpdk.org (mails.dpdk.org [217.70.189.124]) by inbox.dpdk.org (Postfix) with ESMTP id AA011A0A0E; Thu, 4 Feb 2021 08:35:25 +0100 (CET) Received: from [217.70.189.124] (localhost [127.0.0.1]) by mails.dpdk.org (Postfix) with ESMTP id EE0AC2405C1; Thu, 4 Feb 2021 08:35:04 +0100 (CET) Received: from mail-wm1-f42.google.com (mail-wm1-f42.google.com [209.85.128.42]) by mails.dpdk.org (Postfix) with ESMTP id A8E0324059F for ; Thu, 4 Feb 2021 08:35:03 +0100 (CET) Received: by mail-wm1-f42.google.com with SMTP id l12so2205827wmq.2 for ; Wed, 03 Feb 2021 23:35:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=emumba-com.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references; bh=fYgUCFObKmBMlEz3xcUZhSHwLZmaVrRvR10lapU6ZHM=; b=QCCcSLiCqIS6JSrAGAdZf/2vKf4mgG/jxEnKBKtzezXQb1o/ct6dh9WuklmPXxuwqJ XlbtVgeHiwMRSeeTRxdjTP0+HGxLPxCBsy/axRjIE4+TK93kHka4dzIHipKx4iCTn2Pr GCn7V7ckYCY2VooCNHSFtjHYrpkeTNuxZnthQOdDqNJXZH+0qVuuOGt9CXqIlgl1wKba 8c2+DmkmlZvi73eui3Kln12Rv1Ew1jDqPHZuDhEXkbWpQS5BKwir9aWtjkoTXsHgubO+ aJwujav59KmWR/Tk3qyUSsoEv3kBbo5df1rDsTlKMJFhnJeq+nnN1R/kU619vS6HBWKw IlVg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references; bh=fYgUCFObKmBMlEz3xcUZhSHwLZmaVrRvR10lapU6ZHM=; b=HZKl8G1Wgn4ISuB/SrWyq9B4wPBEM3mOh2zGuciDBeESaL0YM/BcQAoUv2IkThvIQJ 7TRrTN4wYpqoKuHm+6ZpGHqY1pDGNkUaTCqb4e8D6c/AsVcjiLTLHBrWvUKgQmLg+3Zc aKSb3oPT7E0bxhYRoBsFhrGk+/1XdmTbRdyHIzK1m484VVZad8CK0yKlquQEKuqxB0hm CNljjtw75OM8amvrY3/myN3Fv+HEB+pitbNIMqL/vZ2Sh1xRTaVvybsWSXGaLaOHlbZx Xk+K+8dHaFXFhWEnmlVIcSo3LRNwpFsydMY4/PMEmUD/pDyKPNNZeGsXI85GaeNPmowX Kmvg== X-Gm-Message-State: AOAM530eWbveScnC1CIAugIskJX0bA25lq+K6W29ZRz1qmra2y/GQyqL qeT/z4tDaW5MU5LjJWjDEVHIDg== X-Google-Smtp-Source: ABdhPJz2t/9H/1M31YrRU4zprNhssFIly32KqPeksXEujjnXZv2Ztx9MVo9W1pBfB1g7Ewdmm0HDfQ== X-Received: by 2002:a05:600c:3545:: with SMTP id i5mr6286822wmq.134.1612424103376; Wed, 03 Feb 2021 23:35:03 -0800 (PST) Received: from localhost.localdomain ([39.33.143.135]) by smtp.gmail.com with ESMTPSA id j11sm7082604wrt.26.2021.02.03.23.35.00 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Feb 2021 23:35:02 -0800 (PST) From: Ibtisam Tariq To: maxime.coquelin@redhat.com, chenbo.xia@intel.com, cristian.dumitrescu@intel.com, jasvinder.singh@intel.com, john.mcnamara@intel.com, reshma.pattan@intel.com, konstantin.ananyev@intel.com, marko.kovacevic@intel.com Cc: dev@dpdk.org, Ibtisam Tariq , roy.fan.zhang@intel.com Date: Thu, 4 Feb 2021 07:34:20 +0000 Message-Id: <20210204073420.2421-7-ibtisam.tariq@emumba.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20210204073420.2421-1-ibtisam.tariq@emumba.com> References: <20210204073420.2421-1-ibtisam.tariq@emumba.com> Subject: [dpdk-dev] [PATCH v4 7/7] examples/vhost_crypto: enhance getopt_long usage X-BeenThere: dev@dpdk.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: DPDK patches and discussions List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: dev-bounces@dpdk.org Sender: "dev" Instead of using getopt_long return value, strcmp was used to compare the input parameters with the struct option array. This patch get rid of all those strcmp by directly binding each longopt with an int enum. This is to improve readability and consistency in all examples. Bugzilla ID: 238 Cc: roy.fan.zhang@intel.com Reported-by: David Marchand Signed-off-by: Ibtisam Tariq --- v4: * Set indentation of preprocessor directives. v3: * None. v2: * Remove extra indentations. * Remove extra block brackets in switch statement. * Change enum names to start with OPT_ and remove KEYWORD from enum names. * Remove unused short options. v1: * enhance getopt_long usage. --- examples/vhost_crypto/main.c | 86 +++++++++++++++++++++--------------- 1 file changed, 50 insertions(+), 36 deletions(-) diff --git a/examples/vhost_crypto/main.c b/examples/vhost_crypto/main.c index 29c8f7228..efae99781 100644 --- a/examples/vhost_crypto/main.c +++ b/examples/vhost_crypto/main.c @@ -62,10 +62,16 @@ struct vhost_crypto_options { uint32_t guest_polling; } options; -#define CONFIG_KEYWORD "config" -#define SOCKET_FILE_KEYWORD "socket-file" -#define ZERO_COPY_KEYWORD "zero-copy" -#define POLLING_KEYWORD "guest-polling" +enum { +#define OPT_CONFIG "config" + OPT_CONFIG_NUM = 256, +#define OPT_SOCKET_FILE "socket-file" + OPT_SOCKET_FILE_NUM, +#define OPT_ZERO_COPY "zero-copy" + OPT_ZERO_COPY_NUM, +#define OPT_POLLING "guest-polling" + OPT_POLLING_NUM, +}; #define NB_SOCKET_FIELDS (2) @@ -198,8 +204,8 @@ vhost_crypto_usage(const char *prgname) " --%s (lcore,cdev_id,queue_id)[,(lcore,cdev_id,queue_id)]\n" " --%s: zero copy\n" " --%s: guest polling\n", - prgname, SOCKET_FILE_KEYWORD, CONFIG_KEYWORD, - ZERO_COPY_KEYWORD, POLLING_KEYWORD); + prgname, OPT_SOCKET_FILE, OPT_CONFIG, + OPT_ZERO_COPY, OPT_POLLING); } static int @@ -210,11 +216,15 @@ vhost_crypto_parse_args(int argc, char **argv) char **argvopt; int option_index; struct option lgopts[] = { - {SOCKET_FILE_KEYWORD, required_argument, 0, 0}, - {CONFIG_KEYWORD, required_argument, 0, 0}, - {ZERO_COPY_KEYWORD, no_argument, 0, 0}, - {POLLING_KEYWORD, no_argument, 0, 0}, - {NULL, 0, 0, 0} + {OPT_SOCKET_FILE, required_argument, + NULL, OPT_SOCKET_FILE_NUM}, + {OPT_CONFIG, required_argument, + NULL, OPT_CONFIG_NUM}, + {OPT_ZERO_COPY, no_argument, + NULL, OPT_ZERO_COPY_NUM}, + {OPT_POLLING, no_argument, + NULL, OPT_POLLING_NUM}, + {NULL, 0, 0, 0} }; argvopt = argv; @@ -222,36 +232,40 @@ vhost_crypto_parse_args(int argc, char **argv) while ((opt = getopt_long(argc, argvopt, "s:", lgopts, &option_index)) != EOF) { + if (opt == '?') { + vhost_crypto_usage(prgname); + return -1; + } + switch (opt) { - case 0: - if (strcmp(lgopts[option_index].name, - SOCKET_FILE_KEYWORD) == 0) { - ret = parse_socket_arg(optarg); - if (ret < 0) { - vhost_crypto_usage(prgname); - return ret; - } - } else if (strcmp(lgopts[option_index].name, - CONFIG_KEYWORD) == 0) { - ret = parse_config(optarg); - if (ret < 0) { - vhost_crypto_usage(prgname); - return ret; - } - } else if (strcmp(lgopts[option_index].name, - ZERO_COPY_KEYWORD) == 0) { - options.zero_copy = - RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE; - } else if (strcmp(lgopts[option_index].name, - POLLING_KEYWORD) == 0) { - options.guest_polling = 1; - } else { + case OPT_SOCKET_FILE_NUM: + ret = parse_socket_arg(optarg); + if (ret < 0) { vhost_crypto_usage(prgname); - return -EINVAL; + return ret; + } + break; + + case OPT_CONFIG_NUM: + ret = parse_config(optarg); + if (ret < 0) { + vhost_crypto_usage(prgname); + return ret; } break; + + case OPT_ZERO_COPY_NUM: + options.zero_copy = + RTE_VHOST_CRYPTO_ZERO_COPY_ENABLE; + break; + + case OPT_POLLING_NUM: + options.guest_polling = 1; + break; + default: - return -1; + vhost_crypto_usage(prgname); + return -EINVAL; } } -- 2.17.1