Ookii.CommandLine for C++  2.0.0
command_line_switch.h
Go to the documentation of this file.
1 #ifndef OOKII_COMMAND_LINE_SWITCH_H_
4 #define OOKII_COMMAND_LINE_SWITCH_H_
5 
6 #pragma once
7 
8 #include <ostream>
9 #include "format_helper.h"
10 #include "value_description.h"
11 
12 namespace ookii
13 {
23  template<typename CharType, typename Traits, typename Alloc>
24  struct lexical_convert<bool, CharType, Traits, Alloc>
25  {
29  static std::optional<bool> from_string(std::basic_string_view<CharType, Traits> value, const std::locale & = {})
30  {
31  constexpr auto true_value = literal_cast<CharType>("true");
32  constexpr auto true_alt_value = literal_cast<CharType>("1");
33  constexpr auto false_value = literal_cast<CharType>("false");
34  constexpr auto false_alt_value = literal_cast<CharType>("0");
35 
36  if (string_equal_case_insensitive<CharType, Traits>(value, true_value.data()) ||
37  value == true_alt_value.data())
38  {
39  return {true};
40  }
41  else if (string_equal_case_insensitive<CharType, Traits>(value, false_value.data()) ||
42  value == false_alt_value.data())
43  {
44  return {false};
45  }
46  else
47  {
48  return {};
49  }
50  }
51  };
52 
53  namespace details
54  {
55  template<typename T>
56  struct is_switch : public std::false_type {};
57 
58  template<>
59  struct is_switch<bool> : public std::true_type {};
60 
61  template<>
62  struct is_switch<std::optional<bool>> : public std::true_type {};
63  }
64 }
65 
66 #endif
Allows selection between the C++20 <format> library and libfmt.
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
static std::optional< bool > from_string(std::basic_string_view< CharType, Traits > value, const std::locale &={})
Convert a string to the specified type.
Definition: command_line_switch.h:29
Template class used to convert strings to strongly typed argument values.
Definition: string_helper.h:212
Provides a template used to determine the default value description for a type.