diff --git a/tools/fiptool/fiptool.c b/tools/fiptool/fiptool.c index f53e9ab3b21464f5b41acaa6c5fb1b10973dc3a2..70ec6fd786572c799e49311a8e487945ec9c17b1 100644 --- a/tools/fiptool/fiptool.c +++ b/tools/fiptool/fiptool.c @@ -977,17 +977,36 @@ int main(int argc, char *argv[]) { 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) {