Update argparse
This commit is contained in:
parent
ea83449ef7
commit
6cf6888489
|
@ -23,6 +23,7 @@
|
|||
|
||||
# Note: The glew folder isn't added here because it is added inside the main CMakeLists.txt
|
||||
|
||||
set( ARGPARSE_INSTALL OFF )
|
||||
add_subdirectory( argparse )
|
||||
add_subdirectory( clipper )
|
||||
add_subdirectory( clipper2 )
|
||||
|
|
|
@ -1,12 +1,13 @@
|
|||
cmake_minimum_required(VERSION 3.12.4)
|
||||
|
||||
project(argparse
|
||||
VERSION 2.9.0
|
||||
VERSION 2.9.0
|
||||
DESCRIPTION "A single header argument parser for C++17"
|
||||
HOMEPAGE_URL "https://github.com/p-ranav/argparse"
|
||||
LANGUAGES CXX
|
||||
)
|
||||
|
||||
option(ARGPARSE_INSTALL ON)
|
||||
option(ARGPARSE_BUILD_TESTS OFF)
|
||||
option(ARGPARSE_LONG_VERSION_ARG_ONLY OFF)
|
||||
|
||||
|
@ -36,4 +37,65 @@ if(ARGPARSE_BUILD_TESTS)
|
|||
add_subdirectory(test)
|
||||
endif()
|
||||
|
||||
## Modified to remove undesirable install steps
|
||||
if(ARGPARSE_INSTALL)
|
||||
install(TARGETS argparse EXPORT argparseConfig)
|
||||
install(EXPORT argparseConfig
|
||||
NAMESPACE argparse::
|
||||
DESTINATION ${CMAKE_INSTALL_LIBDIR_ARCHIND}/cmake/${PROJECT_NAME})
|
||||
install(FILES ${CMAKE_CURRENT_LIST_DIR}/include/argparse/argparse.hpp
|
||||
DESTINATION ${CMAKE_INSTALL_INCLUDEDIR}/argparse)
|
||||
|
||||
|
||||
set(CONFIG_FILE_NAME_WITHOUT_EXT "${PROJECT_NAME}Config")
|
||||
set(CMAKE_CONFIG_FILE_BASENAME "${CMAKE_CURRENT_BINARY_DIR}/${CONFIG_FILE_NAME_WITHOUT_EXT}")
|
||||
set(CMAKE_CONFIG_VERSION_FILE_NAME "${CMAKE_CONFIG_FILE_BASENAME}-version.cmake")
|
||||
set(CMAKE_CONFIG_FILE_NAME "${CMAKE_CONFIG_FILE_BASENAME}.cmake")
|
||||
|
||||
if(${CMAKE_VERSION} VERSION_GREATER "3.14")
|
||||
set(OPTIONAL_ARCH_INDEPENDENT "ARCH_INDEPENDENT")
|
||||
endif()
|
||||
|
||||
write_basic_package_version_file("${CMAKE_CONFIG_VERSION_FILE_NAME}"
|
||||
COMPATIBILITY ExactVersion
|
||||
${OPTIONAL_ARCH_INDEPENDENT}
|
||||
)
|
||||
|
||||
export(EXPORT argparseConfig
|
||||
NAMESPACE argparse::)
|
||||
|
||||
install(FILES "${CMAKE_CONFIG_VERSION_FILE_NAME}"
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR_ARCHIND}/cmake/${PROJECT_NAME}")
|
||||
|
||||
set(PackagingTemplatesDir "${CMAKE_CURRENT_SOURCE_DIR}/packaging")
|
||||
|
||||
set(CPACK_PACKAGE_NAME "${PROJECT_NAME}")
|
||||
set(CPACK_PACKAGE_VENDOR "argparse (C++) developers")
|
||||
set(CPACK_PACKAGE_DESCRIPTION "${PROJECT_DESCRIPTION}")
|
||||
set(CPACK_DEBIAN_PACKAGE_NAME "${CPACK_PACKAGE_NAME}")
|
||||
set(CPACK_RPM_PACKAGE_NAME "${CPACK_PACKAGE_NAME}")
|
||||
set(CPACK_PACKAGE_HOMEPAGE_URL "${PROJECT_HOMEPAGE_URL}")
|
||||
set(CPACK_PACKAGE_MAINTAINER "Pranav Srinivas Kumar")
|
||||
set(CPACK_DEBIAN_PACKAGE_MAINTAINER "${CPACK_PACKAGE_MAINTAINER}")
|
||||
set(CPACK_RESOURCE_FILE_LICENSE "${CMAKE_CURRENT_SOURCE_DIR}/LICENSE")
|
||||
set(CPACK_RESOURCE_FILE_README "${CMAKE_CURRENT_SOURCE_DIR}/README.md")
|
||||
|
||||
set(CPACK_DEBIAN_PACKAGE_NAME "lib${PROJECT_NAME}-dev")
|
||||
set(CPACK_DEBIAN_PACKAGE_DEPENDS "libc6-dev")
|
||||
set(CPACK_DEBIAN_PACKAGE_SUGGESTS "cmake, pkg-config, pkg-conf")
|
||||
|
||||
set(CPACK_RPM_PACKAGE_NAME "lib${PROJECT_NAME}-devel")
|
||||
set(CPACK_RPM_PACKAGE_SUGGESTS "${CPACK_DEBIAN_PACKAGE_SUGGESTS}")
|
||||
|
||||
set(CPACK_DEB_COMPONENT_INSTALL ON)
|
||||
set(CPACK_RPM_COMPONENT_INSTALL ON)
|
||||
set(CPACK_NSIS_COMPONENT_INSTALL ON)
|
||||
set(CPACK_DEBIAN_COMPRESSION_TYPE "xz")
|
||||
|
||||
set(PKG_CONFIG_FILE_NAME "${CMAKE_CURRENT_BINARY_DIR}/${PROJECT_NAME}.pc")
|
||||
configure_file("${PackagingTemplatesDir}/pkgconfig.pc.in" "${PKG_CONFIG_FILE_NAME}" @ONLY)
|
||||
install(FILES "${PKG_CONFIG_FILE_NAME}"
|
||||
DESTINATION "${CMAKE_INSTALL_LIBDIR_ARCHIND}/pkgconfig"
|
||||
)
|
||||
endif()
|
||||
|
||||
include(CPack)
|
||||
|
|
|
@ -38,6 +38,7 @@
|
|||
* [Parent Parsers](#parent-parsers)
|
||||
* [Subcommands](#subcommands)
|
||||
* [Parse Known Args](#parse-known-args)
|
||||
* [ArgumentParser in bool Context](#argumentparser-in-bool-context)
|
||||
* [Custom Prefix Characters](#custom-prefix-characters)
|
||||
* [Custom Assignment Characters](#custom-assignment-characters)
|
||||
* [Further Examples](#further-examples)
|
||||
|
@ -824,7 +825,7 @@ Subcommands:
|
|||
|
||||
When a help message is requested from a subparser, only the help for that particular parser will be printed. The help message will not include parent parser or sibling parser messages.
|
||||
|
||||
Additionally, every parser has a `.is_subcommand_used("<command_name>")` member function to check if a subcommand was used.
|
||||
Additionally, every parser has the `.is_subcommand_used("<command_name>")` and `.is_subcommand_used(subparser)` member functions to check if a subcommand was used.
|
||||
|
||||
### Parse Known Args
|
||||
|
||||
|
@ -848,7 +849,13 @@ int main(int argc, char *argv[]) {
|
|||
}
|
||||
```
|
||||
|
||||
### Custom Prefix Characters
|
||||
### ArgumentParser in bool Context
|
||||
|
||||
An `ArgumentParser` is `false` until it (or one of its subparsers) have extracted
|
||||
known value(s) with `.parse_args` or `.parse_known_args`. When using `.parse_known_args`,
|
||||
unknown arguments will not make a parser `true`.
|
||||
|
||||
### Custom Prefix Characters
|
||||
|
||||
Most command-line options will use `-` as the prefix, e.g. `-f/--foo`. Parsers that need to support different or additional prefix characters, e.g. for options like `+f` or `/foo`, may specify them using the `set_prefix_chars()`.
|
||||
|
||||
|
@ -1092,7 +1099,7 @@ foo@bar:/home/dev/$ ./test --bar=BAR --foo
|
|||
Use the latest argparse in your CMake project without copying any content.
|
||||
|
||||
```cmake
|
||||
cmake_minimum_required(VERSION 3.11)
|
||||
cmake_minimum_required(VERSION 3.14)
|
||||
|
||||
PROJECT(myproject)
|
||||
|
||||
|
|
|
@ -63,6 +63,8 @@ struct HasContainerTraits : std::false_type {};
|
|||
|
||||
template <> struct HasContainerTraits<std::string> : std::false_type {};
|
||||
|
||||
template <> struct HasContainerTraits<std::string_view> : std::false_type {};
|
||||
|
||||
template <typename T>
|
||||
struct HasContainerTraits<
|
||||
T, std::void_t<typename T::value_type, decltype(std::declval<T>().begin()),
|
||||
|
@ -597,7 +599,7 @@ public:
|
|||
std::string get_inline_usage() const {
|
||||
std::stringstream usage;
|
||||
// Find the longest variant to show in the usage string
|
||||
std::string longest_name = m_names[0];
|
||||
std::string longest_name = m_names.front();
|
||||
for (const auto &s : m_names) {
|
||||
if (s.size() > longest_name.size()) {
|
||||
longest_name = s;
|
||||
|
@ -758,6 +760,8 @@ private:
|
|||
std::stringstream stream;
|
||||
if (!m_used_name.empty()) {
|
||||
stream << m_used_name << ": ";
|
||||
} else {
|
||||
stream << m_names.front() << ": ";
|
||||
}
|
||||
if (m_num_args_range.is_exact()) {
|
||||
stream << m_num_args_range.get_min();
|
||||
|
@ -773,7 +777,7 @@ private:
|
|||
|
||||
void throw_required_arg_not_used_error() const {
|
||||
std::stringstream stream;
|
||||
stream << m_names[0] << ": required.";
|
||||
stream << m_names.front() << ": required.";
|
||||
throw std::runtime_error(stream.str());
|
||||
}
|
||||
|
||||
|
@ -1102,6 +1106,21 @@ public:
|
|||
return *this;
|
||||
}
|
||||
|
||||
explicit operator bool() const {
|
||||
auto arg_used = std::any_of(m_argument_map.cbegin(),
|
||||
m_argument_map.cend(),
|
||||
[](auto &it) {
|
||||
return it.second->m_is_used;
|
||||
});
|
||||
auto subparser_used = std::any_of(m_subparser_used.cbegin(),
|
||||
m_subparser_used.cend(),
|
||||
[](auto &it) {
|
||||
return it.second;
|
||||
});
|
||||
|
||||
return m_is_parsed && (arg_used || subparser_used);
|
||||
}
|
||||
|
||||
// Parameter packing
|
||||
// Call add_argument with variadic number of string arguments
|
||||
template <typename... Targs> Argument &add_argument(Targs... f_args) {
|
||||
|
@ -1236,13 +1255,18 @@ public:
|
|||
return (*this)[arg_name].m_is_used;
|
||||
}
|
||||
|
||||
/* Getter that returns true for user-supplied options. Returns false if not
|
||||
* user-supplied, even with a default value.
|
||||
/* Getter that returns true if a subcommand is used.
|
||||
*/
|
||||
auto is_subcommand_used(std::string_view subcommand_name) const {
|
||||
return m_subparser_used.at(subcommand_name);
|
||||
}
|
||||
|
||||
/* Getter that returns true if a subcommand is used.
|
||||
*/
|
||||
auto is_subcommand_used(const ArgumentParser &subparser) const {
|
||||
return is_subcommand_used(subparser.m_program_name);
|
||||
}
|
||||
|
||||
/* Indexing operator. Return a reference to an Argument object
|
||||
* Used in conjuction with Argument.operator== e.g., parser["foo"] == true
|
||||
* @throws std::logic_error in case of an invalid argument name
|
||||
|
@ -1341,9 +1365,9 @@ public:
|
|||
|
||||
// Add any options inline here
|
||||
for (const auto &argument : this->m_optional_arguments) {
|
||||
if (argument.m_names[0] == "-v") {
|
||||
if (argument.m_names.front() == "-v") {
|
||||
continue;
|
||||
} else if (argument.m_names[0] == "-h") {
|
||||
} else if (argument.m_names.front() == "-h") {
|
||||
stream << " [-h]";
|
||||
} else {
|
||||
stream << " " << argument.get_inline_usage();
|
||||
|
@ -1642,11 +1666,11 @@ private:
|
|||
bool m_is_parsed = false;
|
||||
std::list<Argument> m_positional_arguments;
|
||||
std::list<Argument> m_optional_arguments;
|
||||
std::map<std::string_view, argument_it, std::less<>> m_argument_map;
|
||||
std::map<std::string_view, argument_it> m_argument_map;
|
||||
std::string m_parser_path;
|
||||
std::list<std::reference_wrapper<ArgumentParser>> m_subparsers;
|
||||
std::map<std::string_view, argument_parser_it, std::less<>> m_subparser_map;
|
||||
std::map<std::string_view, bool, std::less<>> m_subparser_used;
|
||||
std::map<std::string_view, argument_parser_it> m_subparser_map;
|
||||
std::map<std::string_view, bool> m_subparser_used;
|
||||
};
|
||||
|
||||
} // namespace argparse
|
||||
|
|
|
@ -25,7 +25,6 @@ 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()
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <cassert>
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <cassert>
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
#include <cassert>
|
||||
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
// SPDX-License-Identifier: MIT
|
||||
|
||||
#include <argparse/argparse.hpp>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
|
|
|
@ -27,6 +27,7 @@ file(GLOB ARGPARSE_TEST_SOURCES
|
|||
main.cpp
|
||||
test_actions.cpp
|
||||
test_append.cpp
|
||||
test_bool_operator.cpp
|
||||
test_compound_arguments.cpp
|
||||
test_container_arguments.cpp
|
||||
test_const_correct.cpp
|
||||
|
|
|
@ -0,0 +1,85 @@
|
|||
#include <argparse/argparse.hpp>
|
||||
#include <doctest.hpp>
|
||||
|
||||
using doctest::test_suite;
|
||||
|
||||
TEST_CASE("ArgumentParser in bool context" *
|
||||
test_suite("argument_parser")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("cases").remaining();
|
||||
|
||||
program.parse_args({"test"});
|
||||
REQUIRE_FALSE(program);
|
||||
|
||||
program.parse_args({"test", "one", "two"});
|
||||
REQUIRE(program);
|
||||
}
|
||||
|
||||
TEST_CASE("With subparsers in bool context" * test_suite("argument_parser")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
argparse::ArgumentParser cmd_fly("fly");
|
||||
cmd_fly.add_argument("plane");
|
||||
|
||||
argparse::ArgumentParser cmd_soar("soar");
|
||||
cmd_soar.add_argument("direction");
|
||||
|
||||
program.add_subparser(cmd_fly);
|
||||
program.add_subparser(cmd_soar);
|
||||
|
||||
program.parse_args({"test", "fly", "glider"});
|
||||
REQUIRE(program);
|
||||
REQUIRE(cmd_fly);
|
||||
REQUIRE_FALSE(cmd_soar);
|
||||
}
|
||||
|
||||
TEST_CASE("Parsers remain false with unknown arguments" *
|
||||
test_suite("argument_parser")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
argparse::ArgumentParser cmd_build("build");
|
||||
cmd_build.add_argument("--file").nargs(1);
|
||||
|
||||
argparse::ArgumentParser cmd_run("run");
|
||||
cmd_run.add_argument("--file").nargs(1);
|
||||
|
||||
program.add_subparser(cmd_build);
|
||||
program.add_subparser(cmd_run);
|
||||
|
||||
auto unknowns =
|
||||
program.parse_known_args({"test", "badger", "--add-meal", "grubs"});
|
||||
REQUIRE_FALSE(program);
|
||||
REQUIRE_FALSE(cmd_build);
|
||||
REQUIRE_FALSE(cmd_run);
|
||||
}
|
||||
|
||||
TEST_CASE("Multi-level parsers match subparser bool" *
|
||||
test_suite("argument_parser")) {
|
||||
argparse::ArgumentParser program("test");
|
||||
|
||||
argparse::ArgumentParser cmd_cook("cook");
|
||||
cmd_cook.add_argument("--temperature");
|
||||
|
||||
argparse::ArgumentParser cmd_cook_boil("boil");
|
||||
cmd_cook_boil.add_argument("--rate");
|
||||
|
||||
argparse::ArgumentParser cmd_cook_boil_stir("stir");
|
||||
cmd_cook_boil_stir.add_argument("--rate");
|
||||
|
||||
argparse::ArgumentParser cmd_wash("wash");
|
||||
|
||||
program.add_subparser(cmd_cook);
|
||||
cmd_cook.add_subparser(cmd_cook_boil);
|
||||
cmd_cook_boil.add_subparser(cmd_cook_boil_stir);
|
||||
|
||||
program.add_subparser(cmd_wash);
|
||||
|
||||
auto unknowns = program.parse_known_args(
|
||||
{"test", "cook", "boil", "stir", "--rate", "fast"});
|
||||
|
||||
REQUIRE(program);
|
||||
REQUIRE(cmd_cook);
|
||||
REQUIRE(cmd_cook_boil);
|
||||
REQUIRE(cmd_cook_boil_stir);
|
||||
REQUIRE_FALSE(cmd_wash);
|
||||
}
|
|
@ -18,7 +18,7 @@ TEST_CASE("Missing expected positional argument" *
|
|||
argparse::ArgumentParser program("test");
|
||||
program.add_argument("input");
|
||||
REQUIRE_THROWS_WITH_AS(program.parse_args({"test"}),
|
||||
"1 argument(s) expected. 0 provided.",
|
||||
"input: 1 argument(s) expected. 0 provided.",
|
||||
std::runtime_error);
|
||||
}
|
||||
|
||||
|
|
|
@ -199,4 +199,41 @@ TEST_CASE("Parse git commands" * test_suite("subparsers")) {
|
|||
REQUIRE(submodule_update_command.get<bool>("--init") == true);
|
||||
REQUIRE(submodule_update_command.get<bool>("--recursive") == true);
|
||||
}
|
||||
}
|
||||
|
||||
TEST_CASE("Check is_subcommand_used after parse" * test_suite("subparsers")) {
|
||||
argparse::ArgumentParser command_1("add");
|
||||
|
||||
argparse::ArgumentParser command_2("clean");
|
||||
command_2.add_argument("--fullclean")
|
||||
.default_value(false)
|
||||
.implicit_value(true);
|
||||
|
||||
argparse::ArgumentParser program("test");
|
||||
program.add_subparser(command_1);
|
||||
program.add_subparser(command_2);
|
||||
|
||||
SUBCASE("command 1") {
|
||||
program.parse_args({"test", "add"});
|
||||
REQUIRE(program.is_subcommand_used("add") == true);
|
||||
REQUIRE(program.is_subcommand_used(command_1) == true);
|
||||
REQUIRE(program.is_subcommand_used("clean") == false);
|
||||
REQUIRE(program.is_subcommand_used(command_2) == false);
|
||||
}
|
||||
|
||||
SUBCASE("command 2") {
|
||||
program.parse_args({"test", "clean", "--fullclean"});
|
||||
REQUIRE(program.is_subcommand_used("add") == false);
|
||||
REQUIRE(program.is_subcommand_used(command_1) == false);
|
||||
REQUIRE(program.is_subcommand_used("clean") == true);
|
||||
REQUIRE(program.is_subcommand_used(command_2) == true);
|
||||
}
|
||||
|
||||
SUBCASE("none") {
|
||||
program.parse_args({"test"});
|
||||
REQUIRE(program.is_subcommand_used("add") == false);
|
||||
REQUIRE(program.is_subcommand_used(command_1) == false);
|
||||
REQUIRE(program.is_subcommand_used("clean") == false);
|
||||
REQUIRE(program.is_subcommand_used(command_2) == false);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue