Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional #95

Merged
merged 21 commits into from
Apr 4, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions .appveyor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,14 @@ install:
build_script:
- mkdir build
- cd build
- cmake .. -DCLI_SINGLE_FILE_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_GENERATOR="Visual Studio 14 2015"
- cmake --build .
- ps: cmake .. -DCLI11_SINGLE_FILE_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_GENERATOR="Visual Studio 14 2015"
- ps: cmake --build .
- cd ..
- conan create . CLIUtils/CLI11

test_script:
- ctest --output-on-failure -C Debug
- cd build
- ps: ctest --output-on-failure -C Debug

notifications:
- provider: Webhook
Expand Down
2 changes: 1 addition & 1 deletion .ci/make_and_test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ set -evx

mkdir -p build
cd build
cmake .. -DCLI_CXX_STD=$1 -DCLI_SINGLE_FILE_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
cmake .. -DCLI11_CXX_STD=$1 -DCLI11_SINGLE_FILE_TESTS=ON -DCMAKE_BUILD_TYPE=Debug -DCMAKE_CXX_COMPILER_LAUNCHER=ccache
cmake --build . -- -j2

set +evx
Expand Down
4 changes: 2 additions & 2 deletions .ci/run_codecov.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ set -evx
cd ${TRAVIS_BUILD_DIR}
mkdir -p build
cd build
cmake .. -DCLI_SINGLE_FILE_TESTS=OFF -DCLI_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Coverage
cmake .. -DCLI11_SINGLE_FILE_TESTS=OFF -DCLI11_EXAMPLES=OFF -DCMAKE_BUILD_TYPE=Coverage
cmake --build . -- -j2
cmake --build . --target CLI_coverage
cmake --build . --target CLI11_coverage

set +evx
echo -en "travis_fold:end:script.build\\r"
Expand Down
20 changes: 17 additions & 3 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,15 @@ matrix:
include:
# Default clang
- compiler: clang
script:
- .ci/make_and_test.sh 11
- .ci/make_and_test.sh 14
- .ci/make_and_test.sh 17

# Check style/tidy
- compiler: clang
env:
- CHECK_STYLE=yes
script:
- cd "${TRAVIS_BUILD_DIR}"
- scripts/check_style.sh
Expand Down Expand Up @@ -50,24 +56,32 @@ matrix:

# GCC 6 and Coverage
- compiler: gcc
env:
- GCC_VER=7
addons:
apt:
sources:
- ubuntu-toolchain-r-test
packages:
- g++-6
- g++-7
- curl
- lcov
install:
- export CC=gcc-6
- export CXX=g++-6
- export CC=gcc-7
- export CXX=g++-7
- DEPS_DIR="${TRAVIS_BUILD_DIR}/deps"
- cd $TRAVIS_BUILD_DIR
- ". .ci/build_lcov.sh"
- ".ci/run_codecov.sh"
script:
- .ci/make_and_test.sh 11
- .ci/make_and_test.sh 14
- .ci/make_and_test.sh 17

# GCC 4.7 and Conan
- compiler: gcc
env:
- GCC_VER=4.7
addons:
apt:
packages:
Expand Down
17 changes: 17 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,27 @@
## In progress

This version has some internal cleanup and improved support for the newest compilers.

Note: This is the final release with `requires`, please switch to `needs`.

