-
Notifications
You must be signed in to change notification settings - Fork 264
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
135 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters