Parse command line with boost program_options
Some checks failed
Test / build (push) Failing after 16s
Some checks failed
Test / build (push) Failing after 16s
This commit is contained in:
73
cli/command_line.cpp
Normal file
73
cli/command_line.cpp
Normal 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]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user