DPDK patches and discussions
 help / color / mirror / Atom feed
From: Ibtisam Tariq <ibtisam.tariq@emumba.com>
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 <ibtisam.tariq@emumba.com>,
	roy.fan.zhang@intel.com
Subject: [dpdk-dev] [PATCH 7/7] examples/vhost_crypto: enhance getopt_long usage
Date: Tue, 24 Nov 2020 12:32:49 +0000
Message-ID: <20201124123249.14451-7-ibtisam.tariq@emumba.com> (raw)
In-Reply-To: <20201124123249.14451-1-ibtisam.tariq@emumba.com>

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>
---
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 | 90 +++++++++++++++++++++---------------
 1 file changed, 52 insertions(+), 38 deletions(-)

diff --git a/examples/vhost_crypto/main.c b/examples/vhost_crypto/main.c
index 29c8f7228..d731751eb 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)
 
@@ -195,11 +201,11 @@ vhost_crypto_usage(const char *prgname)
 {
 	printf("%s [EAL options] --\n"
 		"  --%s <lcore>,SOCKET-FILE-PATH\n"
-		"  --%s (lcore,cdev_id,queue_id)[,(lcore,cdev_id,queue_id)]\n"
+		"  --%s (lcore,cdev_id,queue_id)[,(lcore,cdev_id,queue_id)]"
 		"  --%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,48 +216,56 @@ 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;
 
-	while ((opt = getopt_long(argc, argvopt, "s:",
+	while ((opt = getopt_long(argc, argvopt, "",
 				  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


  parent reply	other threads:[~2020-11-24 12:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-11-24 12:32 [dpdk-dev] [PATCH 1/7] examples/fips_validation: " Ibtisam Tariq
2020-11-24 12:32 ` [dpdk-dev] [PATCH 2/7] examples/l3fwd-acl: " Ibtisam Tariq
2020-11-24 12:32 ` [dpdk-dev] [PATCH 3/7] examples/packet_ordering: " Ibtisam Tariq
2020-11-24 12:32 ` [dpdk-dev] [PATCH 4/7] examples/performance-thread/l3fwd-thread: " Ibtisam Tariq
2020-11-24 12:32 ` [dpdk-dev] [PATCH 5/7] examples/qos_sched: " Ibtisam Tariq
2020-11-24 12:32 ` [dpdk-dev] [PATCH 6/7] examples/vhost: " Ibtisam Tariq
2020-12-07 10:51   ` David Marchand
2020-12-31  7:00     ` Ibtisam Tariq
2020-11-24 12:32 ` Ibtisam Tariq [this message]
2020-12-07 10:50 ` [dpdk-dev] [PATCH 1/7] examples/fips_validation: " David Marchand
  -- strict thread matches above, loose matches on Subject: below --
2020-11-11  8:15 Ibtisam Tariq
2020-11-11  8:15 ` [dpdk-dev] [PATCH 7/7] examples/vhost_crypto: " Ibtisam Tariq

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20201124123249.14451-7-ibtisam.tariq@emumba.com \
    --to=ibtisam.tariq@emumba.com \
    --cc=chenbo.xia@intel.com \
    --cc=cristian.dumitrescu@intel.com \
    --cc=dev@dpdk.org \
    --cc=jasvinder.singh@intel.com \
    --cc=john.mcnamara@intel.com \
    --cc=konstantin.ananyev@intel.com \
    --cc=marko.kovacevic@intel.com \
    --cc=maxime.coquelin@redhat.com \
    --cc=reshma.pattan@intel.com \
    --cc=roy.fan.zhang@intel.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link

DPDK patches and discussions

This inbox may be cloned and mirrored by anyone:

	git clone --mirror https://inbox.dpdk.org/dev/0 dev/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 dev dev/ https://inbox.dpdk.org/dev \
		dev@dpdk.org
	public-inbox-index dev

Example config snippet for mirrors.
Newsgroup available over NNTP:
	nntp://inbox.dpdk.org/inbox.dpdk.dev


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git