UsenetSearch/include/usenetsearch/Application.h

104 lines
2.6 KiB
C++

/*
Copyright© 2021 John Sennesael
UsenetSearch is Free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
UsenetSearch is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with UsenetSearch. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include "usenetsearch/Configuration.h"
#include "usenetsearch/Database.h"
#include "usenetsearch/Filter.h"
#include <filesystem>
#include <string>
#include <vector>
namespace usenetsearch {
enum class CommandLineOptionType
{
Boolean, Integer, Path, String
};
struct CommandLineOption
{
virtual ~CommandLineOption() = default;
CommandLineOptionType type;
char option;
std::string helpText;
};
template<typename T>
struct CommandLineOptionValue: public CommandLineOption
{
virtual ~CommandLineOptionValue() = default;
T value;
std::function<void(T value)> onParse;
};
class Application
{
bool m_canRun{false};
std::vector<std::shared_ptr<CommandLineOption>> m_commandLineArguments;
Configuration m_config;
std::string m_configFile{"usenetsearch.conf"};
Database m_db;
Filter m_filter;
void ExecuteCustomOption(
std::shared_ptr<CommandLineOption>&,
const std::string& value=""
);
void ParseArgs(int argc, char* argv[]);
public:
Application();
void AddBooleanOption(
char option,
const std::string& help,
std::function<void(bool)> onParse,
bool defaultValue = false
);
void AddFileOption(
char option,
const std::string& help,
std::function<void(std::filesystem::path)> onParse,
std::filesystem::path defaultValue = "."
);
void AddIntegerOption(
char option,
const std::string& help,
std::function<void(int)> onParse,
int defaultValue = 0
);
void AddStringOption(
char option,
const std::string& help,
std::function<void(std::string)> onParse,
std::string defaultValue = ""
);
bool CanRun() const;
Configuration& GetConfig();
Database& GetDb();
Filter& GetFilter();
bool Init(int argc, char* argv[]);
void Usage(const std::string& programName);
};
} // namespace usenetsearch