diff mbox series

[v4,7/7] examples/vhost_crypto: enhance getopt_long usage

Message ID 20210204073420.2421-7-ibtisam.tariq@emumba.com (mailing list archive)
State Accepted
Delegated to: David Marchand
Headers show
Series [v4,1/7] examples/fips_validation: enhance getopt_long usage | expand

Checks

Context Check Description
ci/iol-testing warning Testing issues
ci/travis-robot warning Travis build: failed
ci/intel-Testing success Testing PASS
ci/iol-broadcom-Functional success Functional Testing PASS
ci/Intel-compilation success Compilation OK
ci/iol-broadcom-Performance success Performance Testing PASS
ci/checkpatch success coding style OK

Commit Message

Ibtisam Tariq Feb. 4, 2021, 7:34 a.m. UTC
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 <david.marchand@redhat.com>
Signed-off-by: Ibtisam Tariq <ibtisam.tariq@emumba.com>
---
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(-)

Comments

Fan Zhang Feb. 5, 2021, 4:21 p.m. UTC | #1
> -----Original Message-----
> From: Ibtisam Tariq <ibtisam.tariq@emumba.com>
> Sent: Thursday, February 4, 2021 7:34 AM
> To: maxime.coquelin@redhat.com; Xia, Chenbo <chenbo.xia@intel.com>;
> Dumitrescu, Cristian <cristian.dumitrescu@intel.com>; Singh, Jasvinder
> <jasvinder.singh@intel.com>; Mcnamara, John
> <john.mcnamara@intel.com>; Pattan, Reshma <reshma.pattan@intel.com>;
> Ananyev, Konstantin <konstantin.ananyev@intel.com>; Kovacevic, Marko
> <marko.kovacevic@intel.com>
> Cc: dev@dpdk.org; Ibtisam Tariq <ibtisam.tariq@emumba.com>; Zhang, Roy
> Fan <roy.fan.zhang@intel.com>
> Subject: [PATCH v4 7/7] examples/vhost_crypto: enhance getopt_long usage
> 
> 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 <david.marchand@redhat.com>
> Signed-off-by: Ibtisam Tariq <ibtisam.tariq@emumba.com>

Acked-by: Fan Zhang <roy.fan.zhang@intel.com>

Thank you very much!
Maxime Coquelin March 23, 2021, 11:27 a.m. UTC | #2
On 2/4/21 8:34 AM, Ibtisam Tariq wrote:
> 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 <david.marchand@redhat.com>
> Signed-off-by: Ibtisam Tariq <ibtisam.tariq@emumba.com>
> ---
> 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(-)

Reviewed-by: Maxime Coquelin <maxime.coquelin@redhat.com>

Thanks,
Maxime
diff mbox series

Patch

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;
 		}
 	}