libos: Put everything under the os namespace
This commit is contained in:
parent
f1e2937528
commit
c752b2b343
@ -35,7 +35,7 @@ int main(int argc, char** argv)
|
||||
{
|
||||
StringView filename;
|
||||
|
||||
ArgumentParser parser;
|
||||
os::ArgumentParser parser;
|
||||
parser.add_positional_argument(filename, "file"_sv, "-"_sv);
|
||||
Vector<StringView> extra_files = parser.parse(argc, argv).value();
|
||||
|
||||
|
@ -9,7 +9,7 @@ int main(int argc, char** argv)
|
||||
{
|
||||
StringView date;
|
||||
|
||||
ArgumentParser parser;
|
||||
os::ArgumentParser parser;
|
||||
parser.add_value_argument(date, 'd', "date"_sv, true);
|
||||
parser.parse(argc, argv);
|
||||
|
||||
|
@ -9,7 +9,7 @@ int main(int argc, char** argv)
|
||||
FILE* f;
|
||||
StringView pathname;
|
||||
|
||||
ArgumentParser parser;
|
||||
os::ArgumentParser parser;
|
||||
parser.add_positional_argument(pathname, "file"_sv, true);
|
||||
parser.parse(argc, argv);
|
||||
|
||||
|
@ -11,7 +11,7 @@ int main(int argc, char** argv)
|
||||
bool show_all { false };
|
||||
bool show_almost_all { false };
|
||||
|
||||
ArgumentParser parser;
|
||||
os::ArgumentParser parser;
|
||||
parser.add_positional_argument(pathname, "directory"_sv, "/"_sv);
|
||||
parser.add_switch_argument(show_all, 'a', "all"_sv);
|
||||
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv);
|
||||
|
@ -2,8 +2,10 @@
|
||||
#include <luna/StringView.h>
|
||||
#include <luna/Vector.h>
|
||||
|
||||
class ArgumentParser
|
||||
namespace os
|
||||
{
|
||||
class ArgumentParser
|
||||
{
|
||||
public:
|
||||
ArgumentParser() = default;
|
||||
|
||||
@ -45,4 +47,5 @@ class ArgumentParser
|
||||
Vector<PositionalArgument> m_positional_args;
|
||||
Vector<SwitchArgument> m_switch_args;
|
||||
Vector<ValueArgument> m_value_args;
|
||||
};
|
||||
};
|
||||
}
|
||||
|
@ -2,55 +2,57 @@
|
||||
#include <stdio.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, {} };
|
||||
|
||||
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 };
|
||||
|
||||
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 };
|
||||
|
||||
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)
|
||||
{
|
||||
{
|
||||
ValueArgument arg { &out, short_flag, long_flag, value_required, {} };
|
||||
|
||||
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)
|
||||
{
|
||||
{
|
||||
ValueArgument arg { &out, short_flag, long_flag, false, fallback };
|
||||
|
||||
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] == '-';
|
||||
}
|
||||
}
|
||||
|
||||
static bool looks_like_long_flag(StringView arg)
|
||||
{
|
||||
static bool looks_like_long_flag(StringView arg)
|
||||
{
|
||||
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];
|
||||
|
||||
Vector<StringView> leftovers;
|
||||
@ -194,4 +196,5 @@ Result<Vector<StringView>> ArgumentParser::parse(int argc, char* const* argv)
|
||||
}
|
||||
|
||||
return leftovers;
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user