diff --git a/.gitignore b/.gitignore index 9129724..205cef5 100644 --- a/.gitignore +++ b/.gitignore @@ -1,4 +1,5 @@ build/ +build-*/ *.swp *.*~ diff --git a/.travis.yml b/.travis.yml index 74c5c27..1ed4146 100644 --- a/.travis.yml +++ b/.travis.yml @@ -3,12 +3,6 @@ sudo: false language: - cpp -script: - - cmake -H. -Bbuild -DCMAKE_C_COMPILER=$C_COMPILER -DCMAKE_CXX_COMPILER=$CXX_COMPILER - - cd build - - cmake --build . - - ctest - matrix: include: - os: linux @@ -43,6 +37,19 @@ matrix: - C_COMPILER=gcc-8 - CXX_COMPILER=g++-8 +install: + - mkdir /tmp/build-gtest && cd /tmp/build-gtest + - cmake /usr/src/gtest + - make + - sudo cp libgtest* /usr/lib/ + - cd - + +script: + - cmake -H. -Bbuild -DCMAKE_C_COMPILER=$C_COMPILER -DCMAKE_CXX_COMPILER=$CXX_COMPILER + - cd build + - cmake --build . + - ctest + notifications: email: false diff --git a/CMakeLists.txt b/CMakeLists.txt index ca0246f..66adbda 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -1,20 +1,53 @@ cmake_minimum_required(VERSION 3.1) -project(ArgParser) +project(CppArgparse VERSION 0.1.0) + +option( CPPARGPARSE_BUILD_EXAMPLES "build examples" ON ) +option( CPPARGPARSE_BUILD_TESTS "build tests" ON ) +option( CPPARGPARSE_INSTALL "enable library installation" ON ) +option( CPPARGPARSE_PEDANTIC "treat warnings as errors" OFF ) set(CMAKE_CXX_STANDARD 17) +set(INCLUDE_INSTALL_DIR include/ ) +set(LIB_INSTALL_DIR lib/ ) +set(CONFIG_INSTALL_DIR ${CMAKE_INSTALL_LIBDIR}/cmake/CppArgparse) +set(CPPARGPARSE_STATIC_NAME argparse-s) add_subdirectory(src) -add_subdirectory(example) +add_subdirectory(util) + +if( CPPARGPARSE_BUILD_EXAMPLES ) + add_subdirectory(example) +endif() + +if( CPPARGPARSE_BUILD_TESTS ) + enable_testing() + add_subdirectory(test) + add_subdirectory(test/staticlib) + add_subdirectory(test/headerlib) +endif() + +if (CPPARGPARSE_INSTALL) + include(GNUInstallDirs) + add_subdirectory(include) + + include(CMakePackageConfigHelpers) + + configure_package_config_file(cmake/CppArgparseConfig.cmake.in + ${CMAKE_CURRENT_BINARY_DIR}/CppArgparseConfig.cmake + INSTALL_DESTINATION ${LIB_INSTALL_DIR}/CppArgparse/cmake + PATH_VARS INCLUDE_INSTALL_DIR LIB_INSTALL_DIR ) + write_basic_package_version_file( + ${CMAKE_CURRENT_BINARY_DIR}/CppArgparseConfigVersion.cmake + VERSION ${PROJECT_VERSION} + COMPATIBILITY AnyNewerVersion + ) -include(cmake/googletest/googletest.cmake) -fetch_googletest( - ${PROJECT_SOURCE_DIR}/cmake/googletest - ${PROJECT_BINARY_DIR}/googletest - ) + install(FILES + ${CMAKE_CURRENT_BINARY_DIR}/CppArgparseConfig.cmake + ${CMAKE_CURRENT_BINARY_DIR}/CppArgparseConfigVersion.cmake + DESTINATION ${CONFIG_INSTALL_DIR} + ) -enable_testing() -add_subdirectory(test) -add_subdirectory(test/staticlib) -add_subdirectory(test/headerlib) +endif() diff --git a/cmake/CppArgparseConfig.cmake.in b/cmake/CppArgparseConfig.cmake.in new file mode 100644 index 0000000..4d83794 --- /dev/null +++ b/cmake/CppArgparseConfig.cmake.in @@ -0,0 +1,4 @@ +@PACKAGE_INIT@ + +set_and_check(CPPARGPARSE_INCLUDE_DIR "@PACKAGE_INCLUDE_INSTALL_DIR@") +set_and_check(CPPARGPARSE_LIB_DIR "@PACKAGE_LIB_INSTALL_DIR@") diff --git a/cmake/googletest/README.md b/cmake/googletest/README.md deleted file mode 100644 index db79da2..0000000 --- a/cmake/googletest/README.md +++ /dev/null @@ -1,30 +0,0 @@ -The cmake files in this directory are copied from https://github.com/bast/gtest-demo -Where they are published under the following license (2018-12-17): - -Copyright (c) 2015-2018, Radovan Bast -All rights reserved. - -Redistribution and use in source and binary forms, with or without -modification, are permitted provided that the following conditions are met: - -* Redistributions of source code must retain the above copyright notice, this - list of conditions and the following disclaimer. - -* Redistributions in binary form must reproduce the above copyright notice, - this list of conditions and the following disclaimer in the documentation - and/or other materials provided with the distribution. - -* Neither the name of gtest-demo nor the names of its - contributors may be used to endorse or promote products derived from - this software without specific prior written permission. - -THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" -AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE -IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE -DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE -FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL -DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR -SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER -CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, -OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE -OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/cmake/googletest/googletest-download.cmake b/cmake/googletest/googletest-download.cmake deleted file mode 100644 index 4926683..0000000 --- a/cmake/googletest/googletest-download.cmake +++ /dev/null @@ -1,20 +0,0 @@ -# code copied from https://crascit.com/2015/07/25/cmake-gtest/ -cmake_minimum_required(VERSION 3.5 FATAL_ERROR) - -project(googletest-download NONE) - -include(ExternalProject) - -ExternalProject_Add( - googletest - SOURCE_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-src" - BINARY_DIR "@GOOGLETEST_DOWNLOAD_ROOT@/googletest-build" - GIT_REPOSITORY - https://github.com/google/googletest.git - GIT_TAG - release-1.8.0 - CONFIGURE_COMMAND "" - BUILD_COMMAND "" - INSTALL_COMMAND "" - TEST_COMMAND "" - ) diff --git a/cmake/googletest/googletest.cmake b/cmake/googletest/googletest.cmake deleted file mode 100644 index 5ca7090..0000000 --- a/cmake/googletest/googletest.cmake +++ /dev/null @@ -1,32 +0,0 @@ -# the following code to fetch googletest -# is inspired by and adapted after https://crascit.com/2015/07/25/cmake-gtest/ -# download and unpack googletest at configure time - -macro(fetch_googletest _download_module_path _download_root) - set(GOOGLETEST_DOWNLOAD_ROOT ${_download_root}) - configure_file( - ${_download_module_path}/googletest-download.cmake - ${_download_root}/CMakeLists.txt - @ONLY - ) - unset(GOOGLETEST_DOWNLOAD_ROOT) - - execute_process( - COMMAND - "${CMAKE_COMMAND}" -G "${CMAKE_GENERATOR}" . - WORKING_DIRECTORY - ${_download_root} - ) - execute_process( - COMMAND - "${CMAKE_COMMAND}" --build . - WORKING_DIRECTORY - ${_download_root} - ) - - # adds the targers: gtest, gtest_main, gmock, gmock_main - add_subdirectory( - ${_download_root}/googletest-src - ${_download_root}/googletest-build - ) -endmacro() diff --git a/example/CMakeLists.txt b/example/CMakeLists.txt index 8d4de43..1600310 100644 --- a/example/CMakeLists.txt +++ b/example/CMakeLists.txt @@ -1,5 +1,6 @@ include_directories( ../include ) +set ( CPPARGPARSE_EXAMPLE_LIB ${CPPARGPARSE_STATIC_NAME} ) # The library is currently header-only. set(PARSER_SOURCES @@ -9,24 +10,48 @@ set(PARSER_SOURCES add_executable( basic basic.cpp ) +target_link_libraries( basic + ${CMAKE_BINARY_DIR}/src/libargparse-s.a + ) +add_dependencies( basic ${CPPARGPARSE_EXAMPLE_LIB} ) add_executable( basic_action basic_action.cpp ) +target_link_libraries( basic_action + ${CMAKE_BINARY_DIR}/src/libargparse-s.a + ) +add_dependencies( basic_action ${CPPARGPARSE_EXAMPLE_LIB} ) add_executable( basic_struct basic_struct.cpp ) +target_link_libraries( basic_struct + ${CMAKE_BINARY_DIR}/src/libargparse-s.a + ) +add_dependencies( basic_struct ${CPPARGPARSE_EXAMPLE_LIB} ) add_executable( basic_command basic_command.cpp ) +target_link_libraries( basic_command + ${CMAKE_BINARY_DIR}/src/libargparse-s.a + ) +add_dependencies( basic_command ${CPPARGPARSE_EXAMPLE_LIB} ) add_executable( example1 example1.cpp ) +target_link_libraries( example1 + ${CMAKE_BINARY_DIR}/src/libargparse-s.a + ) +add_dependencies( example1 ${CPPARGPARSE_EXAMPLE_LIB} ) add_executable( example2 example2.cpp ) +target_link_libraries( example2 + ${CMAKE_BINARY_DIR}/src/libargparse-s.a + ) +add_dependencies( example2 ${CPPARGPARSE_EXAMPLE_LIB} ) diff --git a/include/CMakeLists.txt b/include/CMakeLists.txt new file mode 100644 index 0000000..10f28b9 --- /dev/null +++ b/include/CMakeLists.txt @@ -0,0 +1,30 @@ + +add_custom_command( + OUTPUT ${CMAKE_CURRENT_BINARY_DIR}/fake_create_headers.cpp + DEPENDS instutil cppargparse/argparse.h + COMMENT "Preparing library headers for publishing" + + COMMAND ${CMAKE_COMMAND} -E make_directory ${CMAKE_CURRENT_BINARY_DIR}/cppargparse + + COMMAND ${CMAKE_COMMAND} -E copy_directory + ${CMAKE_SOURCE_DIR}/src ${CMAKE_CURRENT_BINARY_DIR}/cppargparse/inc + + COMMAND ${CMAKE_COMMAND} -E remove + ${CMAKE_CURRENT_BINARY_DIR}/cppargparse/inc/*.cpp + ${CMAKE_CURRENT_BINARY_DIR}/cppargparse/inc/*.txt + ${CMAKE_CURRENT_BINARY_DIR}/cppargparse/inc/*_impl.h + + COMMAND sed -e "s#\.\./\.\./src#inc#g" + ${CMAKE_CURRENT_SOURCE_DIR}/cppargparse/argparse.h + > ${CMAKE_CURRENT_BINARY_DIR}/cppargparse/argparse.h + + COMMAND ${CMAKE_COMMAND} -E echo 'int main(){ return 0\; }' > fake_create_headers.cpp + ) + +add_executable(fake_create_headers + ${CMAKE_CURRENT_BINARY_DIR}/fake_create_headers.cpp + ) + +install(DIRECTORY ${CMAKE_BINARY_DIR}/include/cppargparse + DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}) + diff --git a/include/cppargparse/argparse-h.h b/include/cppargparse/argparse-h.h new file mode 100644 index 0000000..420d577 --- /dev/null +++ b/include/cppargparse/argparse-h.h @@ -0,0 +1,30 @@ +// Copyright (c) 2019 Marko Mahnič +// License: MPL2. See LICENSE in the root of the project. + +#pragma once + +#include "../../src/argparser.h" + +#define CPPARGPARSE_INLINE inline + +#include "../../src/argdescriber_impl.h" +#include "../../src/argparser_impl.h" +#include "../../src/argumentstream_impl.h" +#include "../../src/command_impl.h" +#include "../../src/commandconfig_impl.h" +#include "../../src/convert_impl.h" +#include "../../src/environment_impl.h" +#include "../../src/group_impl.h" +#include "../../src/groupconfig_impl.h" +#include "../../src/helpformatter_impl.h" +#include "../../src/option_impl.h" +#include "../../src/optionconfig_impl.h" +#include "../../src/optionsorter_impl.h" +#include "../../src/parser_impl.h" +#include "../../src/parserconfig_impl.h" +#include "../../src/parserdefinition_impl.h" +#include "../../src/parseresult_impl.h" +#include "../../src/value_impl.h" +#include "../../src/writer_impl.h" + +#undef CPPARGPARSE_INLINE diff --git a/include/cppargparse/argparse-s.h b/include/cppargparse/argparse-s.h deleted file mode 100644 index ac68973..0000000 --- a/include/cppargparse/argparse-s.h +++ /dev/null @@ -1,7 +0,0 @@ -// Copyright (c) 2019 Marko Mahnič -// License: MPL2. See LICENSE in the root of the project. - -#pragma once - -#include "../../src/argparser.h" -#include "../../src/exceptions.h" diff --git a/include/cppargparse/argparse.h b/include/cppargparse/argparse.h index 420d577..ac68973 100644 --- a/include/cppargparse/argparse.h +++ b/include/cppargparse/argparse.h @@ -4,27 +4,4 @@ #pragma once #include "../../src/argparser.h" - -#define CPPARGPARSE_INLINE inline - -#include "../../src/argdescriber_impl.h" -#include "../../src/argparser_impl.h" -#include "../../src/argumentstream_impl.h" -#include "../../src/command_impl.h" -#include "../../src/commandconfig_impl.h" -#include "../../src/convert_impl.h" -#include "../../src/environment_impl.h" -#include "../../src/group_impl.h" -#include "../../src/groupconfig_impl.h" -#include "../../src/helpformatter_impl.h" -#include "../../src/option_impl.h" -#include "../../src/optionconfig_impl.h" -#include "../../src/optionsorter_impl.h" -#include "../../src/parser_impl.h" -#include "../../src/parserconfig_impl.h" -#include "../../src/parserdefinition_impl.h" -#include "../../src/parseresult_impl.h" -#include "../../src/value_impl.h" -#include "../../src/writer_impl.h" - -#undef CPPARGPARSE_INLINE +#include "../../src/exceptions.h" diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 18803e6..26e3b98 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -1,4 +1,17 @@ -add_library(argparse-s STATIC +add_library(${CPPARGPARSE_STATIC_NAME} STATIC argparser.cpp ) +if( CPPARGPARSE_PEDANTIC ) + target_compile_options(${CPPARGPARSE_STATIC_NAME} PRIVATE + $<$:-Werror -Wall> + $<$:/WX /permissive- /Za> + ) +endif() + +if (CPPARGPARSE_INSTALL) + install( TARGETS ${CPPARGPARSE_STATIC_NAME} + ARCHIVE DESTINATION lib + LIBRARY DESTINATION lib + ) +endif() diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index 5eeccf4..93073ac 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,9 @@ include_directories( ../include ) +set ( CPPARGPARSE_TEST_LIB ${CPPARGPARSE_STATIC_NAME} ) + +find_package( GTest REQUIRED ) +find_package( Threads REQUIRED ) # The library is currently header-only. set(PARSER_SOURCES @@ -16,7 +20,6 @@ add_executable( runUnitTests group_t.cpp command_t.cpp action_t.cpp - helpwriter_t.cpp help_t.cpp commandhelp_t.cpp filesystemarguments_t.cpp @@ -27,9 +30,11 @@ add_executable( runUnitTests ) target_link_libraries( runUnitTests - gtest_main + ${GTEST_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_BINARY_DIR}/src/libargparse-s.a ) +add_dependencies( runUnitTests ${CPPARGPARSE_TEST_LIB} ) add_executable( utilityTests runtest.cpp @@ -38,8 +43,10 @@ add_executable( utilityTests ) target_link_libraries( utilityTests - gtest_main + ${GTEST_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} ) +add_dependencies( utilityTests ${CPPARGPARSE_TEST_LIB} ) add_test( NAME diff --git a/test/action_t.cpp b/test/action_t.cpp index a987fc3..c6cfe2e 100644 --- a/test/action_t.cpp +++ b/test/action_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2018, 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include diff --git a/test/argparser_t.cpp b/test/argparser_t.cpp index 847a95a..eb10bfc 100644 --- a/test/argparser_t.cpp +++ b/test/argparser_t.cpp @@ -4,7 +4,7 @@ #include "testutil.h" #include "vectors.h" -#include +#include #include #include diff --git a/test/argumentstream_t.cpp b/test/argumentstream_t.cpp index c6a2b12..9d1ed0d 100644 --- a/test/argumentstream_t.cpp +++ b/test/argumentstream_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2018, 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include diff --git a/test/command_t.cpp b/test/command_t.cpp index 4ee09d0..97734b0 100644 --- a/test/command_t.cpp +++ b/test/command_t.cpp @@ -3,7 +3,7 @@ #include "testutil.h" -#include +#include #include #include diff --git a/test/commandhelp_t.cpp b/test/commandhelp_t.cpp index 7394cf5..849c547 100644 --- a/test/commandhelp_t.cpp +++ b/test/commandhelp_t.cpp @@ -3,7 +3,7 @@ #include "testutil.h" -#include +#include #include #include diff --git a/test/convert_t.cpp b/test/convert_t.cpp index 54a483b..81c815f 100644 --- a/test/convert_t.cpp +++ b/test/convert_t.cpp @@ -3,7 +3,7 @@ #include "vectors.h" -#include +#include #include #include diff --git a/test/filesystemarguments_t.cpp b/test/filesystemarguments_t.cpp index a6c6529..6c9b7c2 100644 --- a/test/filesystemarguments_t.cpp +++ b/test/filesystemarguments_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include #include diff --git a/test/group_t.cpp b/test/group_t.cpp index d79d122..a7b96b4 100644 --- a/test/group_t.cpp +++ b/test/group_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include #include diff --git a/test/headerlib/CMakeLists.txt b/test/headerlib/CMakeLists.txt index 6e2c97b..77c8e4e 100644 --- a/test/headerlib/CMakeLists.txt +++ b/test/headerlib/CMakeLists.txt @@ -1,6 +1,9 @@ include_directories( ../../include ) +find_package( GTest REQUIRED ) +find_package( Threads REQUIRED ) + add_executable( headerLibraryTest runtest.cpp ../testutil.cpp @@ -8,7 +11,8 @@ add_executable( headerLibraryTest ) target_link_libraries( headerLibraryTest - gtest_main + ${GTEST_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} ) add_test( diff --git a/test/headerlib/basic_command_t.cpp b/test/headerlib/basic_command_t.cpp index 95c3988..e8cadde 100644 --- a/test/headerlib/basic_command_t.cpp +++ b/test/headerlib/basic_command_t.cpp @@ -1,7 +1,7 @@ #include "../testutil.h" #include -#include +#include #include #include #include diff --git a/test/help_t.cpp b/test/help_t.cpp index 044e449..cc8e703 100644 --- a/test/help_t.cpp +++ b/test/help_t.cpp @@ -3,7 +3,7 @@ #include "testutil.h" -#include +#include #include #include diff --git a/test/helpwriter_t.cpp b/test/helpwriter_t.cpp index ceeee66..480395c 100644 --- a/test/helpwriter_t.cpp +++ b/test/helpwriter_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2018, 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include diff --git a/test/negativenumber_t.cpp b/test/negativenumber_t.cpp index 17c3307..93d5322 100644 --- a/test/negativenumber_t.cpp +++ b/test/negativenumber_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2018, 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include #include diff --git a/test/number_t.cpp b/test/number_t.cpp index bbf45ec..7555f5e 100644 --- a/test/number_t.cpp +++ b/test/number_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2018, 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include #include diff --git a/test/optionfactory_t.cpp b/test/optionfactory_t.cpp index 753e7ca..f717225 100644 --- a/test/optionfactory_t.cpp +++ b/test/optionfactory_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2018, 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include diff --git a/test/staticlib/CMakeLists.txt b/test/staticlib/CMakeLists.txt index d0351a8..18baba7 100644 --- a/test/staticlib/CMakeLists.txt +++ b/test/staticlib/CMakeLists.txt @@ -1,5 +1,9 @@ include_directories( ../../include ) +set ( CPPARGPARSE_TEST_LIB ${CPPARGPARSE_STATIC_NAME} ) + +find_package( GTest REQUIRED ) +find_package( Threads REQUIRED ) add_executable( staticLibraryTest runtest.cpp @@ -8,9 +12,11 @@ add_executable( staticLibraryTest ) target_link_libraries( staticLibraryTest - gtest_main + ${GTEST_LIBRARIES} + ${CMAKE_THREAD_LIBS_INIT} ${CMAKE_BINARY_DIR}/src/libargparse-s.a ) +add_dependencies( staticLibraryTest ${CPPARGPARSE_TEST_LIB} ) add_test( NAME diff --git a/test/staticlib/basic_command_t.cpp b/test/staticlib/basic_command_t.cpp index 4edbd8c..95c3988 100644 --- a/test/staticlib/basic_command_t.cpp +++ b/test/staticlib/basic_command_t.cpp @@ -1,7 +1,7 @@ #include "../testutil.h" #include -#include +#include #include #include #include diff --git a/test/testutil.h b/test/testutil.h index b9df35c..f3857f9 100644 --- a/test/testutil.h +++ b/test/testutil.h @@ -3,7 +3,7 @@ #pragma once -#include +#include #include #include diff --git a/test/value_t.cpp b/test/value_t.cpp index d9206be..6d7fa5e 100644 --- a/test/value_t.cpp +++ b/test/value_t.cpp @@ -1,7 +1,7 @@ // Copyright (c) 2018, 2019 Marko Mahnič // License: MPL2. See LICENSE in the root of the project. -#include +#include #include diff --git a/util/CMakeLists.txt b/util/CMakeLists.txt new file mode 100644 index 0000000..20b7ce8 --- /dev/null +++ b/util/CMakeLists.txt @@ -0,0 +1,19 @@ + +include_directories( ../include ) + +set(CMAKE_CXX_STANDARD 17) + +add_executable(instutil + instutil.cpp + ) + +if( CPPARGPARSE_PEDANTIC ) + target_compile_options(${CPPARGPARSE_STATIC_NAME} PRIVATE + $<$:-Werror -Wall> + $<$:/WX /permissive- /Za> + ) +endif() + +target_link_libraries( instutil + argparse-s + ) diff --git a/util/instutil.cpp b/util/instutil.cpp new file mode 100644 index 0000000..f5b5578 --- /dev/null +++ b/util/instutil.cpp @@ -0,0 +1,62 @@ +// Copyright (c) 2018, 2019 Marko Mahnič +// License: MPL2. See LICENSE in the root of the project. + +#include +#include +#include +#include +#include +#include + +using namespace std; +using namespace argparse; + +class MainHeaderCmd : public argparse::CommandOptions +{ + std::string inputFilename; + std::string outputFilename; + +public: + using CommandOptions::CommandOptions; + void execute( const argparse::ParseResult& res ) override + { + auto fin = ifstream( inputFilename ); + auto fout = ofstream( outputFilename ); + + auto rxSrcPath = std::regex( "\\.\\./\\.\\./src/" ); + auto instPath = "inc/"; + + std::string line; + while ( std::getline( fin, line ) ) + fout << regex_replace( line, rxSrcPath, instPath ) << "\n"; + } + +protected: + void add_arguments( argparse::argument_parser& parser ) override + { + parser.add_argument( inputFilename, "input" ) + .nargs( 1 ) + .help( "The path of the source file." ); + + parser.add_argument( outputFilename, "output" ) + .nargs( 1 ) + .help( "The path of the destination file." ); + } +}; + +int main( int argc, char** argv ) +{ + auto parser = argument_parser{}; + parser.config().program( argv[0] ).description( "cpp-argparse installation utility" ); + parser.add_command( "header" ).help( "Transform the main header." ); + + auto res = parser.parse_args( argc, argv, 1 ); + if ( !res ) + return 1; + + for ( auto& pcmd : res.commands ) + if ( pcmd ) + pcmd->execute( res ); + + return 0; +}