catchorg / Clara

A simple to use, composable, command line parser for C++ 11 and beyond
Boost Software License 1.0
650 stars 67 forks source link

Add support for std::optional if macro '__cpp_if_constexpr' returns 1 #45

Closed ferkulat closed 6 years ago

ferkulat commented 6 years ago

I really like the idea to put command line options into optional values. This way the parser is not responsible for setting default values. And if one compiles with C++17 this PR would make it possible to deal with std::optional variables

codecov[bot] commented 6 years ago

Codecov Report

Merging #45 into master will not change coverage. The diff coverage is n/a.

Impacted file tree graph

@@           Coverage Diff           @@
##           master      #45   +/-   ##
=======================================
  Coverage   90.36%   90.36%           
=======================================
  Files           2        2           
  Lines         685      685           
=======================================
  Hits          619      619           
  Misses         66       66
Impacted Files Coverage Δ
include/clara.hpp 87.67% <ø> (ø) :arrow_up:

Continue to review full report at Codecov.

Legend - Click here to learn more Δ = absolute <relative> (impact), ø = not affected, ? = missing data Powered by Codecov. Last update a66d275...db3f8a1. Read the comment docs.

philsquared commented 6 years ago

I've just implemented support for optionals. I forgot about this PR, so I did it slightly differently - although it's very similar.

The main difference is I do use __has_include(<optional>) for detection, but along with __cplusplus >= 201703L. It seems to work on all GCC (and Clang) versions we currently support - and doesn't break on Windows, AFAICS (but I don't think the auto-detection kicks in there, yet). You can also manually override, and supply alternate optional types by #defineing CLARA_CONFIG_OPTIONAL_TYPE to some (template) type, e.g. boost::optional (although I haven't tested that). It should work for any optional type that supports ! for validity and * for extracting the value. The underlying type must still be default constructible.

horenmar commented 6 years ago

I fixed parts of the optional support and added the extra tests.