Parse command line with boost program_options
Some checks failed
Test / build (push) Failing after 16s

This commit is contained in:
2026-01-18 18:44:22 +01:00
parent 2485d00c4c
commit c9a3ebd623
5 changed files with 116 additions and 116 deletions

73
cli/command_line.cpp Normal file
View File

@@ -0,0 +1,73 @@
/*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
module;
#include <variant>
#include <string>
#include <stdexcept>
#include <algorithm>
#include <cstring>
#include <boost/program_options.hpp>
export module katja.command_line;
namespace katja
{
export enum class command
{
updates,
search,
help
};
export struct command_line
{
const command type;
const std::vector<std::string> arguments;
};
export class invalid_command_error : public std::runtime_error
{
const std::string command;
public:
invalid_command_error()
: std::runtime_error("Expecting the command line to begin with a command.")
{
}
invalid_command_error(const std::string& invalid_command)
: command{ invalid_command },
std::runtime_error("Unknown command given on the command line.")
{
}
};
export command_line parse_command_line(int argc, const char **argv)
{
if (argc == 1)
{
throw invalid_command_error();
}
std::vector<std::string> arguments;
std::copy(argv + 2, argv + argc, std::back_inserter(arguments));
if (strcmp("updates", argv[1]) == 0)
{
return command_line{ command::updates, std::move(arguments) };
}
if (strcmp("search", argv[1]) == 0)
{
return command_line{ command::search, std::move(arguments) };
}
if (strcmp("help", argv[1]) == 0)
{
return command_line{ command::help, std::move(arguments) };
}
throw invalid_command_error(argv[1]);
}
}