mirror of
https://github.com/modelec/cpp-lib.git
synced 2026-03-19 05:20:27 +01:00
initial commit
This commit is contained in:
24
components/CLParser/CMakeLists.txt
Normal file
24
components/CLParser/CMakeLists.txt
Normal file
@@ -0,0 +1,24 @@
|
||||
cmake_minimum_required(VERSION 3.25)
|
||||
project(CLParser)
|
||||
|
||||
# Define the library
|
||||
add_library(CLParser SHARED
|
||||
src/CLParser.cpp
|
||||
)
|
||||
|
||||
# Specify include directories
|
||||
target_include_directories(CLParser PUBLIC
|
||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}/include>
|
||||
$<INSTALL_INTERFACE:include>
|
||||
)
|
||||
|
||||
# Export target
|
||||
install(TARGETS CLParser
|
||||
EXPORT ModelecTargets
|
||||
LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
|
||||
RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}
|
||||
INCLUDES DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}
|
||||
)
|
||||
|
||||
install(DIRECTORY include/ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
|
||||
33
components/CLParser/include/Modelec/CLParser.h
Normal file
33
components/CLParser/include/Modelec/CLParser.h
Normal file
@@ -0,0 +1,33 @@
|
||||
#pragma once
|
||||
|
||||
#include <map>
|
||||
#include <optional>
|
||||
#include <string>
|
||||
|
||||
class CLParser {
|
||||
|
||||
public:
|
||||
CLParser(int argc, char** argv);
|
||||
|
||||
CLParser(const CLParser& other);
|
||||
|
||||
[[nodiscard]] bool hasOption(const std::string& option) const;
|
||||
|
||||
[[nodiscard]] std::optional<std::string> getOption(const std::string& option) const;
|
||||
|
||||
[[nodiscard]] std::string getOption(const std::string& option, const std::string& defaultValue) const;
|
||||
|
||||
[[nodiscard]] bool hasPositionalArgument(int index) const;
|
||||
|
||||
[[nodiscard]] std::string getPositionalArgument(int index) const;
|
||||
|
||||
[[nodiscard]] int positionalArgumentsCount() const;
|
||||
|
||||
~CLParser();
|
||||
|
||||
private:
|
||||
std::string* _argv;
|
||||
int _argc;
|
||||
|
||||
std::map<std::string, std::string> _options;
|
||||
};
|
||||
68
components/CLParser/src/CLParser.cpp
Normal file
68
components/CLParser/src/CLParser.cpp
Normal file
@@ -0,0 +1,68 @@
|
||||
#include <Modelec/CLParser.h>
|
||||
|
||||
CLParser::CLParser(int argc, char **argv) : _argc(argc) {
|
||||
this->_argv = new std::string[argc];
|
||||
for (int i = 0; i < argc; i++) {
|
||||
this->_argv[i] = argv[i];
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
if (this->_argv[i].rfind("--", 0) == 0) {
|
||||
std::string option = this->_argv[i].substr(2);
|
||||
if (i + 1 < argc && this->_argv[i + 1].rfind("--", 0) != 0) {
|
||||
this->_options[option] = this->_argv[i + 1];
|
||||
} else {
|
||||
this->_options[option] = "";
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
std::optional<std::string> CLParser::getOption(const std::string &option) const {
|
||||
if (!this->hasOption(option)) {
|
||||
return std::nullopt;
|
||||
}
|
||||
|
||||
return this->_options.at(option);
|
||||
}
|
||||
|
||||
bool CLParser::hasOption(const std::string &option) const {
|
||||
return this->_options.find(option) != this->_options.end();
|
||||
}
|
||||
|
||||
std::string CLParser::getOption(const std::string &option, const std::string &defaultValue) const {
|
||||
if (!this->hasOption(option)) {
|
||||
return defaultValue;
|
||||
}
|
||||
|
||||
return this->_options.at(option);
|
||||
}
|
||||
|
||||
bool CLParser::hasPositionalArgument(int index) const {
|
||||
return index < this->_argc;
|
||||
}
|
||||
|
||||
std::string CLParser::getPositionalArgument(int index) const {
|
||||
if (!this->hasPositionalArgument(index)) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return this->_argv[index];
|
||||
}
|
||||
|
||||
int CLParser::positionalArgumentsCount() const {
|
||||
return this->_argc;
|
||||
}
|
||||
|
||||
CLParser::~CLParser() {
|
||||
delete[] this->_argv;
|
||||
}
|
||||
|
||||
CLParser::CLParser(const CLParser &other) : _argc(other._argc) {
|
||||
this->_argv = new std::string[other._argc];
|
||||
for (int i = 0; i < other._argc; i++) {
|
||||
this->_argv[i] = other._argv[i];
|
||||
}
|
||||
|
||||
this->_options = other._options;
|
||||
}
|
||||
Reference in New Issue
Block a user