Ookii.CommandLine for C++  2.0.0
parse_result.h
Go to the documentation of this file.
1 #ifndef OOKII_PARSE_RESULT_H_
4 #define OOKII_PARSE_RESULT_H_
5 
6 #pragma once
7 
9 
10 namespace ookii
11 {
13  enum class parse_error
14  {
16  none,
17 
24 
27 
30 
33 
37 
40 
43 
46  };
47 
60  template<typename CharType, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
61  struct [[nodiscard]] parse_result
62  {
64  using string_type = std::basic_string<CharType, Traits, Alloc>;
67 
75  parse_result(const string_provider_type &string_provider, parse_error error = parse_error::none, string_type error_arg_name = {})
76  : string_provider{&string_provider},
77  error{error},
78  error_arg_name{error_arg_name}
79  {
80  }
81 
84 
87 
91 
94  operator bool() const noexcept
95  {
96  return error == parse_error::none;
97  }
98 
106  {
107  switch (error)
108  {
109  case parse_error::none:
111  return {};
112 
114  return string_provider->invalid_value(error_arg_name);
115 
117  return string_provider->unknown_argument(error_arg_name);
118 
120  return string_provider->missing_value(error_arg_name);
121 
123  return string_provider->duplicate_argument(error_arg_name);
124 
126  return string_provider->too_many_arguments();
127 
129  return string_provider->missing_required_argument(error_arg_name);
130 
132  return string_provider->combined_short_name_non_switch(error_arg_name);
133 
134  default:
135  return string_provider->unknown_error();
136  }
137  }
138  };
139 }
140 
141 #endif
Provides custom localized strings.
Definition: localized_string_provider.h:30
virtual string_type duplicate_argument(string_view_type argument_name) const
Gets the error message for parse_error::duplicate_argument.
Definition: localized_string_provider.h:67
virtual string_type combined_short_name_non_switch(string_view_type argument_name) const
Gets the error message for parse_error::combined_short_name_non_switch.
Definition: localized_string_provider.h:87
virtual string_type too_many_arguments() const
Gets the error message for parse_error::too_many_arguments.
Definition: localized_string_provider.h:73
virtual string_type unknown_error() const
Gets the error message for parse_error::unknown.
Definition: localized_string_provider.h:93
virtual string_type invalid_value(string_view_type argument_name) const
Gets the error message for parse_error::invalid_value.
Definition: localized_string_provider.h:46
virtual string_type unknown_argument(string_view_type argument_name) const
Gets the error message for parse_error::unknown_argument.
Definition: localized_string_provider.h:53
virtual string_type missing_value(string_view_type argument_name) const
Gets the error message for parse_error::missing_value.
Definition: localized_string_provider.h:60
virtual string_type missing_required_argument(string_view_type argument_name) const
Gets the error message for parse_error::missing_required_argument.
Definition: localized_string_provider.h:80
Provides the ookii::basic_localized_string_provider class.
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
@ error
There was an error converting the value to the element type of the argument.
parse_error
The type of error that occurred while parsing the command line.
Definition: parse_result.h:14
@ invalid_value
A supplied value could not be converted to the argument's type.
@ parsing_cancelled
Parsing was cancelled by an argument using basic_parser_builder::argument_builder_common::cancel_pars...
@ none
No error occurred.
@ unknown_argument
An argument name was supplied that doesn't exist.
@ missing_required_argument
One of the required arguments was not supplied.
@ duplicate_argument
An argument, other than a multi-value argument, was supplied more than once, and basic_parser_builder...
@ missing_value
A named argument, other than a switch argument, was supplied without a value.
@ too_many_arguments
More positional arguments were supplied than were defined.
@ combined_short_name_non_switch
A combined short argument contains an argument that is not a switch.
Provides the result, success or error, of a command line argument parsing operation.
Definition: parse_result.h:62
const string_provider_type * string_provider
The string provider used to get error messages.
Definition: parse_result.h:83
parse_error error
The type of error that occurred, or parse_error::none to indicate no error.
Definition: parse_result.h:86
parse_result(const string_provider_type &string_provider, parse_error error=parse_error::none, string_type error_arg_name={})
Initializes a new instance of the parse_result structure.
Definition: parse_result.h:75
string_type error_arg_name
The name of the argument that caused the error, or a blank string if there was no error or the error ...
Definition: parse_result.h:90
string_type get_error_message() const
Gets a default, English language error message for the current error.
Definition: parse_result.h:105
std::basic_string< CharType, Traits, Alloc > string_type
The concrete string type used by this structure.
Definition: parse_result.h:64