From 87467871372df6505ef14eb2545a008137e17cad Mon Sep 17 00:00:00 2001 From: Nathan Hughes Date: Wed, 30 Jul 2025 13:35:46 -0400 Subject: [PATCH] add option to force block style when compositing --- config_utilities/app/composite_configs.cpp | 30 +++++++++++++++++++ .../config_utilities/parsing/commandline.h | 1 + config_utilities/src/commandline.cpp | 7 +++++ 3 files changed, 38 insertions(+) diff --git a/config_utilities/app/composite_configs.cpp b/config_utilities/app/composite_configs.cpp index 1aa7648f..aa2a6b79 100644 --- a/config_utilities/app/composite_configs.cpp +++ b/config_utilities/app/composite_configs.cpp @@ -21,6 +21,7 @@ Invalid YAML or missing files get dropped during compositing. multiple times. -d/--disable-substitutions: Turn off substitutions resolution --no-disable-substitutions: Turn substitution resolution on (currently on by default) + --force-block-style: Force emitted YAML to only use block style Example: > echo "{a: 42, bar: hello}" > /tmp/test_in.yaml @@ -87,6 +88,32 @@ inline void forceBlockForNonLeaves(YAML::Node node) { } } +inline void forceBlockForAll(YAML::Node node) { + switch (node.Type()) { + case YAML::NodeType::Sequence: + if (node.size() > 0) { + node.SetStyle(YAML::EmitterStyle::Block); + } + for (const auto& child : node) { + forceBlockForAll(child); + } + break; + case YAML::NodeType::Map: + if (node.size() > 0) { + node.SetStyle(YAML::EmitterStyle::Block); + } + for (const auto& child : node) { + forceBlockForAll(child.second); + } + break; + case YAML::NodeType::Null: + case YAML::NodeType::Undefined: + case YAML::NodeType::Scalar: + default: + return; + } +} + } // namespace int main(int argc, char* argv[]) { @@ -98,6 +125,9 @@ int main(int argc, char* argv[]) { } forceBlockForNonLeaves(result); + if (info.force_block_style) { + forceBlockForAll(result); + } YAML::Emitter emit; switch (result.Type()) { diff --git a/config_utilities/include/config_utilities/parsing/commandline.h b/config_utilities/include/config_utilities/parsing/commandline.h index b4864d50..c7d6d28e 100644 --- a/config_utilities/include/config_utilities/parsing/commandline.h +++ b/config_utilities/include/config_utilities/parsing/commandline.h @@ -47,6 +47,7 @@ namespace internal { struct ParserInfo { bool help_present = false; + bool force_block_style = false; }; /** diff --git a/config_utilities/src/commandline.cpp b/config_utilities/src/commandline.cpp index 3a64472a..aefce586 100644 --- a/config_utilities/src/commandline.cpp +++ b/config_utilities/src/commandline.cpp @@ -215,6 +215,13 @@ CliParser& CliParser::parse(int& argc, char* argv[], bool remove_args) { continue; } + if ((curr_opt == "--force-block-style") && !found_separator) { + info.force_block_style = true; + spans.emplace_back(Span{i, 0, curr_opt}); + ++i; + continue; + } + if (curr_opt == "--" && !found_separator) { found_separator = true; spans.emplace_back(Span{i, 0, curr_opt});