Compare commits

..

No commits in common. "eb58b4acc83c27dcb8c2702a7915732dd344cfe7" and "d56e8baca5ff273bc431fa4c1f4e68887d8bda36" have entirely different histories.

13 changed files with 20 additions and 145 deletions

View File

@ -30,7 +30,6 @@ Result<int> luna_main(int argc, char** argv)
StringView filename;
os::ArgumentParser parser;
parser.add_description("Concatenate files to standard output."_sv);
parser.add_positional_argument(filename, "file"_sv, "-"_sv);
Vector<StringView> extra_files = TRY(parser.parse(argc, argv));

View File

@ -9,7 +9,6 @@ int main(int argc, char** argv)
StringView path;
os::ArgumentParser parser;
parser.add_description("Change the permissions of a file."_sv);
parser.add_positional_argument(mode_string, "mode"_sv, true);
parser.add_positional_argument(path, "path"_sv, true);
parser.parse(argc, argv);

View File

@ -9,7 +9,6 @@ int main(int argc, char** argv)
StringView path;
os::ArgumentParser parser;
parser.add_description("Change the owner and group of a file."_sv);
parser.add_positional_argument(user, "user"_sv, true);
parser.add_positional_argument(path, "path"_sv, true);
parser.parse(argc, argv);

View File

@ -10,9 +10,7 @@ int main(int argc, char** argv)
StringView date;
os::ArgumentParser parser;
parser.add_description("Display the current (or another) date and time."_sv);
parser.add_value_argument(date, 'd', "date"_sv, true,
"the UNIX timestamp to display instead of the current time"_sv);
parser.add_value_argument(date, 'd', "date"_sv, true);
parser.parse(argc, argv);
time_t now;

View File

@ -9,7 +9,6 @@ Result<int> luna_main(int argc, char** argv)
StringView pathname;
os::ArgumentParser parser;
parser.add_description("Edit a file using basic line-based shell editing."_sv);
parser.add_positional_argument(pathname, "path"_sv, true);
parser.parse(argc, argv);

View File

@ -12,10 +12,9 @@ Result<int> luna_main(int argc, char** argv)
bool show_almost_all { false };
os::ArgumentParser parser;
parser.add_description("List files contained in a directory (defaults to '.', the current directory)"_sv);
parser.add_positional_argument(pathname, "directory"_sv, "."_sv);
parser.add_switch_argument(show_all, 'a', "all"_sv, "also list hidden files (whose filename begins with a dot)"_sv);
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv, "list all files except '.' and '..'"_sv);
parser.add_switch_argument(show_all, 'a', "all"_sv);
parser.add_switch_argument(show_almost_all, 'A', "almost-all"_sv);
parser.parse(argc, argv);
DIR* dp = opendir(pathname.chars());

View File

@ -30,11 +30,9 @@ Result<int> luna_main(int argc, char** argv)
bool recursive;
os::ArgumentParser parser;
parser.add_description("Create directories."_sv);
parser.add_positional_argument(path, "path"_sv, true);
parser.add_positional_argument(mode_string, "mode"_sv, "755"_sv);
parser.add_switch_argument(recursive, 'p', "parents"_sv,
"if parent directories do not exist, create them as well"_sv);
parser.add_switch_argument(recursive, 'p', "parents"_sv);
parser.parse(argc, argv);
mode_t mode = (mode_t)parse_unsigned_integer(mode_string.chars(), nullptr, 8);

View File

@ -7,10 +7,8 @@ Result<int> luna_main(int argc, char** argv)
bool recursive;
os::ArgumentParser parser;
parser.add_description("Remove a path from the file system."_sv);
parser.add_positional_argument(path, "path"_sv, true);
parser.add_switch_argument(recursive, 'r', "recursive"_sv,
"remove a directory recursively (by default, rm removes only empty directories)"_sv);
parser.add_switch_argument(recursive, 'r', "recursive"_sv);
parser.parse(argc, argv);
if (!recursive) TRY(os::FileSystem::remove(path));

View File

