[PATCH 1/2] samples: timers: hpet_example: Improve argument parsing

Akhilesh Patil posted 2 patches 1 month, 2 weeks ago
[PATCH 1/2] samples: timers: hpet_example: Improve argument parsing
Posted by Akhilesh Patil 1 month, 2 weeks ago
Improve user experience by adding helper text while argument parsing.
Print format of the command line arguments for the program along
with example invocation and supported commands.
Define and use ARRAY_SIZE() macro to iterate over command array to
improve readability of the code.

Print as below upon incorrect invocation:
$ ./hpet_example

-hpet: requires command
Format: hpet_example <command> </dev/device_name>
Supported commands:
        open-close
        info
        poll
        fasync

Example: $ ./hpet_example info /dev/hpet

Signed-off-by: Akhilesh Patil <akhilesh@ee.iitb.ac.in>
---
 samples/timers/hpet_example.c | 11 +++++++++--
 1 file changed, 9 insertions(+), 2 deletions(-)

diff --git a/samples/timers/hpet_example.c b/samples/timers/hpet_example.c
index f1cb622f6ec0..59474c7be576 100644
--- a/samples/timers/hpet_example.c
+++ b/samples/timers/hpet_example.c
@@ -25,6 +25,8 @@ extern void hpet_read(int, const char **);
 #include <sys/poll.h>
 #include <sys/ioctl.h>
 
+#define ARRAY_SIZE(x)	(sizeof(x) / sizeof(x[0]))
+
 struct hpet_command {
 	char		*command;
 	void		(*func)(int argc, const char ** argv);
@@ -56,12 +58,17 @@ main(int argc, const char ** argv)
 	argv++;
 
 	if (!argc) {
-		fprintf(stderr, "-hpet: requires command\n");
+		fprintf(stderr, "-hpet: requires command\n"
+			"Format: hpet_example <command> </dev/device_name>\n");
+		fprintf(stderr, "Supported commands:\n");
+		for (i = 0; i < ARRAY_SIZE(hpet_command); i++)
+			fprintf(stderr, "\t%s\n", hpet_command[i].command);
+		fprintf(stderr, "\nExample: $ ./hpet_example info /dev/hpet\n");
 		return -1;
 	}
 
 
-	for (i = 0; i < (sizeof (hpet_command) / sizeof (hpet_command[0])); i++)
+	for (i = 0; i < ARRAY_SIZE(hpet_command); i++)
 		if (!strcmp(argv[0], hpet_command[i].command)) {
 			argc--;
 			argv++;
-- 
2.34.1