diff options
author | Vito Caputo <vcaputo@pengaru.com> | 2023-07-26 19:13:38 -0700 |
---|---|---|
committer | Vito Caputo <vcaputo@pengaru.com> | 2023-07-28 16:34:18 -0700 |
commit | 3e731cc575d3e56af4537a74a2f42bf278ad172f (patch) | |
tree | b2b94d48a8272b621bc472e25316af5697c27d55 | |
parent | e63cf6fa951b77819b846b8e52a5f89c404a2a19 (diff) |
til_settings: filter til_settings_as_arg() results
Most of the time if there are undescribed settings in the
settings hierarchy, they're just noise in the serialized as_arg
form.
So change til_settings_as_arg() to always skip undsecribed
entries, and introduce til_settings_as_arg_unfiltered() for any
use cases that actually want everything included. The unfiltered
variant may just go the way of the dodo if nothing ever makes use
of it.
-rw-r--r-- | src/til_settings.c | 30 | ||||
-rw-r--r-- | src/til_settings.h | 1 |
2 files changed, 25 insertions, 6 deletions
diff --git a/src/til_settings.c b/src/til_settings.c index 62f63e1..be8457d 100644 --- a/src/til_settings.c +++ b/src/til_settings.c @@ -600,11 +600,14 @@ static inline void fputs_escaped(til_str_t *out, const char *value, unsigned dep } -static int settings_as_arg(const til_settings_t *settings, unsigned depth, til_str_t *out) +static int settings_as_arg(const til_settings_t *settings, int unfiltered, unsigned depth, til_str_t *out) { - for (size_t i = 0; i < settings->num; i++) { + for (size_t i = 0, j = 0; i < settings->num; i++) { + if (!unfiltered && !settings->entries[i]->desc) + continue; + /* FIXME TODO: detect errors */ - if (i > 0) + if (j > 0) fputc_escaped(out, ',', depth); if (settings->entries[i]->key) { @@ -614,17 +617,18 @@ static int settings_as_arg(const til_settings_t *settings, unsigned depth, til_s } if (settings->entries[i]->value_as_nested_settings) { - settings_as_arg(settings->entries[i]->value_as_nested_settings, depth + 1, out); + settings_as_arg(settings->entries[i]->value_as_nested_settings, unfiltered, depth + 1, out); } else if (settings->entries[i]->value) { fputs_escaped(out, settings->entries[i]->value, depth); } + j++; } return 0; } -char * til_settings_as_arg(const til_settings_t *settings) +static char * _settings_as_arg(const til_settings_t *settings, int unfiltered) { til_str_t *str; @@ -632,13 +636,27 @@ char * til_settings_as_arg(const til_settings_t *settings) if (!str) return NULL; - if (settings_as_arg(settings, 0, str) < 0) + if (settings_as_arg(settings, unfiltered, 0, str) < 0) return til_str_free(str); return til_str_to_buf(str, NULL); } +/* returns the serialized form of settings usable as a cli argument, omitting any undescribed settings */ +char * til_settings_as_arg(const til_settings_t *settings) +{ + return _settings_as_arg(settings, 0); +} + + +/* same as til_settings_as_arg() but including undescribed settings */ +char * til_settings_as_arg_unfiltered(const til_settings_t *settings) +{ + return _settings_as_arg(settings, 1); +} + + /* generate a positional label for a given setting, stored @ res_label. * this is added specifically for labeling bare-value settings in an array subscript fashion... */ diff --git a/src/til_settings.h b/src/til_settings.h index 77811f1..b025158 100644 --- a/src/til_settings.h +++ b/src/til_settings.h @@ -56,6 +56,7 @@ til_setting_t * til_settings_add_value(til_settings_t *settings, const char *key void til_settings_reset_descs(til_settings_t *settings); int til_settings_get_and_describe_value(const til_settings_t *settings, const til_setting_spec_t *spec, const char **res_value, til_setting_t **res_setting, const til_setting_desc_t **res_desc); char * til_settings_as_arg(const til_settings_t *settings); +char * til_settings_as_arg_unfiltered(const til_settings_t *settings); int til_settings_apply_desc_generators(const til_settings_t *settings, const til_setting_desc_generator_t generators[], unsigned n_generators, til_setup_t *setup, til_setting_t **res_setting, const til_setting_desc_t **res_desc, til_setup_t **res_setup); int til_setting_desc_new(const til_settings_t *settings, const til_setting_spec_t *spec, const til_setting_desc_t **res_desc); |