@ -37,9 +37,8 @@ Result<int> luna_main(int argc, char** argv)
SharedPtr<File> input_file;
os::ArgumentParser parser;
parser.add_description("The Luna system's command shell."_sv);
parser.add_positional_argument(path, "path"_sv, "-"_sv);
parser.add_value_argument(command, 'c', "command"_sv, true, "execute a single command and then exit"_sv);
parser.add_value_argument(command, 'c', "command"_sv, true);
parser.parse(argc, argv);
if (!command.is_empty()) TRY(execute_command(command));

View File

@ -62,7 +62,6 @@ Result<int> luna_main(int argc, char** argv)
}
os::ArgumentParser parser;
parser.add_description("Switch to a different user."_sv);
parser.add_positional_argument(name, "name"_sv, true);
parser.parse(argc, argv);

View File

@ -117,7 +117,6 @@ static usize parse_width(const char** format, flags_t& flags, va_list ap)
flags |= FLAG_LEFT_ALIGN;
result = (usize)-width;
}
(*format)++;
}
return result;
@ -140,7 +139,6 @@ static usize parse_precision(const char** format, flags_t& flags, va_list ap)
if (precision >= 0) result = (usize)precision;
else
result = 0;
(*format)++;
}
}

View File

@ -7,19 +7,15 @@ namespace os
class ArgumentParser
{
public:
ArgumentParser(bool add_help = true);
void add_description(StringView description);
ArgumentParser() = default;
Result<void> add_positional_argument(StringView& out, StringView name, bool required);
Result<void> add_positional_argument(StringView& out, StringView name, StringView fallback);
Result<void> add_switch_argument(bool& out, char short_flag, StringView long_flag, StringView help = {});
Result<void> add_switch_argument(bool& out, char short_flag, StringView long_flag);
Result<void> add_value_argument(StringView& out, char short_flag, StringView long_flag, bool value_required,
StringView help = {});
Result<void> add_value_argument(StringView& out, char short_flag, StringView long_flag, StringView fallback,
StringView help = {});
Result<void> add_value_argument(StringView& out, char short_flag, StringView long_flag, bool value_required);
Result<void> add_value_argument(StringView& out, char short_flag, StringView long_flag, StringView fallback);
Result<Vector<StringView>> parse(int argc, char* const* argv);
@ -37,7 +33,6 @@ namespace os
bool* out;
char short_flag;
StringView long_flag;
StringView help;
};
struct ValueArgument
@ -47,17 +42,10 @@ namespace os
StringView long_flag;
bool required;
StringView fallback;
StringView help;
};
Result<void> usage(StringView program_name);
void short_usage(StringView program_name);
Vector<PositionalArgument> m_positional_args;
Vector<SwitchArgument> m_switch_args;
Vector<ValueArgument> m_value_args;
StringView m_description = {};
bool m_add_short_help_flag { false };
bool m_add_long_help_flag { false };
};
}

View File

