mirror of
https://github.com/KeqingMoe/argparse.git
synced 2025-07-03 14:44:40 +00:00
Started adding samples
This commit is contained in:
parent
3b9df0b1e7
commit
05232b7487
@ -28,6 +28,9 @@ target_include_directories(argparse INTERFACE
|
|||||||
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
$<INSTALL_INTERFACE:${CMAKE_INSTALL_INCLUDEDIR}>
|
||||||
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
|
$<BUILD_INTERFACE:${CMAKE_CURRENT_LIST_DIR}/include>)
|
||||||
|
|
||||||
|
if(ARGPARSE_BUILD_SAMPLES)
|
||||||
|
add_subdirectory(samples)
|
||||||
|
endif()
|
||||||
|
|
||||||
if(ARGPARSE_BUILD_TESTS)
|
if(ARGPARSE_BUILD_TESTS)
|
||||||
add_subdirectory(test)
|
add_subdirectory(test)
|
||||||
|
@ -1 +1 @@
|
|||||||
clang-format -i include/argparse/*.hpp test/*.cpp
|
clang-format -i include/argparse/*.hpp test/*.cpp samples/*.cpp
|
||||||
|
33
samples/CMakeLists.txt
Normal file
33
samples/CMakeLists.txt
Normal file
@ -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
samples/git_subcommands.cpp
Normal file
65
samples/git_subcommands.cpp
Normal file
@ -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
samples/square_a_number.cpp
Normal file
28
samples/square_a_number.cpp
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
cmake_minimum_required(VERSION 3.6)
|
cmake_minimum_required(VERSION 3.6)
|
||||||
project(argparse)
|
project(argparse_tests)
|
||||||
|
|
||||||
if(MSVC)
|
if(MSVC)
|
||||||
# Force to always compile with W4
|
# Force to always compile with W4
|
||||||
@ -54,10 +54,10 @@ file(GLOB ARGPARSE_TEST_SOURCES
|
|||||||
set_source_files_properties(main.cpp
|
set_source_files_properties(main.cpp
|
||||||
PROPERTIES
|
PROPERTIES
|
||||||
COMPILE_DEFINITIONS DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)
|
COMPILE_DEFINITIONS DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN)
|
||||||
ADD_EXECUTABLE(ARGPARSE ${ARGPARSE_TEST_SOURCES})
|
ADD_EXECUTABLE(ARGPARSE_TESTS ${ARGPARSE_TEST_SOURCES})
|
||||||
INCLUDE_DIRECTORIES("../include" ".")
|
INCLUDE_DIRECTORIES("../include" ".")
|
||||||
set_target_properties(ARGPARSE PROPERTIES OUTPUT_NAME tests)
|
set_target_properties(ARGPARSE_TESTS PROPERTIES OUTPUT_NAME tests)
|
||||||
set_property(TARGET ARGPARSE PROPERTY CXX_STANDARD 17)
|
set_property(TARGET ARGPARSE_TESTS PROPERTY CXX_STANDARD 17)
|
||||||
|
|
||||||
# Set ${PROJECT_NAME} as the startup project
|
# 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)
|
||||||
|
Loading…
Reference in New Issue
Block a user