cmake + bugfix

This commit is contained in:
Benedek László 2024-04-25 19:23:56 +02:00
parent ea025ba2fc
commit 729d23a1e9
6 changed files with 58 additions and 4 deletions

9
.clang-format Normal file
View File

@ -0,0 +1,9 @@
BasedOnStyle: Chromium
IndentWidth: 2
ColumnLimit: 160
SpaceAfterCStyleCast: false
UseTab: Never
AllowShortIfStatementsOnASingleLine: false
AlignTrailingComments: false
SpacesBeforeTrailingComments: 1
AlignConsecutiveMacros: Consecutive

23
CMakeLists.txt Normal file
View File

@ -0,0 +1,23 @@
cmake_minimum_required(VERSION 3.22)
project(flags-cpp)
set(CMAKE_CXX_COMPILER clang++)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -std=c++20 -Wall -Wextra -Werror")
set(CMAKE_EXPORT_COMPILE_COMMANDS true)
file(GLOB_RECURSE SOURCES "${CMAKE_SOURCE_DIR}/src/flags-cpp/*.cpp")
add_library(flags-cpp OBJECT ${SOURCES})
set_target_properties(flags-cpp PROPERTIES POSITION_INDEPENDENT_CODE 1)
target_include_directories(flags-cpp PUBLIC "${CMAKE_SOURCE_DIR}/inc")
add_library(flags-cpp-static STATIC $<TARGET_OBJECTS:flags-cpp>)
set_target_properties(flags-cpp-static PROPERTIES OUTPUT_NAME flags-cpp)
target_include_directories(flags-cpp-static PUBLIC "${CMAKE_SOURCE_DIR}/inc")
add_library(flags-cpp-shared SHARED $<TARGET_OBJECTS:flags-cpp>)
set_target_properties(flags-cpp-shared PROPERTIES OUTPUT_NAME flags-cpp)
target_include_directories(flags-cpp-shared PUBLIC "${CMAKE_SOURCE_DIR}/inc")
add_executable(example "${CMAKE_SOURCE_DIR}/src/main.cpp")
target_link_libraries(example flags-cpp-shared)

View File

@ -4,7 +4,7 @@ This project aims to recreate the functionality of the `flag` package from Golan
## Usage
### Create a parser
```c++
#include "flags.h"
#include <flags-cpp/flags-cpp.h>
Parser parser;
or

View File

@ -4,6 +4,7 @@
#include <functional>
#include <map>
#include <string>
#include <vector>
#include <typeindex>
namespace Flags {

View File

@ -1,4 +1,4 @@
#include "flags.h"
#include <flags-cpp/flags-cpp.h>
#include <iostream>
#include <sstream>
@ -136,13 +136,15 @@ bool Parser::parse(int argc, char** argv) {
for (size_t i = 1; i < args.size() - 1; i++) {
const std::string& arg = args[i];
if (starts_with(arg, prefix) && arg.size() > prefix.size()) {
flags[arg.substr(prefix.size())]->parse(args[i + 1]);
if (flags.contains(arg.substr(prefix.size())))
flags[arg.substr(prefix.size())]->parse(args[i + 1]);
}
}
// check for the last argument
if (starts_with(args[args.size() - 1], prefix) && args[args.size() - 1].size() > prefix.size()) {
flags[args[args.size() - 1].substr(prefix.size())]->parse("");
if (flags.contains(args.back().substr(prefix.size())))
flags[args.back().substr(prefix.size())]->parse("");
}
return get_found() == flags.size();

19
src/main.cpp Normal file
View File

@ -0,0 +1,19 @@
#include <flags-cpp/flags-cpp.h>
#include <iostream>
using namespace Flags;
int main(int argc, char **argv) {
Parser parser;
auto x = parser.add<std::string>("myStr", "some description", true,
"default value string");
auto y = parser.add<int>("myInt", "some description", true, 13);
if (!parser.parse(argc, argv))
parser.help();
std::cout << *x << std::endl << *y << std::endl;
return 0;
}