@ -1,19 +1,9 @@
#include <luna/StringBuilder.h>
#include <os/ArgumentParser.h>
#include <stdio.h>
#include <stdlib.h>
namespace os
{
ArgumentParser::ArgumentParser(bool add_help) : m_add_short_help_flag(add_help), m_add_long_help_flag(add_help)
{
}
void ArgumentParser::add_description(StringView description)
{
m_description = description;
}
Result<void> ArgumentParser::add_positional_argument(StringView& out, StringView name, bool required)
{
PositionalArgument arg { &out, name, required, {} };
@ -28,34 +18,25 @@ namespace os
return m_positional_args.try_append(move(arg));
}
Result<void> ArgumentParser::add_switch_argument(bool& out, char short_flag, StringView long_flag, StringView help)
Result<void> ArgumentParser::add_switch_argument(bool& out, char short_flag, StringView long_flag)
{
SwitchArgument arg { &out, short_flag, long_flag, help };
if (short_flag == 'h') m_add_short_help_flag = false;
if (long_flag == "help"_sv) m_add_long_help_flag = false;
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,
bool value_required, StringView help)
bool value_required)
{
ValueArgument arg { &out, short_flag, long_flag, value_required, {}, help };
if (short_flag == 'h') m_add_short_help_flag = false;
if (long_flag == "help"_sv) m_add_long_help_flag = false;
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,
StringView fallback, StringView help)
StringView fallback)
{
ValueArgument arg { &out, short_flag, long_flag, false, fallback, help };
if (short_flag == 'h') m_add_short_help_flag = false;
if (long_flag == "help"_sv) m_add_long_help_flag = false;
ValueArgument arg { &out, short_flag, long_flag, false, fallback };
return m_value_args.try_append(move(arg));
}
@ -106,8 +87,6 @@ namespace os
bool found = false;
if (m_add_long_help_flag && flag == "help"_sv) { TRY(usage(program_name)); }
for (const auto& current : m_switch_args)
{
if (current.long_flag == flag)
@ -132,7 +111,7 @@ namespace os
if (found) continue;
fprintf(stderr, "%s: unrecognized option '%s'\n", program_name.chars(), arg.chars());
short_usage(program_name);
exit(1);
}
else if (looks_like_short_flag(arg))
{
@ -143,8 +122,6 @@ namespace os
char c = flags[j];
bool found = false;
if (m_add_short_help_flag && c == 'h') { TRY(usage(program_name)); }
// Last flag, this could be a value flag
if (j + 1 == flags.length())
{
@ -179,7 +156,7 @@ namespace os
if (found) continue;
fprintf(stderr, "%s: invalid option -- '%c'\n", program_name.chars(), c);
short_usage(program_name);
exit(1);
}
continue;
@ -202,7 +179,7 @@ namespace os
{
fprintf(stderr, "%s: option '--%s' requires an argument\n", program_name.chars(),
current_value_argument->long_flag.chars());
short_usage(program_name);
exit(1);
}
else { *current_value_argument->out = current_value_argument->fallback; }
}
@ -213,86 +190,11 @@ namespace os
if (arg.required)
{
fprintf(stderr, "%s: required argument '%s' not provided\n", program_name.chars(), arg.name.chars());
short_usage(program_name);
exit(1);
}
else { *arg.out = arg.fallback; }
}
return leftovers;
}
Result<void> ArgumentParser::usage(StringView program_name)
{
StringBuilder sb;
TRY(sb.format("Usage: %s <options>"_sv, program_name.chars()));
for (const auto& arg : m_positional_args)
{
if (arg.required) { TRY(sb.format(" %s", arg.name.chars())); }
else { TRY(sb.format(" [%s]", arg.name.chars())); }
}
TRY(sb.add('\n'));
auto usage_line = TRY(sb.string());
fputs(usage_line.chars(), stdout);
if (!m_description.is_empty()) { puts(m_description.chars()); }
if (m_switch_args.size() || m_value_args.size())
{
putchar('\n');
puts("Options:");
}
for (const auto& arg : m_switch_args)
{
if (arg.long_flag.is_empty())
{
if (arg.short_flag == ' ') continue;
printf(" -%-25c %s\n", arg.short_flag, arg.help.chars());
}
else if (arg.short_flag == ' ') { printf(" --%-20s %s\n", arg.long_flag.chars(), arg.help.chars()); }
else { printf(" -%c, --%-20s %s\n", arg.short_flag, arg.long_flag.chars(), arg.help.chars()); }
}
for (const auto& arg : m_value_args)
{
StringView value_name;
if (arg.required) value_name = "VALUE"_sv;
else
value_name = "[VALUE]"_sv;
int field_size = 20 - (int)(arg.long_flag.length() + 1);
if (field_size < 0) field_size = 0;
if (arg.long_flag.is_empty())
{
if (arg.short_flag == ' ') continue;
printf(" -%c %-20s %s\n", arg.short_flag, value_name.chars(), arg.help.chars());
}
else if (arg.short_flag == ' ')
{
printf(" --%s %-*s %s\n", arg.long_flag.chars(), field_size, value_name.chars(), arg.help.chars());
}
else
{
printf(" -%c, --%s %-*s %s\n", arg.short_flag, arg.long_flag.chars(), field_size, value_name.chars(),
arg.help.chars());
}
}
exit(0);
}
void ArgumentParser::short_usage(StringView program_name)
{
if (m_add_short_help_flag || m_add_long_help_flag)
fprintf(stderr, "Try running '%s %s' for more information.\n", program_name.chars(),
m_add_long_help_flag ? "--help" : "-h");
exit(1);
}
}