From 5f7e531ce1d1cd7c5bc49dc9ee060781c22572db Mon Sep 17 00:00:00 2001 From: Tal Kedar Date: Mon, 15 May 2023 19:41:16 -0400 Subject: [PATCH 1/2] [parser::params] add overload accepting std::initializer_list --- argh.h | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/argh.h b/argh.h index 5c1d1cb..60bed8d 100644 --- a/argh.h +++ b/argh.h @@ -8,6 +8,7 @@ #include #include #include +#include namespace argh { @@ -121,6 +122,7 @@ namespace argh std::multiset const& flags() const { return flags_; } std::multimap const& params() const { return params_; } multimap_iteration_wrapper params(std::string const& name) const; + auto params(std::initializer_list init_list) const; std::vector const& pos_args() const { return pos_args_; } // begin() and end() for using range-for over positional args. @@ -482,4 +484,13 @@ namespace argh auto trimmed_name = trim_leading_dashes(name); return multimap_iteration_wrapper(params_.lower_bound(trimmed_name), params_.upper_bound(trimmed_name)); } + + inline auto parser::params(std::initializer_list init_list) const + { + return std::ranges::subrange( init_list.begin(), init_list.end() ) | + std::views::transform( [ this ]( char const * const name ) -> multimap_iteration_wrapper { + return this->params( std::string( name ) ); + } ) | + std::views::join; + } } From 91cd4ac404b2135628f3c548244f4cad9f211c0e Mon Sep 17 00:00:00 2001 From: Tal Kedar Date: Wed, 17 May 2023 13:16:16 -0400 Subject: [PATCH 2/2] [parser::params] conditionally enable std::initializer_list overload by std::ranges avilability --- argh.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/argh.h b/argh.h index 60bed8d..c62b023 100644 --- a/argh.h +++ b/argh.h @@ -8,7 +8,14 @@ #include #include #include -#include + +#if __cplusplus >= 201703L && __has_include( ) +# include // for library features tests +#endif + +#if defined( __cpp_lib_ranges ) +# include +#endif namespace argh { @@ -122,7 +129,9 @@ namespace argh std::multiset const& flags() const { return flags_; } std::multimap const& params() const { return params_; } multimap_iteration_wrapper params(std::string const& name) const; +#if defined( __cpp_lib_ranges ) auto params(std::initializer_list init_list) const; +#endif std::vector const& pos_args() const { return pos_args_; } // begin() and end() for using range-for over positional args. @@ -485,6 +494,9 @@ namespace argh return multimap_iteration_wrapper(params_.lower_bound(trimmed_name), params_.upper_bound(trimmed_name)); } + ////////////////////////////////////////////////////////////////////////// + +#if defined( __cpp_lib_ranges ) inline auto parser::params(std::initializer_list init_list) const { return std::ranges::subrange( init_list.begin(), init_list.end() ) | @@ -493,4 +505,5 @@ namespace argh } ) | std::views::join; } +#endif }