diff options
Diffstat (limited to 'wfpctl/src/extras/cli/util.cpp')
| -rw-r--r-- | wfpctl/src/extras/cli/util.cpp | 62 |
1 files changed, 62 insertions, 0 deletions
diff --git a/wfpctl/src/extras/cli/util.cpp b/wfpctl/src/extras/cli/util.cpp new file mode 100644 index 0000000000..6181e68639 --- /dev/null +++ b/wfpctl/src/extras/cli/util.cpp @@ -0,0 +1,62 @@ +#include "stdafx.h" +#include "util.h" +#include "inlineformatter.h" +#include "libcommon/string.h" +#include <string> + +void PrettyPrintProperties(MessageSink messageSink, PrettyPrintOptions options, const PropertyList &properties) +{ + size_t longestName = 0; + + for (auto &property : properties) + { + longestName = max(longestName, property.name.size()); + } + + // + // Format is: + // indent, name, [separator], tab, [tab, ...] + // + + const auto separator = std::wstring(options.useSeparator ? L":" : L""); + + auto insert = options.indent + longestName + separator.size(); + + if (0 == (insert % 8)) + { + insert += 8; + } + else + { + insert = ((insert / 8) + 1) * 8; + } + + std::wstring indenter(options.indent, L' '); + InlineFormatter f; + + for (auto &property : properties) + { + auto at = options.indent + property.name.size() + separator.size(); + auto distance = insert - at; + auto tabs = (0 == (distance % 8) ? distance / 8 : (distance / 8) + 1); + + messageSink((f << indenter << property.name << separator + << std::wstring(tabs, L'\t') << property.value).str()); + } +} + +std::wstring GetArgumentValue(const common::string::KeyValuePairs &arguments, const std::wstring &key) +{ + auto arg = arguments.find(key); + + if (arguments.end() == arg) + { + std::wstringstream ss; + + ss << L"Missing argument: '" << key << L"'"; + + throw std::runtime_error(common::string::ToAnsi(ss.str())); + } + + return arg->second; +} |
