Skip to content

Commit

Permalink
wip args parser
Browse files Browse the repository at this point in the history
  • Loading branch information
yeetari committed Mar 6, 2024
1 parent 30a654e commit 8f740d1
Show file tree
Hide file tree
Showing 8 changed files with 438 additions and 32 deletions.
59 changes: 59 additions & 0 deletions engine/include/vull/support/args_parser.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
#pragma once

#include <vull/container/vector.hh>
#include <vull/support/function.hh>
#include <vull/support/optional.hh>
#include <vull/support/string.hh>
#include <vull/support/string_builder.hh>

namespace vull {

enum class ArgsParseResult {
Continue,
ExitFailure,
ExitSuccess,
};

class ArgsParser {
struct Argument {
String name;
uint32_t min_values;
uint32_t max_values;
Function<ArgsParseResult(StringView, StringView)> accept_value;

String to_string() const;
};

struct Option {
String help_string;
String long_name;
char short_name;
bool has_argument;
Function<ArgsParseResult(StringView, StringView)> accept_value;
};

private:
String m_name;
String m_description;
String m_version;
Vector<Argument> m_arguments;
Vector<Option> m_options;

void print_help(StringView program_path);
ArgsParseResult handle_long_option(Option *&option_waiting_for_argument, StringView program_path,
StringView option_name);
ArgsParseResult handle_short_option(Option *&option_waiting_for_argument, StringView program_path,
char option_name);

public:
ArgsParser(String name, String description, String version);

template <typename T>
void add_argument(T &value, String name, bool required);
void add_flag(bool &present, String help_string, String long_name, char short_name = '\0');
template <typename T>
void add_option(T &value, String help_string, String long_name, char short_name = '\0');
ArgsParseResult parse_args(int argc, const char *const *argv);
};

} // namespace vull
11 changes: 9 additions & 2 deletions engine/include/vull/support/string.hh
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class String {
public:
static String copy_raw(const char *data, size_t length);
static String move_raw(char *data, size_t length);
static String repeated(char ch, size_t length);

constexpr String() = default;
explicit String(size_t length);
Expand All @@ -28,6 +29,14 @@ public:
String &operator=(const String &) = delete;
String &operator=(String &&);

int compare(StringView other) const;

bool starts_with(char ch) const;
bool ends_with(char ch) const;

bool starts_with(StringView other) const;
bool ends_with(StringView other) const;

char *begin() const { return m_data; }
char *end() const { return m_data + m_length; }

Expand All @@ -37,8 +46,6 @@ public:
char &operator[](size_t index) { return m_data[index]; }
const char &operator[](size_t index) const { return m_data[index]; }

bool ends_with(StringView end);

operator StringView() const { return view(); }
StringView view() const { return {m_data, m_length}; }

Expand Down
37 changes: 37 additions & 0 deletions engine/include/vull/support/string_view.hh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#pragma once

#include <vull/maths/common.hh>
#include <vull/support/assert.hh>
#include <vull/support/span.hh>

Expand All @@ -12,12 +13,32 @@ public:
using Span::Span;
constexpr StringView(const char *c_string) : Span(c_string, __builtin_strlen(c_string)) {}

constexpr int compare(StringView other) const;
constexpr StringView substr(size_t offset) const;
constexpr StringView substr(size_t offset, size_t length) const;

constexpr bool starts_with(char ch) const;
constexpr bool ends_with(char ch) const;

constexpr bool starts_with(StringView other) const;
constexpr bool ends_with(StringView other) const;

constexpr bool operator==(StringView other) const;
constexpr size_t length() const { return size(); }
};

constexpr int StringView::compare(StringView other) const {
int ret = __builtin_memcmp(data(), other.data(), vull::min(length(), other.length()));
if (ret == 0) {
if (length() < other.length()) {
ret = -1;
} else if (length() > other.length()) {
ret = 1;
}
}
return ret;
}

constexpr StringView StringView::substr(size_t offset) const {
return substr(offset, length() - offset);
}
Expand All @@ -27,6 +48,22 @@ constexpr StringView StringView::substr(size_t offset, size_t length) const {
return {data() + offset, length};
}

constexpr bool StringView::starts_with(char ch) const {
return !empty() && begin()[0] == ch;
}

constexpr bool StringView::ends_with(char ch) const {
return !empty() && end()[-1] == ch;
}

constexpr bool StringView::starts_with(StringView other) const {
return length() >= other.length() && substr(0, other.length()).compare(other) == 0;
}

constexpr bool StringView::ends_with(StringView other) const {
return length() >= other.length() && substr(length() - other.length()).compare(other) == 0;
}

constexpr bool StringView::operator==(StringView other) const {
if (data() == nullptr) {
return other.data() == nullptr;
Expand Down
1 change: 1 addition & 0 deletions engine/sources/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ target_sources(vull-core PRIVATE
platform/linux.cc
scene/scene.cc
scene/transform.cc
support/args_parser.cc
support/assert.cc
support/stream.cc
support/string.cc
Expand Down

0 comments on commit 8f740d1

Please sign in to comment.