Browse Source

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.
Ali Mohammad Pur 2 năm trước cách đây
mục cha
commit
6e5ba82929
1 tập tin đã thay đổi với 13 bổ sung0 xóa
  1. 13 0
      Userland/Libraries/LibCore/ArgsParser.h

+ 13 - 0
Userland/Libraries/LibCore/ArgsParser.h

@@ -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);
     }