libarc-validate/arc/validate/command-line.cxx
2024-09-03 01:43:56 +02:00

113 lines
2.5 KiB
C++

#include <arc/validate/command-line.hxx>
namespace arc::validate
{
command_line_t
parse_command_line(std::vector<std::string> const& args)
{
command_line_t command_line;
bool only_files{false};
for (auto it = args.begin(); it != args.end(); ++it) {
auto const& option = *it;
// fixme: should empty options be an error?
//
if (option.size() < 1) {
continue;
}
if (only_files || '-' != option[0] || 1 == option.size()) {
throw command_line_error_t{"invalid file argument '" + option + "'"};
}
if ("--" == option) {
only_files = true;
continue;
}
if ("--version" == option) {
command_line.print_version = true;
continue;
}
if ("--help" == option) {
command_line.print_usage = true;
continue;
}
if ("--print-information" == option) {
command_line.print_information = true;
continue;
}
if ("--print-warnings" == option) {
command_line.print_warnings = true;
continue;
}
if ("--print-success" == option) {
command_line.print_success = true;
continue;
}
if ("--print-failure" == option) {
command_line.print_failure = true;
continue;
}
if ("--verbose" == option) {
command_line.print_information = true;
command_line.print_warnings = true;
command_line.print_success = true;
command_line.print_failure = true;
continue;
}
if ("-i" == option || "--include" == option) {
++it;
if (it == args.end()) {
throw command_line_error_t{"missing argument to '--include'"};
}
if (!command_line.exclude.empty()) {
throw command_line_error_t{"cannot specify both `-i, --include` and `-e, --exclude`"};
}
command_line.include.emplace(*it);
continue;
}
if ("-e" == option || "--exclude" == option) {
++it;
if (it == args.end()) {
throw command_line_error_t{"missing argument to '--exclude'"};
}
if (!command_line.include.empty()) {
throw command_line_error_t{"cannot specify both `-i, --include` and `-e, --exclude`"};
}
command_line.exclude.emplace(*it);
continue;
}
if ("-l" == option || "--list" == option) {
command_line.list = true;
continue;
}
throw command_line_error_t{"unrecognized option '" + option + "'"};
}
return command_line;
}
}