Use getopt_long's flag field to distinguish short and long options where needed and name both variants if this is not sufficient.
Signed-off-by: Stefan Tauner stefan.tauner@student.tuwien.ac.at --- the const bits are not really needed, but at least they make more sense than the static modifier i hope :) option_index is used to index the long_options array hence it has to be guaranteed to be in its range. getopt_long guarantees this in the case opt == 0 afaics so we are safe.
cli_classic.c | 28 +++++++++++++++++++--------- 1 files changed, 19 insertions(+), 9 deletions(-)
diff --git a/cli_classic.c b/cli_classic.c index da658a6..f864b05 100644 --- a/cli_classic.c +++ b/cli_classic.c @@ -177,13 +177,15 @@ int main(int argc, char *argv[]) enum programmer prog = PROGRAMMER_INVALID; int ret = 0;
- static const char optstring[] = "r:Rw:v:nVEfc:m:l:i:p:Lzh"; - static const struct option long_options[] = { + const char const optstring[] = "r:Rw:v:nVEfc:m:l:i:p:Lzh"; + int long_verify = -1; + int long_noverify = -1; + const struct option const long_options[] = { {"read", 1, NULL, 'r'}, {"write", 1, NULL, 'w'}, {"erase", 0, NULL, 'E'}, - {"verify", 1, NULL, 'v'}, - {"noverify", 0, NULL, 'n'}, + {"verify", 1, &long_verify, 'v'}, + {"noverify", 0, &long_noverify, 'n'}, {"chip", 1, NULL, 'c'}, {"verbose", 0, NULL, 'V'}, {"force", 0, NULL, 'f'}, @@ -214,6 +216,10 @@ int main(int argc, char *argv[]) */ while ((opt = getopt_long(argc, argv, optstring, long_options, &option_index)) != EOF) { + if (opt == 0) { + opt = long_options[option_index].val; + } + switch (opt) { case 'r': if (++operation_specified > 1) { @@ -241,8 +247,10 @@ int main(int argc, char *argv[]) cli_classic_abort_usage(); } if (dont_verify_it) { - fprintf(stderr, "--verify and --noverify are" - "mutually exclusive. Aborting.\n"); + fprintf(stderr, "%s and %s are " + "mutually exclusive. Aborting.\n", + long_verify != -1 ? "--verify" : "-v", + long_noverify != -1? "--noverify" : "-n"); cli_classic_abort_usage(); } filename = strdup(optarg); @@ -250,8 +258,10 @@ int main(int argc, char *argv[]) break; case 'n': if (verify_it) { - fprintf(stderr, "--verify and --noverify are" - "mutually exclusive. Aborting.\n"); + fprintf(stderr, "%s and %s are " + "mutually exclusive. Aborting.\n", + long_verify != -1 ? "--verify" : "-v", + long_noverify != -1? "--noverify" : "-n"); cli_classic_abort_usage(); } dont_verify_it = 1; @@ -310,7 +320,7 @@ int main(int argc, char *argv[]) break; case 'p': if (prog != PROGRAMMER_INVALID) { - fprintf(stderr, "Error: --programmer specified " + fprintf(stderr, "Error: --programmer/-p specified " "more than once. You can separate " "multiple\nparameters for a programmer " "with ",". Please see the man page "