* Fix unlimited short options eating two values before checking for positionals when no space present [#90]
* Symmetric exclude text when excluding options, exclude can be called multiple times [#64]
* Support for `std::optional`, `std::experimental::optional`, and `boost::optional` added if `__has_include` is supported [#95]
* All macros/CMake variables now start with `CLI11_` instead of just `CLI_` [#95]
* The internal stream was not being cleared before use in some cases. Fixed. [#95]

Other, non-user facing changes:

* Added `Macros.hpp` with better C++ mode discovery [#95]
* Deprecated macros added for all platforms
* C++17 is now tested on supported platforms [#95]
* Informational printout now added to CTest [#95]
* Better single file generation [#95]
* Added support for GTest on MSVC 2017 (but not in C++17 mode, will need next version of GTest)

[#64]: https://github.com/CLIUtils/CLI11/issues/64
[#90]: https://github.com/CLIUtils/CLI11/issues/90
[#95]: https://github.com/CLIUtils/CLI11/pull/95


## Version 1.4: More feedback
Expand Down
34 changes: 17 additions & 17 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ set(CMAKE_MODULE_PATH "${CMAKE_CURRENT_SOURCE_DIR}/cmake" ${CMAKE_MODULE_PATH})
# Only if built as the main project
if(CMAKE_PROJECT_NAME STREQUAL PROJECT_NAME)
# User settable
set(CLI_CXX_STD "11" CACHE STRING "The CMake standard to require")
set(CLI11_CXX_STD "11" CACHE STRING "The CMake standard to require")

set(CUR_PROJ ON)
set(CMAKE_CXX_STANDARD ${CLI_CXX_STD})
set(CMAKE_CXX_STANDARD ${CLI11_CXX_STD})
set(CMAKE_CXX_EXTENSIONS OFF)
set(CMAKE_CXX_STANDARD_REQUIRED ON)

# Be moderately paranoid with flags
if(MSVC)
add_definitions("/W4")
else()
add_definitions(-Wall -Wextra -pedantic)
add_definitions(-Wall -Wextra -pedantic -Wno-deprecated-declarations)
endif()

if(CMAKE_VERSION VERSION_GREATER 3.6)
Expand Down Expand Up @@ -56,10 +56,10 @@ set_property(GLOBAL PROPERTY USE_FOLDERS ON)

if(CMAKE_BUILD_TYPE STREQUAL Coverage)
include(CodeCoverage)
setup_target_for_coverage(CLI_coverage ctest coverage)
setup_target_for_coverage(CLI11_coverage ctest coverage)
endif()

file(GLOB CLI_headers "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/*")
file(GLOB CLI11_headers "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/*")
# To see in IDE, must be listed for target

add_library(CLI11 INTERFACE)
Expand Down Expand Up @@ -113,18 +113,18 @@ export(PACKAGE CLI11)
# Single file test
find_package(PythonInterp)
if(CUR_PROJ AND PYTHONINTERP_FOUND)
set(CLI_SINGLE_FILE_DEFAULT ON)
set(CLI11_SINGLE_FILE_DEFAULT ON)
else()
set(CLI_SINGLE_FILE_DEFAULT OFF)
set(CLI11_SINGLE_FILE_DEFAULT OFF)
endif()

option(CLI_SINGLE_FILE "Generate a single header file" ${CLI_SINGLE_FILE_DEFAULT})
if(CLI_SINGLE_FILE)
option(CLI11_SINGLE_FILE "Generate a single header file" ${CLI11_SINGLE_FILE_DEFAULT})
if(CLI11_SINGLE_FILE)
find_package(PythonInterp REQUIRED)
file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/include")
add_custom_command(OUTPUT "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp"
COMMAND "${PYTHON_EXECUTABLE}" "${CMAKE_CURRENT_SOURCE_DIR}/scripts/MakeSingleHeader.py" "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp"
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI_headers}
DEPENDS "${CMAKE_CURRENT_SOURCE_DIR}/include/CLI/CLI.hpp" ${CLI11_headers}
)
add_custom_target(generate_cli_single_file ALL
DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/include/CLI11.hpp")
Expand All @@ -134,25 +134,25 @@ if(CLI_SINGLE_FILE)
add_library(CLI11_SINGLE INTERFACE)
target_link_libraries(CLI11_SINGLE INTERFACE CLI11)
add_dependencies(CLI11_SINGLE generate_cli_single_file)
target_compile_definitions(CLI11_SINGLE INTERFACE -DCLI_SINGLE_FILE)
target_compile_definitions(CLI11_SINGLE INTERFACE -DCLI11_SINGLE_FILE)
target_include_directories(CLI11_SINGLE INTERFACE "${CMAKE_CURRENT_BINARY_DIR}/include/")
endif()

option(CLI_SINGLE_FILE_TESTS "Duplicate all the tests for a single file build" OFF)
option(CLI11_SINGLE_FILE_TESTS "Duplicate all the tests for a single file build" OFF)

option(CLI_TESTING "Build the tests and add them" ${CUR_PROJ})
if(CLI_TESTING)
option(CLI11_TESTING "Build the tests and add them" ${CUR_PROJ})
if(CLI11_TESTING)
enable_testing()
add_subdirectory(tests)
endif()

option(CLI_EXAMPLES "Build the examples" ${CUR_PROJ})
if(CLI_EXAMPLES)
option(CLI11_EXAMPLES "Build the examples" ${CUR_PROJ})
if(CLI11_EXAMPLES)
add_subdirectory(examples)
endif()

if(NOT CUR_PROJ)
mark_as_advanced(CLI_SINGLE_FILE_TESTS CLI_EXAMPLES CLI_TESTING)
mark_as_advanced(CLI11_SINGLE_FILE_TESTS CLI11_EXAMPLES CLI11_TESTING)
endif()

# Packaging support
Expand Down
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ After I wrote this, I also found the following libraries:
| [Argh!] | Very minimalistic C++11 parser, single header. Don't have many features. No help generation?!?! At least it's exception-free.|
| [CLI] | Custom language and parser. Huge build-system overkill for very little benefit. Last release in 2009, but still occasionally active. |

See [Awesome C++] for a less-biased list of parsers.
See [Awesome C++] for a less-biased list of parsers. You can also find other single file libraries at [Single file libs].

</p></details>
<br/>
Expand Down Expand Up @@ -164,6 +164,8 @@ An option name must start with a alphabetic character or underscore. For long op

On a C++14 compiler, you can pass a callback function directly to `.add_flag`, while in C++11 mode you'll need to use `.add_flag_function` if you want a callback function. The function will be given the number of times the flag was passed. You can throw a relevant `CLI::ParseError` to signal a failure.

On a compiler that supports C++17's `__has_include`, you can also use `std::optional`, `std::experimental::optional`, and `boost::optional` directly in an `add_option` call. If you don't have `__has_include`, you can define `CLI11_BOOST_OPTIONAL` before including CLI11 to manually add support for `boost::optional`. See [CLI11 Internals] for information on how this was done and how you can add your own converters.

### Example

* `"one,-o,--one"`: Valid as long as not a flag, would create an option that can be specified positionally, or with `-o` or `--one`
Expand Down Expand Up @@ -421,6 +423,7 @@ CLI11 was developed at the [University of Cincinnati] to support of the [GooFit]
[NSF Award 1414736]: https://nsf.gov/awardsearch/showAward?AWD_ID=1414736
[University of Cincinnati]: http://www.uc.edu
[GitBook]: https://cliutils.gitlab.io/CLI11Tutorial
[CLI11 Internals]: https://cliutils.gitlab.io/CLI11Tutorial/chapters/internals.html
[ProgramOptions.hxx]: https://github.com/Fytch/ProgramOptions.hxx
[Argument Aggregator]: https://github.com/vietjtnguyen/argagg
[Args]: https://github.com/Taywee/args
Expand All @@ -434,5 +437,7 @@ CLI11 was developed at the [University of Cincinnati] to support of the [GooFit]
[wandbox-link]: https://wandbox.org/permlink/g7tRkuU8xY3aTIVP
[releases-badge]: https://img.shields.io/github/release/CLIUtils/CLI11.svg
[cli11-po-compare]: https://iscinumpy.gitlab.io/post/comparing-cli11-and-boostpo/
[DIANA slides]: https://indico.cern.ch/event/619465/contributions/2507949/attachments/1448567/2232649/20170424-diana-2.pdf
[Awesome C++]: https://github.com/fffaraz/awesome-cpp/blob/master/README.md#cli
[DIANA slides]: https://indico.cern.ch/event/619465/contributions/2507949/attachments/1448567/2232649/20170424-diana-2.pdf
[Awesome C++]: https://github.com/fffaraz/awesome-cpp/blob/master/README.md#cli
[CLI]: https://codesynthesis.com/projects/cli/
[Single file libs]: https://github.com/nothings/single_file_libs/blob/master/README.md
7 changes: 7 additions & 0 deletions cmake/AddGoogletest.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,10 @@ BUILD_GTEST

set_target_properties(gtest gtest_main gmock gmock_main
PROPERTIES FOLDER "Extern")

if(MSVC AND MSVC_VERSION GREATER_EQUAL 1900)
target_compile_definitions(gtest PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
target_compile_definitions(gtest_main PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
target_compile_definitions(gmock PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
target_compile_definitions(gmock_main PUBLIC _SILENCE_TR1_NAMESPACE_DEPRECATION_WARNING)
endif()
4 changes: 2 additions & 2 deletions conanfile.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ class HelloConan(ConanFile):

def build(self): # this is not building a library, just tests
cmake = CMake(self)
cmake.definitions["CLI_EXAMPLES"] = "OFF"
cmake.definitions["CLI_SINGLE_FILE"] = "OFF"
cmake.definitions["CLI11_EXAMPLES"] = "OFF"
cmake.definitions["CLI11_SINGLE_FILE"] = "OFF"
cmake.configure()
cmake.build()
cmake.test()
Expand Down
2 changes: 1 addition & 1 deletion docs/Doxyfile
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,7 @@ BUILTIN_STL_SUPPORT = NO
# enable parsing support.
# The default value is: NO.

CPP_CLI_SUPPORT = NO
CPP_CLI11_SUPPORT = NO

# Set the SIP_SUPPORT tag to YES if your project consists of sip (see:
# http://www.riverbankcomputing.co.uk/software/sip/intro) sources only. Doxygen
Expand Down
2 changes: 1 addition & 1 deletion examples/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
function(add_cli_exe T)
add_executable(${T} ${ARGN} ${CLI_headers})
add_executable(${T} ${ARGN} ${CLI11_headers})
target_link_libraries(${T} PUBLIC CLI11)
set_target_properties(
${T} PROPERTIES
Expand Down
3 changes: 2 additions & 1 deletion include/CLI/App.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
// CLI Library includes
#include "CLI/Error.hpp"
#include "CLI/Ini.hpp"
#include "CLI/Macros.hpp"
#include "CLI/Option.hpp"
#include "CLI/Split.hpp"
#include "CLI/StringTools.hpp"
Expand Down Expand Up @@ -447,7 +448,7 @@ class App {
return opt;
}

#if __cplusplus >= 201402L
#ifdef CLI11_CPP14
/// Add option for callback (C++14 or better only)
Option *add_flag(std::string name,
std::function<void(size_t)> function, ///< A function to call, void(size_t)
Expand Down
4 changes: 4 additions & 0 deletions include/CLI/CLI.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@

#include "CLI/Version.hpp"

#include "CLI/Macros.hpp"

#include "CLI/Optional.hpp"

#include "CLI/StringTools.hpp"

#include "CLI/Error.hpp"
Expand Down
44 changes: 44 additions & 0 deletions include/CLI/Macros.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
#pragma once

// Distributed under the 3-Clause BSD License. See accompanying
// file LICENSE or https://github.com/CLIUtils/CLI11 for details.

namespace CLI {

// Note that all code in CLI11 must be in a namespace, even if it just a define.

// The following version macro is very similar to the one in PyBind11

#if !(defined(_MSC_VER) && __cplusplus == 199711L) && !defined(__INTEL_COMPILER)
#if __cplusplus >= 201402L
#define CLI11_CPP14
#if __cplusplus >= 201703L
#define CLI11_CPP17
#if __cplusplus > 201703L
#define CLI11_CPP20
#endif
#endif
#endif
#elif defined(_MSC_VER) && __cplusplus == 199711L
// MSVC sets _MSVC_LANG rather than __cplusplus (supposedly until the standard is fully implemented)
// Unless you use the /Zc:__cplusplus flag on Visual Studio 2017 15.7 Preview 3 or newer
#if _MSVC_LANG >= 201402L
#define CLI11_CPP14
#if _MSVC_LANG > 201402L && _MSC_VER >= 1910
#define CLI11_CPP17
#if __MSVC_LANG > 201703L && _MSC_VER >= 1910
#define CLI11_CPP20
#endif
#endif
#endif
#endif

#if defined(PYBIND11_CPP14)
#define CLI11_DEPRECATED(reason) [[deprecated(reason)]]
#elif defined(_MSC_VER)
#define CLI11_DEPRECATED(reason) __declspec(deprecated(reason))
#else
#define CLI11_DEPRECATED(reason) __attribute__((deprecated(reason)))
#endif

} // namespace CLI
15 changes: 11 additions & 4 deletions include/CLI/Option.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
#include <vector>

#include "CLI/Error.hpp"
#include "CLI/Macros.hpp"
#include "CLI/Split.hpp"
#include "CLI/StringTools.hpp"

Expand Down Expand Up @@ -299,17 +300,23 @@ class Option : public OptionBase<Option> {
return needs(opt1, args...);
}

#if __cplusplus <= 201703L
#ifndef CLI11_CPP20
/// Sets required options \deprecated
CLI11_DEPRECATED("Use needs instead of requires (eventual keyword clash)")
Option *requires(Option *opt) { return needs(opt); }

/// Can find a string if needed \deprecated
template <typename T = App> Option *requires(std::string opt_name) { return needs<T>(opt_name); }
template <typename T = App> Option *requires(std::string opt_name) {
for(const Option_p &opt : dynamic_cast<T *>(parent_)->options_)
if(opt.get() != this && opt->check_name(opt_name))
return needs(opt.get());
throw IncorrectConstruction::MissingOption(opt_name);
}

/// Any number supported, any mix of string and Opt \deprecated
template <typename A, typename B, typename... ARG> Option *requires(A opt, B opt1, ARG... args) {
needs(opt);
return needs(opt1, args...);
requires(opt);
return requires(opt1, args...);
}
#endif

Expand Down
Loading