LibCore: Make ArgsParser tolerate Main::Arguments with only .strings

This currently allocates in .parse(), but that's better than making the
caller do the exact same before passing us the values.

Note that this is only temporary to aid in conversion, a future commit
will remove this and switch to requiring the users to allocate the
vector instead.
This commit is contained in:
Ali Mohammad Pur 2023-02-18 12:29:21 +03:30 committed by Ali Mohammad Pur
parent d575c50f3e
commit 6e5ba82929
Notes: sideshowbarker 2024-07-18 04:38:32 +09:00

View file

@ -73,6 +73,19 @@ public:
bool parse(int argc, char* const* argv, FailureBehavior failure_behavior = FailureBehavior::PrintUsageAndExit);
bool parse(Main::Arguments const& arguments, FailureBehavior failure_behavior = FailureBehavior::PrintUsageAndExit)
{
if (arguments.argv == nullptr && arguments.argc == 0) {
// Allocate the data from arguments.strings instead.
Vector<DeprecatedString> strings;
Vector<char const*> data;
strings.ensure_capacity(arguments.strings.size());
data.ensure_capacity(arguments.strings.size());
for (auto& entry : arguments.strings) {
strings.append(entry);
data.append(strings.last().characters());
}
return parse(data.size(), const_cast<char* const*>(data.data()), failure_behavior);
}
return parse(arguments.argc, arguments.argv, failure_behavior);
}