13 template <
typename Enum>
17 typedef Enum value_type;
20 std::map<value_type, std::vector<std::string>>
values;
23 std::map<std::string, value_type, CaseInsensitiveLess>
names;
25 void init(value_type next_value)
29 void init(value_type next_value, std::string name)
31 names[name] = next_value;
33 values[next_value] = std::vector<std::string>(1, name);
35 values[next_value].push_back(name);
38 template <
typename ... Args>
39 void init(value_type next_value, std::string name, std::string next_name, Args ... args)
41 init(next_value, name);
42 init(value_type(next_value + 1), next_name, args...);
45 void init(value_type _, std::string name, value_type actual_next_value)
47 init(actual_next_value, name);
50 template <
typename ... Args>
51 void init(value_type _, std::string name, value_type actual_next_value, std::string next_name, Args ... args)
53 init(actual_next_value, name);
54 init(value_type(actual_next_value + 1), next_name, args...);
58 template <
typename ... Args>
59 Enumeration(Args ... args)
61 init(value_type(0), args...);
65 std::optional<value_type>
LookupName(std::string name)
const
67 auto it =
names.find(name);
69 return std::optional<value_type>();
71 return std::make_optional(it->second);
83 typedef typename T::value_type value_type;
85 ItemOf(value_type value = value_type()) : value(value) { }
86 operator value_type()
const {
return value; }
95 T ParseValue(std::string value)
126 return std::stoll(value,
nullptr, 0);
128 catch(std::invalid_argument& a)
130 Linker::Error <<
"Error: Unable to parse " << value <<
", ignoring" << std::endl;
149 return value !=
"0" && value !=
"false" && value !=
"no" && value ==
"off";
160 template <
typename T>
166 std::vector<T> result;
167 size_t string_offset = 0;
169 while((comma = value.find(
',', string_offset)) != std::string::npos)
171 result.push_back(Linker::ParseValue<T>(value.substr(string_offset, comma - string_offset)));
172 string_offset = comma + 1;
174 result.push_back(Linker::ParseValue<T>(value.substr(string_offset)));
181 std::ostringstream oss;
188 template <
typename T>
194 return std::optional<T>(Linker::ParseValue<T>(value));
200 std::ostringstream oss;
207 template <
typename T>
210 static T enumeration;
215 if(
auto enum_value = enumeration.LookupName(value))
232 template <
typename T>
233 class OptionDescription;
246 : name(name), description(description)
252 virtual void PrintDetails(std::ostream& out, std::string indentation)
268 template <
typename T>
277 std::string type_name()
override
283 template <
typename T>
294 Option(std::string name, std::string description)
301 template <
typename T>
313 std::string type_name()
override
321 auto option_it = options->find(name);
322 if(option_it != options->end())
324 return ParseValue<T>(option_it->second);
334 template <
typename T>
341 Option(std::string name, std::string description, T::value_type
default_value =
typename T::value_type())
348 return "string option";
354 auto option_it = options->find(name);
355 if(option_it != options->end())
357 return ParseValue<ItemOf<T>>(option_it->second);
365 void PrintDetails(std::ostream& out, std::string indentation)
override
367 out << indentation <<
"permitted values:" << std::endl;
370 out << indentation <<
"\t";
371 bool started =
false;
372 for(
auto name : pair.second)
390 Option(std::string name, std::string description)
403 return options->find(name) != options->end();
408 template <
typename T>
412 Option(std::string name, std::string description)
425 auto option_it = options->find(name);
426 if(option_it == options->end())
428 return std::vector<T>();
431 return ParseValue<std::vector<T>>(option_it->second);
436 template <
typename T>
440 Option(std::string name, std::string description)
453 auto option = options->find(name);
454 if(option != options->end())
456 return ParseValue<T>(option->second);
460 return std::optional<T>();
469 std::vector<Option<void> *> option_list;
474 void InitializeFields()
478 template <
typename ... Args>
479 void InitializeFields(
Option<void>& option, Args& ... args)
481 option_list.push_back(&option);
482 InitializeFields(args...);
486 void ConsiderOptions(std::map<std::string, std::string>& option_map)
488 for(
auto option : option_list)
490 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:20
std::map< std::string, value_type, CaseInsensitiveLess > names
Maps each name to its value.
Definition options.h:23
std::optional< value_type > LookupName(std::string name) const
Searches (in a case-insensitive way) for a string.
Definition options.h:65
Represents an instance of an Enumeration type.
Definition options.h:81
Helper class that contains the options interpreted by the format.
Definition options.h:467
std::string name
The name of a command line option, as provided on the command line.
Definition options.h:241
virtual std::string type_name()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:261
std::string description
Description printed when -h is issued.
Definition options.h:243
A typed option description, used for documenting options.
Definition options.h:270
std::string type_name() override
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:346
ItemOf< T > operator()()
Retrieve the provided value, parsed.
Definition options.h:352
T::value_type default_value
Value of the option if not provided on the command line.
Definition options.h:339
bool operator()()
Retrieve the provided value, parsed.
Definition options.h:401
std::string type_name() override
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:395
std::string type_name() override
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:445
std::optional< T > operator()()
Retrieve the provided value, parsed.
Definition options.h:451
std::string type_name() override
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:417
std::vector< T > operator()()
Retrieve the provided value, parsed.
Definition options.h:423
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:292
Documents and handles command line options.
Definition options.h:303
T operator()()
Retrieve the provided value, parsed.
Definition options.h:319
T default_value
Value of the option if not provided on the command line.
Definition options.h:306
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:226
static ItemOf< T > ParseValue(std::string value)
Parses a string value.
Definition options.h:213
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:153
static bool ParseValue(std::string value)
Parses a string value.
Definition options.h:147
static offset_t ParseValue(std::string value)
Parses a string value.
Definition options.h:122
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:136
static std::optional< T > ParseValue(std::string value)
Parses a string value.
Definition options.h:192
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:198
static std::string ParseValue(std::string value)
Parses a string value.
Definition options.h:105
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:111
static std::vector< T > ParseValue(std::string value)
Parses a string value.
Definition options.h:164
static std::string GetTypeName()
Returns a textual representation of the type, to be displayed to the user.
Definition options.h:179
Helper template to parse and display type of command line options.
Definition options.h:91