Skip to content

Commit a8decb3

Browse files
add option to force block style when compositing (#55)
1 parent 37165a2 commit a8decb3

File tree

3 files changed

+38
-0
lines changed

3 files changed

+38
-0
lines changed

config_utilities/app/composite_configs.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ Invalid YAML or missing files get dropped during compositing.
2121
multiple times.
2222
-d/--disable-substitutions: Turn off substitutions resolution
2323
--no-disable-substitutions: Turn substitution resolution on (currently on by default)
24+
--force-block-style: Force emitted YAML to only use block style
2425
2526
Example:
2627
> echo "{a: 42, bar: hello}" > /tmp/test_in.yaml
@@ -87,6 +88,32 @@ inline void forceBlockForNonLeaves(YAML::Node node) {
8788
}
8889
}
8990

91+
inline void forceBlockForAll(YAML::Node node) {
92+
switch (node.Type()) {
93+
case YAML::NodeType::Sequence:
94+
if (node.size() > 0) {
95+
node.SetStyle(YAML::EmitterStyle::Block);
96+
}
97+
for (const auto& child : node) {
98+
forceBlockForAll(child);
99+
}
100+
break;
101+
case YAML::NodeType::Map:
102+
if (node.size() > 0) {
103+
node.SetStyle(YAML::EmitterStyle::Block);
104+
}
105+
for (const auto& child : node) {
106+
forceBlockForAll(child.second);
107+
}
108+
break;
109+
case YAML::NodeType::Null:
110+
case YAML::NodeType::Undefined:
111+
case YAML::NodeType::Scalar:
112+
default:
113+
return;
114+
}
115+
}
116+
90117
} // namespace
91118

92119
int main(int argc, char* argv[]) {
@@ -98,6 +125,9 @@ int main(int argc, char* argv[]) {
98125
}
99126

100127
forceBlockForNonLeaves(result);
128+
if (info.force_block_style) {
129+
forceBlockForAll(result);
130+
}
101131

102132
YAML::Emitter emit;
103133
switch (result.Type()) {

config_utilities/include/config_utilities/parsing/commandline.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,7 @@ namespace internal {
4747

4848
struct ParserInfo {
4949
bool help_present = false;
50+
bool force_block_style = false;
5051
};
5152

5253
/**

config_utilities/src/commandline.cpp

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,13 @@ CliParser& CliParser::parse(int& argc, char* argv[], bool remove_args) {
215215
continue;
216216
}
217217

218+
if ((curr_opt == "--force-block-style") && !found_separator) {
219+
info.force_block_style = true;
220+
spans.emplace_back(Span{i, 0, curr_opt});
221+
++i;
222+
continue;
223+
}
224+
218225
if (curr_opt == "--" && !found_separator) {
219226
found_separator = true;
220227
spans.emplace_back(Span{i, 0, curr_opt});

0 commit comments

Comments
 (0)