#include #include #include Result ArgumentParser::add_positional_argument(StringView& out, StringView name, bool required) { PositionalArgument arg { &out, name, required, {} }; return m_positional_args.try_append(move(arg)); } Result ArgumentParser::add_positional_argument(StringView& out, StringView name, bool required, StringView fallback) { PositionalArgument arg { &out, name, required, fallback }; return m_positional_args.try_append(move(arg)); } void ArgumentParser::parse(int argc, char* const* argv) { StringView program_name = argv[0]; for (int i = 1; i < argc; i++) { StringView arg = argv[i]; Option current = m_positional_args.try_dequeue(); if (!current.has_value()) { fprintf(stderr, "%s: unused argument '%s'\n", program_name.chars(), arg.chars()); continue; } *current->out = arg; } // Loop through all remaining positional arguments. for (const auto& arg : m_positional_args) { if (arg.required) { fprintf(stderr, "%s: required argument '%s' not provided\n", program_name.chars(), arg.name.chars()); exit(1); } else { *arg.out = arg.fallback; } } }