Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 75 additions & 0 deletions .github/workflows/cmake-multi-platform.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
# This starter workflow is for a CMake project running on multiple platforms. There is a different starter workflow if you just want a single platform.
# See: https://github.com/actions/starter-workflows/blob/main/ci/cmake-single-platform.yml
name: CMake on multiple platforms

on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]

jobs:
build:
runs-on: ${{ matrix.os }}

strategy:
# Set fail-fast to false to ensure that feedback is delivered for all matrix combinations. Consider changing this to true when your workflow is stable.
fail-fast: false

# Set up a matrix to run the following 3 configurations:
# 1. <Windows, Release, latest MSVC compiler toolchain on the default runner image, default generator>
# 2. <Linux, Release, latest GCC compiler toolchain on the default runner image, default generator>
# 3. <Linux, Release, latest Clang compiler toolchain on the default runner image, default generator>
#
# To add more build types (Release, Debug, RelWithDebInfo, etc.) customize the build_type list.
matrix:
os: [ubuntu-latest, windows-latest]
build_type: [Release]
c_compiler: [gcc, clang, cl]
include:
- os: windows-latest
c_compiler: cl
cpp_compiler: cl
- os: ubuntu-latest
c_compiler: gcc
cpp_compiler: g++
- os: ubuntu-latest
c_compiler: clang
cpp_compiler: clang++
exclude:
- os: windows-latest
c_compiler: gcc
- os: windows-latest
c_compiler: clang
- os: ubuntu-latest
c_compiler: cl

steps:
- uses: actions/checkout@v4

- name: Set reusable strings
# Turn repeated input strings (such as the build output directory) into step outputs. These step outputs can be used throughout the workflow file.
id: strings
shell: bash
run: |
echo "build-output-dir=${{ github.workspace }}/build" >> "$GITHUB_OUTPUT"

- name: Configure CMake
# Configure CMake in a 'build' subdirectory. `CMAKE_BUILD_TYPE` is only required if you are using a single-configuration generator such as make.
# See https://cmake.org/cmake/help/latest/variable/CMAKE_BUILD_TYPE.html?highlight=cmake_build_type
run: >
cmake -B ${{ steps.strings.outputs.build-output-dir }}
-DCMAKE_CXX_COMPILER=${{ matrix.cpp_compiler }}
-DCMAKE_C_COMPILER=${{ matrix.c_compiler }}
-DCMAKE_BUILD_TYPE=${{ matrix.build_type }}
-S ${{ github.workspace }}

- name: Build
# Build your program with the given configuration. Note that --config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
run: cmake --build ${{ steps.strings.outputs.build-output-dir }} --config ${{ matrix.build_type }}

- name: Test
working-directory: ${{ steps.strings.outputs.build-output-dir }}
# Execute tests defined by the CMake configuration. Note that --build-config is needed because the default Windows generator is a multi-config generator (Visual Studio generator).
# See https://cmake.org/cmake/help/latest/manual/ctest.1.html for more detail
run: ctest --build-config ${{ matrix.build_type }}
27 changes: 2 additions & 25 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,31 +3,8 @@ project(argsparse)

include(${CMAKE_CURRENT_LIST_DIR}/cmake/argsparse-lib.cmake)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/test)

add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/example)

# set(CMAKE_POLICY_DEFAULT_CMP0077 NEW)

# set(CMAKE_CXX_STANDARD 17)
# set(CMAKE_CXX_STANDARD_REQUIRED YES)

# include(${CMAKE_CURRENT_LIST_DIR}/cmake/googletest.cmake)

# include(${CMAKE_CURRENT_LIST_DIR}/cmake/argsparse-lib.cmake)
enable_testing()

# # Main source directory
# include_directories(${CMAKE_CURRENT_LIST_DIR}/.)

# list(APPEND TestFiles
# ${CMAKE_CURRENT_LIST_DIR}/test/argsparseTests.cpp
# )

