From 2ca50624f7d4461cc079ab6dfe520f10e99a72d9 Mon Sep 17 00:00:00 2001 From: Martin Mares Date: Mon, 17 Feb 2014 18:49:48 +0100 Subject: [PATCH] Opt: "--help" can stand both before and after config options The OPT_BEFORE_CONFIG flag was incorrectly interpreted as "this is a config option", so "--help" after all options did not work as expected. Now, OPT_BEFORE_CONFIG means "the option can stand anywhere and the config check logic should not touch it". --- ucw/opt-conf.c | 40 ++++++++++++++++++++++++---------------- 1 file changed, 24 insertions(+), 16 deletions(-) diff --git a/ucw/opt-conf.c b/ucw/opt-conf.c index 25cee0ee..43c60caf 100644 --- a/ucw/opt-conf.c +++ b/ucw/opt-conf.c @@ -24,14 +24,32 @@ static void opt_conf_end_of_options(struct cf_context *cc) { opt_failure("Loading of configuration failed"); } -void opt_handle_config(struct opt_item * opt UNUSED, const char * value, void * data UNUSED) +static void opt_conf_check(struct opt_context *oc) { + switch (oc->conf_state) { + case OPT_CONF_HOOK_BEGIN: + oc->conf_state = OPT_CONF_HOOK_CONFIG; + break; + case OPT_CONF_HOOK_CONFIG: + break; + case OPT_CONF_HOOK_OTHERS: + opt_failure("Config options must stand before other options."); + break; + default: + ASSERT(0); + } +} + +void opt_handle_config(struct opt_item * opt UNUSED, const char * value, void * data) +{ + opt_conf_check(data); if (cf_load(value)) exit(1); // Error message is already printed by cf_load() } -void opt_handle_set(struct opt_item * opt UNUSED, const char * value, void * data UNUSED) +void opt_handle_set(struct opt_item * opt UNUSED, const char * value, void * data) { + opt_conf_check(data); struct cf_context *cc = cf_get_context(); cf_load_default(cc); if (cf_set(value)) @@ -59,26 +77,16 @@ void opt_conf_hook_internal(struct opt_item * opt, uns event, const char * value ASSERT(event == OPT_HOOK_BEFORE_VALUE); - bool confopt = opt->flags & OPT_BEFORE_CONFIG; + if (opt->flags & OPT_BEFORE_CONFIG) + return; switch (oc->conf_state) { case OPT_CONF_HOOK_BEGIN: - if (confopt) - oc->conf_state = OPT_CONF_HOOK_CONFIG; - else { - opt_conf_end_of_options(cc); - oc->conf_state = OPT_CONF_HOOK_OTHERS; - } - break; case OPT_CONF_HOOK_CONFIG: - if (!confopt) { - opt_conf_end_of_options(cc); - oc->conf_state = OPT_CONF_HOOK_OTHERS; - } + opt_conf_end_of_options(cc); + oc->conf_state = OPT_CONF_HOOK_OTHERS; break; case OPT_CONF_HOOK_OTHERS: - if (confopt) - opt_failure("Config options must stand before other options."); break; default: ASSERT(0); -- 2.39.2