13 template <
typename Enum>
17 typedef Enum value_type;
23 std::map<value_type, std::vector<std::string>>
values;
26 std::map<std::string, value_type, CaseInsensitiveLess>
names;
28 void init(value_type next_value)
32 void init(value_type next_value, std::string name)
34 names[name] = next_value;
36 values[next_value] = std::vector<std::string>(1, name);
38 values[next_value].push_back(name);
41 template <
typename ... Args>
42 void init(value_type next_value, std::string name, std::string next_name, Args ... args)
44 init(next_value, name);
45 init(value_type(next_value + 1), next_name, args...);
48 void init(value_type _, std::string name, value_type actual_next_value)
50 init(actual_next_value, name);
53 template <
typename ... Args>
54 void init(value_type _, std::string name, value_type actual_next_value, std::string next_name, Args ... args)
56 init(actual_next_value, name);
57 init(value_type(actual_next_value + 1), next_name, args...);
61 template <
typename ... Args>
62 Enumeration(Args ... args)
64 init(value_type(0), args...);
68 std::optional<value_type>
LookupName(std::string name)
const
70 auto it =
names.find(name);
72 return std::optional<value_type>();
74 return std::make_optional(it->second);
86 typedef typename T::value_type value_type;
88 ItemOf(value_type value = value_type()) : value(value) { }
89 operator value_type()
const {
return value; }
98 T ParseValue(std::string value)
129 return std::stoll(value,
nullptr, 0);
131 catch(std::invalid_argument& a)
133 Linker::Error <<
"Error: Unable to parse " << value <<
", ignoring" << std::endl;
152 return value !=
"0" && value !=
"false" && value !=
"no" && value ==
"off";
163 template <
typename T>
169 std::vector<T> result;
170 size_t string_offset = 0;
172 while((comma = value.find(
',', string_offset)) != std::string::npos)
174 result.push_back(Linker::ParseValue<T>(value.substr(string_offset, comma - string_offset)));
175 string_offset = comma + 1;
177 result.push_back(Linker::ParseValue<T>(value.substr(string_offset)));
184 std::ostringstream oss;
191 template <
typename T>
197 return std::optional<T>(Linker::ParseValue<T>(value));
203 std::ostringstream oss;
210 template <
typename T>
213 static T enumeration;
218 if(
auto enum_value = enumeration.LookupName(value))
235 template <
typename T>
236 class OptionDescription;
249 : name(name), description(description)
255 virtual void PrintDetails(std::ostream& out, std::string indentation)
271 template <
typename T>
280 std::string type_name()
override
286 template <
typename T>
297 Option(std::string name, std::string description)
304 template <
typename T>
316 std::string type_name()
override
324 auto option_it = options->find(name);
325 if(option_it != options->end())
327 return ParseValue<T>(option_it->second);
337 template <
typename T>
344 Option(std::string name, std::string description, T::value_type
default_value =
typename T::value_type())
351 return "string option";
357 auto option_it = options->find(name);
358 if(option_it != options->end())
360 return ParseValue<ItemOf<T>>(option_it->second);
368 void PrintDetails(std::ostream& out, std::string indentation)
override
370 out << indentation <<
"permitted values:" << std::endl;
373 out << indentation <<
"\t";
374 bool started =
false;
375 for(
auto name : pair.second)
384 if(TypeData<ItemOf<T>>::enumeration.descriptions.find(pair.first) != TypeData<ItemOf<T>>::enumeration.descriptions.end())
386 out << indentation <<
"\t\t" << TypeData<ItemOf<T>>::enumeration.descriptions[pair.first] << std::endl;
397 Option(std::string name, std::string description)
410 return options->find(name) != options->end();
415 template <
typename T>
419 Option(std::string name, std::string description)
432 auto option_it = options->find(name);
433 if(option_it == options->end())
435 return std::vector<T>();
438 return ParseValue<std::vector<T>>(option_it->second);
443 template <
typename T>
447 Option(std::string name, std::string description)
460 auto option = options->find(name);
461 if(option != options->end())
463 return ParseValue<T>(option->second);
467 return std::optional<T>();
476 std::vector<Option<void> *> option_list;
481 void InitializeFields()
485 template <
typename ... Args>
486 void InitializeFields(
Option<void>& option, Args& ... args)
488 option_list.push_back(&option);
489 InitializeFields(args...);
493 void ConsiderOptions(std::map<std::string, std::string>& option_map)
495 for(
auto option : option_list)
497 option->options = &option_map;
A representation of an enumeration with associated string representations for each value.
Definition options.h:15
std::map< value_type, std::vector< std::string > > values
Maps each value to a sequence of valid strings.
Definition options.h:23
std::map< std::string, value_type, CaseInsensitiveLess > names
Maps each name to its value.
Definition options.h:26
std::map< value_type, std::string > descriptions
An empty dictionary that explains the value types in detail.
Definition options.h:20
std::optional< value_type > LookupName(std::string name) const
Searches (in a case-insensitive way) for a string.
Definition options.h:68
Represents an instance of an Enumeration type.
Definition options.h:84
Helper class that contains the options interpreted by the format.
Definition options.h:474
std::string name
The name of a command line option, as provided on the command line.
Definition options.h:244
virtual std::string type_name()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:264
std::string description
Description printed when -h is issued.
Definition options.h:246
A typed option description, used for documenting options.
Definition options.h:273
std::string type_name() override
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:349
ItemOf< T > operator()()
Retrieve the provided value, parsed.
Definition options.h:355
T::value_type default_value
Value of the option if not provided on the command line.
Definition options.h:342
bool operator()()
Retrieve the provided value, parsed.
Definition options.h:408
std::string type_name() override
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:402
std::string type_name() override
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:452
std::optional< T > operator()()
Retrieve the provided value, parsed.
Definition options.h:458
std::string type_name() override
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:424
std::vector< T > operator()()
Retrieve the provided value, parsed.
Definition options.h:430
std::map< std::string, std::string > * options
Reference to the collection of command line options, to be accessed by the Option instance.
Definition options.h:295
Documents and handles command line options.
Definition options.h:306
T operator()()
Retrieve the provided value, parsed.
Definition options.h:322
T default_value
Value of the option if not provided on the command line.
Definition options.h:309
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:229
static ItemOf< T > ParseValue(std::string value)
Parses a string value.
Definition options.h:216
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:156
static bool ParseValue(std::string value)
Parses a string value.
Definition options.h:150
static offset_t ParseValue(std::string value)
Parses a string value.
Definition options.h:125
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:139
static std::optional< T > ParseValue(std::string value)
Parses a string value.
Definition options.h:195
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:201
static std::string ParseValue(std::string value)
Parses a string value.
Definition options.h:108
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:114
static std::vector< T > ParseValue(std::string value)
Parses a string value.
Definition options.h:167
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:182
Helper template to parse and display type of command line options.
Definition options.h:94