# # Test executable
# add_executable(${PROJECT_NAME}-test ${CMAKE_CURRENT_LIST_DIR}/test/TestMain.cpp ${TestFiles})
# target_link_libraries(${PROJECT_NAME}-test ${PROJECT_NAME}-lib gtest gmock_main)
# target_include_directories(${PROJECT_NAME}-test PUBLIC ${googletest_SOURCE_DIR}/googlemock/include)

# include(CTest)

# add_test(NAME ${PROJECT_NAME}-test COMMAND ${PROJECT_NAME}-test)
add_subdirectory(${CMAKE_CURRENT_LIST_DIR}/test)
134 changes: 68 additions & 66 deletions argsparse.c
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,16 @@
*
*/

#include "iterate.h"
#include "internal_funcs.h"
#include "iterate.h"

#include <float.h>
#include <getopt.h>
#include <malloc.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include <getopt.h>

ARG_DATA_HANDLE argsparse_create(const char* title)
{
Expand All @@ -38,22 +39,6 @@ void argsparse_free(ARG_DATA_HANDLE handle)
}
}

ARG_ARGUMENT_HANDLE argsparse_create_argument_with_value(ARG_TYPE type, const char* name, const char* desc, ARG_VALUE* value)
{
ARG_ARGUMENT_HANDLE p = create_argument(name, desc);
p->type = type;
if (value)
{
memcpy(&p->value, value, sizeof(ARG_VALUE));
// if no pointer given point to the unused ARG_VALUE memory
if (type == ARGSPARSE_TYPE_FLAG && p->value.flagptr == NULL)
{
p->value.flagptr = (int*)(&p->value.flagptr + 1);
}
}
return p;
}

char* argsparse_get_shortopts(ARG_DATA_HANDLE handle)
{
return (handle) ? handle->shortopts : NULL;
Expand Down Expand Up @@ -81,87 +66,54 @@ int argsparse_argument_count(ARG_DATA_HANDLE handle)
return ret;
}

ARG_ERROR argsparse_put_argument(ARG_DATA_HANDLE handle, ARG_ARGUMENT_HANDLE* href)
ARG_ERROR argsparse_add(ARG_DATA_HANDLE handle, const char* name, const char* description, ARG_TYPE type, const ARG_VALUE* value)
{
ARG_ARGUMENT_HANDLE exists = iterate_arguments_return_on_zero(handle, predicate_compare_name, (*href)->name);
if (exists)
{
// free if not the same
if (exists != *href)
{
free_argument(href);
}
return ERROR_EXISTS;
}

if (handle->count >= ARGSPARSE_MAX_ARGS)
{
free_argument(href);
return ERROR_MAX_ARGS;
}
handle->count++;
HARGPARSE_ARG_LINKED new_link = calloc(1, sizeof(t_argparse_argument_linked));
new_link->argument = *href;
*href = NULL;

generate_short_name(handle, new_link->argument);
if (handle->arguments)
{
HARGPARSE_ARG_LINKED next = handle->arguments;
while (next->next)
{
next = next->next;
}
next->next = new_link;
}
else
handle->arguments = new_link;

return ERROR_NONE;
ARG_ARGUMENT_HANDLE h = create_argument(type, name, description, value);
return put_argument(handle, &h);
}

ARG_ERROR argsparse_add_help(ARG_DATA_HANDLE handle)
{
ARG_ARGUMENT_HANDLE p = argsparse_create_argument_with_value(ARGSPARSE_TYPE_NONE, "help", "Print this message", NULL);
return argsparse_put_argument(handle, &p);
ARG_ARGUMENT_HANDLE p = create_argument(ARGSPARSE_TYPE_NONE, "help", "Print this message", NULL);
return put_argument(handle, &p);
}

ARG_ERROR argsparse_add_int(ARG_DATA_HANDLE handle, const char* name, const char* description, int value)
{
ARG_VALUE argvalue;
argvalue.intvalue = value;

ARG_ARGUMENT_HANDLE p = argsparse_create_argument_with_value(ARGSPARSE_TYPE_INT, name, description, &argvalue);
ARG_ARGUMENT_HANDLE p = create_argument(ARGSPARSE_TYPE_INT, name, description, &argvalue);

return argsparse_put_argument(handle, &p);
return put_argument(handle, &p);
}

