@@ -225,20 +225,39 @@ class App {
225225 } else
226226 throw OptionAlreadyAdded (myopt.get_name ());
227227 }
228+
229+
228230
229- // / Add option for non-vectors
231+ // / Add option for non-vectors (duplicate copy needed without defaulted to avoid `iostream << value`)
230232 template <typename T, enable_if_t <!is_vector<T>::value, detail::enabler> = detail::dummy>
231233 Option *add_option (std::string name,
232234 T &variable, // /< The variable to set
233- std::string description = " " ,
234- bool defaulted = false ) {
235+ std::string description = " " ) {
235236
236237 CLI::callback_t fun = [&variable](CLI::results_t res) {
237238 if (res.size () != 1 )
238239 return false ;
239240 return detail::lexical_cast (res[0 ], variable);
240241 };
241242
243+ Option *opt = add_option (name, fun, description, false );
244+ opt->set_custom_option (detail::type_name<T>());
245+ return opt;
246+ }
247+
248+ // / Add option for non-vectors with a default print
249+ template <typename T, enable_if_t <!is_vector<T>::value, detail::enabler> = detail::dummy>
250+ Option *add_option (std::string name,
251+ T &variable, // /< The variable to set
252+ std::string description,
253+ bool defaulted) {
254+
255+ CLI::callback_t fun = [&variable](CLI::results_t res) {
256+ if (res.size () != 1 )
257+ return false ;
258+ return detail::lexical_cast (res[0 ], variable);
259+ };
260+
242261 Option *opt = add_option (name, fun, description, defaulted);
243262 opt->set_custom_option (detail::type_name<T>());
244263 if (defaulted) {
@@ -248,13 +267,34 @@ class App {
248267 }
249268 return opt;
250269 }
251-
270+
271+ // / Add option for vectors (no default)
272+ template <typename T>
273+ Option *add_option (std::string name,
274+ std::vector<T> &variable, // /< The variable vector to set
275+ std::string description = " " ) {
276+
277+ CLI::callback_t fun = [&variable](CLI::results_t res) {
278+ bool retval = true ;
279+ variable.clear ();
280+ for (const auto &a : res) {
281+ variable.emplace_back ();
282+ retval &= detail::lexical_cast (a, variable.back ());
283+ }
284+ return (!variable.empty ()) && retval;
285+ };
286+
287+ Option *opt = add_option (name, fun, description, false );
288+ opt->set_custom_option (detail::type_name<T>(), -1 , true );
289+ return opt;
290+ }
291+
252292 // / Add option for vectors
253293 template <typename T>
254294 Option *add_option (std::string name,
255295 std::vector<T> &variable, // /< The variable vector to set
256- std::string description = " " ,
257- bool defaulted = false ) {
296+ std::string description,
297+ bool defaulted) {
258298
259299 CLI::callback_t fun = [&variable](CLI::results_t res) {
260300 bool retval = true ;
@@ -323,13 +363,12 @@ class App {
323363 return opt;
324364 }
325365
326- // / Add set of options
366+ // / Add set of options (No default)
327367 template <typename T>
328368 Option *add_set (std::string name,
329369 T &member, // /< The selected member of the set
330370 std::set<T> options, // /< The set of posibilities
331- std::string description = " " ,
332- bool defaulted = false ) {
371+ std::string description = " " ) {
333372
334373 CLI::callback_t fun = [&member, options](CLI::results_t res) {
335374 if (res.size () != 1 ) {
@@ -341,6 +380,31 @@ class App {
341380 return std::find (std::begin (options), std::end (options), member) != std::end (options);
342381 };
343382
383+ Option *opt = add_option (name, fun, description, false );
384+ std::string typeval = detail::type_name<T>();
385+ typeval += " in {" + detail::join (options) + " }" ;
386+ opt->set_custom_option (typeval);
387+ return opt;
388+ }
389+
390+ // / Add set of options
391+ template <typename T>
392+ Option *add_set (std::string name,
393+ T &member, // /< The selected member of the set
394+ std::set<T> options, // /< The set of posibilities
395+ std::string description,
396+ bool defaulted) {
397+
398+ CLI::callback_t fun = [&member, options](CLI::results_t res) {
399+ if (res.size () != 1 ) {
400+ return false ;
401+ }
402+ bool retval = detail::lexical_cast (res[0 ], member);
403+ if (!retval)
404+ return false ;
405+ return std::find (std::begin (options), std::end (options), member) != std::end (options);
406+ };
407+
344408 Option *opt = add_option (name, fun, description, defaulted);
345409 std::string typeval = detail::type_name<T>();
346410 typeval += " in {" + detail::join (options) + " }" ;
@@ -353,12 +417,11 @@ class App {
353417 return opt;
354418 }
355419
356- // / Add set of options, string only, ignore case
420+ // / Add set of options, string only, ignore case (no default)
357421 Option *add_set_ignore_case (std::string name,
358422 std::string &member, // /< The selected member of the set
359423 std::set<std::string> options, // /< The set of posibilities
360- std::string description = " " ,
361- bool defaulted = false ) {
424+ std::string description = " " ) {
362425
363426 CLI::callback_t fun = [&member, options](CLI::results_t res) {
364427 if (res.size () != 1 ) {
@@ -376,6 +439,37 @@ class App {
376439 }
377440 };
378441
442+ Option *opt = add_option (name, fun, description, false );
443+ std::string typeval = detail::type_name<std::string>();
444+ typeval += " in {" + detail::join (options) + " }" ;
445+ opt->set_custom_option (typeval);
446+
447+ return opt;
448+ }
449+
450+ // / Add set of options, string only, ignore case
451+ Option *add_set_ignore_case (std::string name,
452+ std::string &member, // /< The selected member of the set
453+ std::set<std::string> options, // /< The set of posibilities
454+ std::string description,
455+ bool defaulted) {
456+
457+ CLI::callback_t fun = [&member, options](CLI::results_t res) {
458+ if (res.size () != 1 ) {
459+ return false ;
460+ }
461+ member = detail::to_lower (res[0 ]);
462+ auto iter = std::find_if (std::begin (options), std::end (options), [&member](std::string val) {
463+ return detail::to_lower (val) == member;
464+ });
465+ if (iter == std::end (options))
466+ return false ;
467+ else {
468+ member = *iter;
469+ return true ;
470+ }
471+ };
472+
379473 Option *opt = add_option (name, fun, description, defaulted);
380474 std::string typeval = detail::type_name<std::string>();
381475 typeval += " in {" + detail::join (options) + " }" ;
0 commit comments