diff options
| author | Eugen Wissner <belka@caraus.de> | 2026-01-18 18:44:22 +0100 |
|---|---|---|
| committer | Eugen Wissner <belka@caraus.de> | 2026-01-18 18:44:22 +0100 |
| commit | c9a3ebd623bf2f968f7fbdf5bb2d7dda480b9f1c (patch) | |
| tree | e1a1837c62ffcf7cd0255395cdc449430cb9f352 /cli/command_line.cpp | |
| parent | 2485d00c4c40713b6cb3c39595d69ea2cf497b47 (diff) | |
| download | katja-c9a3ebd623bf2f968f7fbdf5bb2d7dda480b9f1c.tar.gz | |
Parse command line with boost program_options
Diffstat (limited to 'cli/command_line.cpp')
| -rw-r--r-- | cli/command_line.cpp | 73 |
1 files changed, 73 insertions, 0 deletions
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 <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]); + } +} |
