From 05232b7487843083aaebffe3edf656300672416b Mon Sep 17 00:00:00 2001 From: Pranav Srinivas Kumar Date: Wed, 21 Sep 2022 18:09:25 -0700 Subject: [PATCH] Started adding samples --- CMakeLists.txt | 3 ++ clang_format.bash | 2 +- samples/CMakeLists.txt | 33 +++++++++++++++++++ samples/git_subcommands.cpp | 65 +++++++++++++++++++++++++++++++++++++ samples/square_a_number.cpp | 28 ++++++++++++++++ test/CMakeLists.txt | 10 +++--- 6 files changed, 135 insertions(+), 6 deletions(-) create mode 100644 samples/CMakeLists.txt create mode 100644 samples/git_subcommands.cpp create mode 100644 samples/square_a_number.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2e8d4b4..8b02105 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -28,6 +28,9 @@ target_include_directories(argparse INTERFACE $ $) +if(ARGPARSE_BUILD_SAMPLES) + add_subdirectory(samples) +endif() if(ARGPARSE_BUILD_TESTS) add_subdirectory(test) diff --git a/clang_format.bash b/clang_format.bash index fa26930..518de18 100755 --- a/clang_format.bash +++ b/clang_format.bash @@ -1 +1 @@ -clang-format -i include/argparse/*.hpp test/*.cpp +clang-format -i include/argparse/*.hpp test/*.cpp samples/*.cpp diff --git a/samples/CMakeLists.txt b/samples/CMakeLists.txt new file mode 100644 index 0000000..3272234 --- /dev/null +++ b/samples/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 3.6) +project(argparse_samples) + +if(MSVC) + # Force to always compile with W4 + if(CMAKE_CXX_FLAGS MATCHES "/W[0-4]") + string(REGEX REPLACE "/W[0-4]" "/W4" CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS}") + else() + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} /W4") + endif() +elseif(CMAKE_COMPILER_IS_GNUCC OR CMAKE_COMPILER_IS_GNUCXX) + # Update if necessary + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -Wno-long-long -pedantic -Wsign-conversion -Wshadow -Wconversion") +endif() + +if(NOT CMAKE_BUILD_TYPE) + set(CMAKE_BUILD_TYPE Release) +endif() + +# Disable deprecation for windows +if (WIN32) + add_compile_definitions(_CRT_SECURE_NO_WARNINGS) +endif() + +function(add_sample NAME) + ADD_EXECUTABLE(ARGPARSE_SAMPLE_${NAME} ${NAME}.cpp) + INCLUDE_DIRECTORIES("../include" ".") + TARGET_LINK_LIBRARIES(ARGPARSE_SAMPLE_${NAME} PRIVATE argparse::argparse) + set_target_properties(ARGPARSE_SAMPLE_${NAME} PROPERTIES OUTPUT_NAME ${NAME}) +endfunction() + +add_sample(git_subcommands) +add_sample(square_a_number) \ No newline at end of file diff --git a/samples/git_subcommands.cpp b/samples/git_subcommands.cpp new file mode 100644 index 0000000..f88f05c --- /dev/null +++ b/samples/git_subcommands.cpp @@ -0,0 +1,65 @@ +#include + +int main(int argc, char *argv[]) { + argparse::ArgumentParser program("git"); + + // git add subparser + argparse::ArgumentParser add_command("add"); + add_command.add_description("Add file contents to the index"); + add_command.add_argument("files") + .help("Files to add content from. Fileglobs (e.g. *.c) can be given to " + "add all matching files.") + .remaining(); + + // git commit subparser + argparse::ArgumentParser commit_command("commit"); + commit_command.add_description("Record changes to the repository"); + commit_command.add_argument("-a", "--all") + .help("Tell the command to automatically stage files that have been " + "modified and deleted.") + .default_value(false) + .implicit_value(true); + + commit_command.add_argument("-m", "--message") + .help("Use the given as the commit message."); + + // git cat-file subparser + argparse::ArgumentParser catfile_command("cat-file"); + catfile_command.add_description( + "Provide content or type and size information for repository objects"); + catfile_command.add_argument("-t").help( + "Instead of the content, show the object type identified by ."); + + catfile_command.add_argument("-p").help( + "Pretty-print the contents of based on its type."); + + // git submodule subparser + argparse::ArgumentParser submodule_command("submodule"); + submodule_command.add_description("Initialize, update or inspect submodules"); + argparse::ArgumentParser submodule_update_command("update"); + submodule_update_command.add_description( + "Update the registered submodules to match what the superproject " + "expects"); + submodule_update_command.add_argument("--init") + .default_value(false) + .implicit_value(true); + submodule_update_command.add_argument("--recursive") + .default_value(false) + .implicit_value(true); + submodule_command.add_subparser(submodule_update_command); + + program.add_subparser(add_command); + program.add_subparser(commit_command); + program.add_subparser(catfile_command); + program.add_subparser(submodule_command); + + try { + program.parse_args(argc, argv); + } catch (const std::runtime_error &err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + // Use arguments +} diff --git a/samples/square_a_number.cpp b/samples/square_a_number.cpp new file mode 100644 index 0000000..646ddc3 --- /dev/null +++ b/samples/square_a_number.cpp @@ -0,0 +1,28 @@ +#include + +int main(int argc, char *argv[]) { + argparse::ArgumentParser program("main"); + + program.add_argument("square") + .help("display the square of a given number") + .scan<'i', int>(); + + program.add_argument("--verbose").default_value(false).implicit_value(true); + + try { + program.parse_args(argc, argv); + } catch (const std::runtime_error &err) { + std::cerr << err.what() << std::endl; + std::cerr << program; + std::exit(1); + } + + int input = program.get("square"); + + if (program["--verbose"] == true) { + std::cout << "The square of " << input << " is " << (input * input) + << std::endl; + } else { + std::cout << (input * input) << std::endl; + } +} diff --git a/test/CMakeLists.txt b/test/CMakeLists.txt index b228feb..c7a5657 100644 --- a/test/CMakeLists.txt +++ b/test/CMakeLists.txt @@ -1,5 +1,5 @@ cmake_minimum_required(VERSION 3.6) -project(argparse) +project(argparse_tests) if(MSVC) # Force to always compile with W4 @@ -54,10 +54,10 @@ file(GLOB ARGPARSE_TEST_SOURCES set_source_files_properties(main.cpp PROPERTIES COMPILE_DEFINITIONS DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN) -ADD_EXECUTABLE(ARGPARSE ${ARGPARSE_TEST_SOURCES}) +ADD_EXECUTABLE(ARGPARSE_TESTS ${ARGPARSE_TEST_SOURCES}) INCLUDE_DIRECTORIES("../include" ".") -set_target_properties(ARGPARSE PROPERTIES OUTPUT_NAME tests) -set_property(TARGET ARGPARSE PROPERTY CXX_STANDARD 17) +set_target_properties(ARGPARSE_TESTS PROPERTIES OUTPUT_NAME tests) +set_property(TARGET ARGPARSE_TESTS PROPERTY CXX_STANDARD 17) # Set ${PROJECT_NAME} as the startup project -set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ARGPARSE) +set_property(DIRECTORY ${CMAKE_CURRENT_SOURCE_DIR} PROPERTY VS_STARTUP_PROJECT ARGPARSE_TESTS)