From edb45ae46449ddabcac3dea6b3f42c6cec01510a Mon Sep 17 00:00:00 2001 From: Timo Teräs Date: Mon, 24 Aug 2020 13:35:36 +0300 Subject: enforce options definitions to bind the enum and the descriptor This uses some macro trickery to make sure that there's one-to-one mapping with the option index enum and the descriptor. The down side is that enum's are generated via #define's and editors might not pick them up for auto completion, but the benefits are more: it's no longer possible have mismatching enum value and descriptor index, and the amount of source code lines is less. --- src/app_search.c | 26 +++++++++----------------- 1 file changed, 9 insertions(+), 17 deletions(-) (limited to 'src/app_search.c') diff --git a/src/app_search.c b/src/app_search.c index 5f6f431..866bb7a 100644 --- a/src/app_search.c +++ b/src/app_search.c @@ -72,23 +72,15 @@ static void print_rdepends(struct search_ctx *ctx, struct apk_package *pkg) apk_pkg_foreach_reverse_dependency(pkg, ctx->matches, print_rdep_pkg, ctx); } -enum { - OPT_SEARCH_all, - OPT_SEARCH_description, - OPT_SEARCH_exact, - OPT_SEARCH_has_origin, - OPT_SEARCH_origin, - OPT_SEARCH_rdepends, -}; - -static const char option_desc[] = - APK_OPTAPPLET - APK_OPT2n("all", "a") - APK_OPT2n("description", "d") - APK_OPT2n("exact", "\xf2""ex") - APK_OPT1n("has-origin") - APK_OPT2n("origin", "o") - APK_OPT2n("rdepends", "r"); +#define SEARCH_OPTIONS(OPT) \ + OPT(OPT_SEARCH_all, APK_OPT_SH("a") "all") \ + OPT(OPT_SEARCH_description, APK_OPT_SH("d") "description") \ + OPT(OPT_SEARCH_exact, APK_OPT_S2("ex") "exact") \ + OPT(OPT_SEARCH_has_origin, "has-origin") \ + OPT(OPT_SEARCH_origin, APK_OPT_SH("o") "origin") \ + OPT(OPT_SEARCH_rdepends, APK_OPT_SH("r") "rdepends") \ + +APK_OPT_APPLET(option_desc, SEARCH_OPTIONS); static int option_parse_applet(void *ctx, struct apk_db_options *dbopts, int opt, const char *optarg) { -- cgit v1.2.3-60-g2f50