From c9a3ebd623bf2f968f7fbdf5bb2d7dda480b9f1c Mon Sep 17 00:00:00 2001 From: Eugen Wissner Date: Sun, 18 Jan 2026 18:44:22 +0100 Subject: Parse command line with boost program_options --- cli/command_line.cpp | 73 ++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 cli/command_line.cpp (limited to 'cli/command_line.cpp') diff --git a/cli/command_line.cpp b/cli/command_line.cpp new file mode 100644 index 0000000..ff6c928 --- /dev/null +++ b/cli/command_line.cpp @@ -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 +#include +#include +#include +#include + +#include + +export module katja.command_line; + +namespace katja +{ + export enum class command + { + updates, + search, + help + }; + + export struct command_line + { + const command type; + const std::vector 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 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]); + } +} -- cgit v1.2.3