ARG_ERROR argsparse_add_double(ARG_DATA_HANDLE handle, const char* name, const char* description, double value)
{
ARG_VALUE argvalue;
argvalue.doublevalue = value;

ARG_ARGUMENT_HANDLE p = argsparse_create_argument_with_value(ARGSPARSE_TYPE_DOUBLE, name, description, &argvalue);
ARG_ARGUMENT_HANDLE p = create_argument(ARGSPARSE_TYPE_DOUBLE, name, description, &argvalue);

return argsparse_put_argument(handle, &p);
return put_argument(handle, &p);
}

ARG_ERROR argsparse_add_cstr(ARG_DATA_HANDLE handle, const char* name, const char* description, const char* value)
{
ARG_VALUE argvalue;
copy_to_argument_string(argvalue.stringvalue, value);

ARG_ARGUMENT_HANDLE p = argsparse_create_argument_with_value(ARGSPARSE_TYPE_STRING, name, description, &argvalue);
return argsparse_put_argument(handle, &p);
ARG_ARGUMENT_HANDLE p = create_argument(ARGSPARSE_TYPE_STRING, name, description, &argvalue);
return put_argument(handle, &p);
}

ARG_ERROR argsparse_add_flag(ARG_DATA_HANDLE handle, const char* name, const char* description, int value, int* ptr_to_value)
{
ARG_VALUE argvalue = {0, };
argvalue.flagptr = ptr_to_value;
ARG_ARGUMENT_HANDLE p = argsparse_create_argument_with_value(ARGSPARSE_TYPE_FLAG, name, description, &argvalue);
ARG_ARGUMENT_HANDLE p = create_argument(ARGSPARSE_TYPE_FLAG, name, description, &argvalue);
p->flag_init.flagvalue = value;
return argsparse_put_argument(handle, &p);
return put_argument(handle, &p);
}

int argsparse_parse_args(ARG_DATA_HANDLE handle, char* const *argv, int argc)
Expand Down Expand Up @@ -280,14 +232,64 @@ void argsparse_show_arguments(ARG_DATA_HANDLE handle)
// Internal functions //
////////////////////////

static ARG_ARGUMENT_HANDLE create_argument(const char* name, const char* desc)
static ARG_ARGUMENT_HANDLE create_argument(ARG_TYPE type, const char* name, const char* desc, const ARG_VALUE* value)
{
ARG_ARGUMENT_HANDLE p = calloc(1, sizeof(argsparse_argument_t));
copy_to_argument_string(p->name, name);
copy_to_argument_string(p->description, desc);

p->type = type;
if (value)
{
memcpy(&p->value, value, sizeof(ARG_VALUE));
// if no pointer given point to the unused ARG_VALUE memory
if (type == ARGSPARSE_TYPE_FLAG && p->value.flagptr == NULL)
{
p->value.flagptr = (int*)(&p->value.flagptr + 1);
}
}
return p;
}

static ARG_ERROR put_argument(ARG_DATA_HANDLE handle, ARG_ARGUMENT_HANDLE* href)
{
ARG_ARGUMENT_HANDLE exists = iterate_arguments_return_on_zero(handle, predicate_compare_name, (*href)->name);
if (exists)
{
// free if not the same
if (exists != *href)
{
free_argument(href);
}
return ERROR_EXISTS;
}

if (handle->count >= ARGSPARSE_MAX_ARGS)
{
free_argument(href);
return ERROR_MAX_ARGS;
}
handle->count++;
HARGPARSE_ARG_LINKED new_link = calloc(1, sizeof(t_argparse_argument_linked));
new_link->argument = *href;
*href = NULL;

generate_short_name(handle, new_link->argument);
if (handle->arguments)
{
HARGPARSE_ARG_LINKED next = handle->arguments;
while (next->next)
{
next = next->next;
}
next->next = new_link;
}
else
handle->arguments = new_link;

return ERROR_NONE;
}

static HARGPARSE_ARG_LINKED free_linked_argument(HARGPARSE_ARG_LINKED linked)
{
HARGPARSE_ARG_LINKED next = NULL;
Expand Down
Loading