libos: Put everything under the os namespace

This commit is contained in:
apio 2023-04-07 10:40:46 +02:00
parent f1e2937528
commit c752b2b343
Signed by: asleepymoon
GPG Key ID: B8A7D06E42258954
6 changed files with 199 additions and 193 deletions

View File

@ -35,7 +35,7 @@ int main(int argc, char** argv)
{ {
StringView filename; StringView filename;
ArgumentParser parser; os::ArgumentParser parser;
parser.add_positional_argument(filename, "file"_sv, "-"_sv); parser.add_positional_argument(filename, "file"_sv, "-"_sv);
Vector<StringView> extra_files = parser.parse(argc, argv).value(); Vector<StringView> extra_files = parser.parse(argc, argv).value();

View File

@ -9,7 +9,7 @@ int main(int argc, char** argv)
{ {
StringView date; StringView date;
ArgumentParser parser; os::ArgumentParser parser;
parser.add_value_argument(date, 'd', "date"_sv, true); parser.add_value_argument(date, 'd', "date"_sv, true);
parser.parse(argc, argv); parser.parse(argc, argv);

View File

@ -9,7 +9,7 @@ int main(int argc, char** argv)
FILE* f; FILE* f;
StringView pathname; StringView pathname;
ArgumentParser parser; os::ArgumentParser parser;
parser.add_positional_argument(pathname, "file"_sv, true); parser.add_positional_argument(pathname, "file"_sv, true);
parser.parse(argc, argv); parser.parse(argc, argv);

View File

@ -11,7 +11,7 @@ int main(int argc, char** argv)
bool show_all { false }; bool show_all { false };
bool show_almost_all { false }; bool show_almost_all { false };
ArgumentParser parser; os::ArgumentParser parser;
parser.add_positional_argument(pathname, "directory"_sv, "/"_sv); parser.add_positional_argument(pathname, "directory"_sv, "/"_sv);
parser.add_switch_argument(show_all, 'a', "all"_sv); parser.add_switch_argument(show_all, 'a', "all"_sv);
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv); parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv);

View File

@ -2,8 +2,10 @@
#include <luna/StringView.h> #include <luna/StringView.h>
#include <luna/Vector.h> #include <luna/Vector.h>
class ArgumentParser namespace os
{ {
class ArgumentParser
{
public: public:
ArgumentParser() = default; ArgumentParser() = default;
@ -45,4 +47,5 @@ class ArgumentParser
Vector<PositionalArgument> m_positional_args; Vector<PositionalArgument> m_positional_args;
Vector<SwitchArgument> m_switch_args; Vector<SwitchArgument> m_switch_args;
Vector<ValueArgument> m_value_args; Vector<ValueArgument> m_value_args;
}; };
}

View File

@ -2,55 +2,57 @@
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
Result<void> ArgumentParser::add_positional_argument(StringView& out, StringView name, bool required) namespace os
{ {
Result<void> ArgumentParser::add_positional_argument(StringView& out, StringView name, bool required)
{
PositionalArgument arg { &out, name, required, {} }; PositionalArgument arg { &out, name, required, {} };
return m_positional_args.try_append(move(arg)); return m_positional_args.try_append(move(arg));
} }
Result<void> ArgumentParser::add_positional_argument(StringView& out, StringView name, StringView fallback) Result<void> ArgumentParser::add_positional_argument(StringView& out, StringView name, StringView fallback)
{ {
PositionalArgument arg { &out, name, false, fallback }; PositionalArgument arg { &out, name, false, fallback };
return m_positional_args.try_append(move(arg)); return m_positional_args.try_append(move(arg));
} }
Result<void> ArgumentParser::add_switch_argument(bool& out, char short_flag, StringView long_flag) Result<void> ArgumentParser::add_switch_argument(bool& out, char short_flag, StringView long_flag)
{ {
SwitchArgument arg { &out, short_flag, long_flag }; SwitchArgument arg { &out, short_flag, long_flag };
return m_switch_args.try_append(move(arg)); return m_switch_args.try_append(move(arg));
} }
Result<void> ArgumentParser::add_value_argument(StringView& out, char short_flag, StringView long_flag, Result<void> ArgumentParser::add_value_argument(StringView& out, char short_flag, StringView long_flag,
bool value_required) bool value_required)
{ {
ValueArgument arg { &out, short_flag, long_flag, value_required, {} }; ValueArgument arg { &out, short_flag, long_flag, value_required, {} };
return m_value_args.try_append(move(arg)); return m_value_args.try_append(move(arg));
} }
Result<void> ArgumentParser::add_value_argument(StringView& out, char short_flag, StringView long_flag, Result<void> ArgumentParser::add_value_argument(StringView& out, char short_flag, StringView long_flag,
StringView fallback) StringView fallback)
{ {
ValueArgument arg { &out, short_flag, long_flag, false, fallback }; ValueArgument arg { &out, short_flag, long_flag, false, fallback };
return m_value_args.try_append(move(arg)); return m_value_args.try_append(move(arg));
} }
static bool looks_like_short_flag(StringView arg) static bool looks_like_short_flag(StringView arg)
{ {
return arg.length() > 1 && arg[0] == '-'; return arg.length() > 1 && arg[0] == '-';
} }
static bool looks_like_long_flag(StringView arg) static bool looks_like_long_flag(StringView arg)
{ {
return arg.length() > 2 && arg[0] == '-' && arg[1] == '-'; return arg.length() > 2 && arg[0] == '-' && arg[1] == '-';
} }
Result<Vector<StringView>> ArgumentParser::parse(int argc, char* const* argv) Result<Vector<StringView>> ArgumentParser::parse(int argc, char* const* argv)
{ {
StringView program_name = argv[0]; StringView program_name = argv[0];
Vector<StringView> leftovers; Vector<StringView> leftovers;
@ -194,4 +196,5 @@ Result<Vector<StringView>> ArgumentParser::parse(int argc, char* const* argv)
} }
return leftovers; return leftovers;
}
} }