From 1ebe3d6733c34a1cb2ec16d2be246d773890932f Mon Sep 17 00:00:00 2001 From: lifang zhang Date: Wed, 17 Aug 2022 17:20:56 +0300 Subject: [PATCH 1/2] Remove unnecessary else Some of the style check complains "WARNING:UNNECESSARY_ELSE: else is not generally useful after a break or return". --- optparse.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/optparse.h b/optparse.h index 8d6c0a9..1c5700c 100644 --- a/optparse.h +++ b/optparse.h @@ -390,8 +390,8 @@ optparse_long(struct optparse *options, options->optarg = options->argv[options->optind]; if (options->optarg == 0) return optparse_error(options, OPTPARSE_MSG_MISSING, name); - else - options->optind++; + + options->optind++; } return options->optopt; } From 18194a8f5fcb475cfc354e50765bd4988d62676c Mon Sep 17 00:00:00 2001 From: lifang zhang Date: Wed, 17 Aug 2022 17:29:36 +0300 Subject: [PATCH 2/2] Return '?' for unkown error --- optparse.h | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/optparse.h b/optparse.h index 1c5700c..6e02a73 100644 --- a/optparse.h +++ b/optparse.h @@ -120,6 +120,7 @@ char *optparse_arg(struct optparse *options); #define OPTPARSE_MSG_INVALID "invalid option" #define OPTPARSE_MSG_MISSING "option requires an argument" #define OPTPARSE_MSG_TOOMANY "option takes no arguments" +#define OPTPARSE_MSG_UNKNOWN "unknown error" static int optparse_error(struct optparse *options, const char *msg, const char *data) @@ -259,8 +260,12 @@ optparse(struct optparse *options, const char *optstring) else options->optarg = 0; return option[0]; + default: { + char str[2] = {0, 0}; + str[0] = option[0]; + return optparse_error(options, OPTPARSE_MSG_UNKNOWN, str); + } } - return 0; } OPTPARSE_API