fiptool: Use getopt for the top level command parsing

Change-Id: I18a4327e41fc090dcea9a647f7673182ca0ed1d9
Signed-off-by: dp-arm <dimitris.papastamos@arm.com>
diff --git a/tools/fiptool/fiptool.c b/tools/fiptool/fiptool.c
index f53e9ab..70ec6fd 100644
--- a/tools/fiptool/fiptool.c
+++ b/tools/fiptool/fiptool.c
@@ -977,17 +977,36 @@
 {
 	int i, ret = 0;
 
-	if (argc < 2)
-		usage();
-	argc--, argv++;
+	while (1) {
+		int c, opt_index = 0;
+		static struct option opts[] = {
+			{ "verbose", no_argument, NULL, 'v' },
+			{ NULL, no_argument, NULL, 0 }
+		};
+
+		/*
+		 * Set POSIX mode so getopt stops at the first non-option
+		 * which is the subcommand.
+		 */
+		c = getopt_long(argc, argv, "+v", opts, &opt_index);
+		if (c == -1)
+			break;
 
-	if (strcmp(argv[0], "-v") == 0 ||
-	    strcmp(argv[0], "--verbose") == 0) {
-		verbose = 1;
-		argc--, argv++;
-		if (argc < 1)
+		switch (c) {
+		case 'v':
+			verbose = 1;
+			break;
+		default:
 			usage();
+		}
 	}
+	argc -= optind;
+	argv += optind;
+	/* Reset optind for subsequent getopt processing. */
+	optind = 0;
+
+	if (argc == 0)
+		usage();
 
 	for (i = 0; i < NELEM(cmds); i++) {
 		if (strcmp(cmds[i].name, argv[0]) == 0) {