Skip to content

Commit

Permalink
Started adding samples
Browse files Browse the repository at this point in the history
  • Loading branch information
p-ranav committed Sep 22, 2022
1 parent 3b9df0b commit 05232b7
Show file tree
Hide file tree
Showing 6 changed files with 135 additions and 6 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ target_include_directories(argparse INTERFACE
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)

if(ARGPARSE_BUILD_SAMPLES)
add_subdirectory(samples)
endif()

if(ARGPARSE_BUILD_TESTS)
add_subdirectory(test)
Expand Down
2 changes: 1 addition & 1 deletion clang_format.bash
Original file line number Diff line number Diff line change
@@ -1 +1 @@
clang-format -i include/argparse/*.hpp test/*.cpp
clang-format -i include/argparse/*.hpp test/*.cpp samples/*.cpp
33 changes: 33 additions & 0 deletions samples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -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)
65 changes: 65 additions & 0 deletions samples/git_subcommands.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
#include <argparse/argparse.hpp>

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 <msg> 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 <object>.");

catfile_command.add_argument("-p").help(
"Pretty-print the contents of <object> 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
}
28 changes: 28 additions & 0 deletions samples/square_a_number.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
#include <argparse/argparse.hpp>

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<int>("square");

if (program["--verbose"] == true) {
std::cout << "The square of " << input << " is " << (input * input)
<< std::endl;
} else {
std::cout << (input * input) << std::endl;
}
}
10 changes: 5 additions & 5 deletions test/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
cmake_minimum_required(VERSION 3.6)
project(argparse)
project(argparse_tests)

if(MSVC)
# Force to always compile with W4
Expand Down Expand Up @@ -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)

0 comments on commit 05232b7

Please sign in to comment.