Ookii.CommandLine for C++  1.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 
8 #include "format_helper.h"
9 
10 namespace ookii
11 {
12  namespace details
13  {
14  template<typename CharType>
15  struct error_formats
16  {
17  static constexpr auto invalid_value = literal_cast<CharType>("The value provided for the argument '{}' was invalid.");
18  static constexpr auto unknown_argument = literal_cast<CharType>("Unknown argument name '{}'.");
19  static constexpr auto missing_value = literal_cast<CharType>("No value was supplied for the argument '{}'.");
20  static constexpr auto duplicate_argument = literal_cast<CharType>("The argument '{}' was supplied more than once.");
21  static constexpr auto too_many_arguments = literal_cast<CharType>("Too many arguments were supplied.");
22  static constexpr auto missing_required_argument = literal_cast<CharType>("The required argument '{}' was not supplied.");
23  static constexpr auto unknown = literal_cast<CharType>("An unknown error has occurred.");
24  };
25  }
26 
28  enum class parse_error
29  {
31  none,
32 
39 
42 
45 
48 
52 
55 
58  };
59 
72  template<typename CharType, typename Traits = std::char_traits<CharType>, typename Alloc = std::allocator<CharType>>
73  struct [[nodiscard]] parse_result
74  {
76  using string_type = std::basic_string<CharType, Traits, Alloc>;
77 
84  parse_result(parse_error error = parse_error::none, string_type error_arg_name = {})
85  : error{error},
86  error_arg_name{error_arg_name}
87  {
88  }
89 
92 
96 
99  operator bool() const noexcept
100  {
101  return error == parse_error::none;
102  }
103 
111  string_type get_error_message(const std::locale &loc = {}) const
112  {
113  switch (error)
114  {
115  case parse_error::none:
117  return {};
118 
120  return OOKII_FMT_NS format(loc, details::error_formats<CharType>::invalid_value.data(), error_arg_name);
121 
123  return OOKII_FMT_NS format(loc, details::error_formats<CharType>::unknown_argument.data(), error_arg_name);
124 
126  return OOKII_FMT_NS format(loc, details::error_formats<CharType>::missing_value.data(), error_arg_name);
127 
129  return OOKII_FMT_NS format(loc, details::error_formats<CharType>::duplicate_argument.data(), error_arg_name);
130 
132  return OOKII_FMT_NS format(loc, details::error_formats<CharType>::too_many_arguments.data(), error_arg_name);
133 
135  return OOKII_FMT_NS format(loc, details::error_formats<CharType>::missing_required_argument.data(), error_arg_name);
136 
137  default:
138  return OOKII_FMT_NS format(loc, details::error_formats<CharType>::unknown.data(), error_arg_name);
139  }
140  }
141  };
142 }
143 
144 #endif
Allows selection between the C++20 <format> library and libfmt.
#define OOKII_FMT_NS
The namespace which contains the formatting library.
Definition: format_helper.h:34
Namespace containing the core Ookii.CommandLine.Cpp types.
Definition: command_line_argument.h:16
parse_error
The type of error that occurred while parsing the command line.
Definition: parse_result.h:29
@ 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::cancel_parsing(),...
@ 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.
Provides the result, success or error, of a command line argument parsing operation.
Definition: parse_result.h:74
string_type get_error_message(const std::locale &loc={}) const
Gets a default, English language error message for the current error.
Definition: parse_result.h:111
parse_error error
The type of error that occurred, or parse_error::none to indicate no error.
Definition: parse_result.h:91
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:95
parse_result(parse_error error=parse_error::none, string_type error_arg_name={})
Initializes a new instance of the parse_result structure.
Definition: parse_result.h:84
std::basic_string< CharType, Traits, Alloc > string_type
The concrete string type used by this structure.
Definition: parse_result.h:76