From 30e436bba66673e17728e3ba3291b58dd16c1696 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Sat, 7 Jul 2018 10:46:25 +0200 Subject: Add optional message to invalid_option exception --- cli/options.cxx | 3 +++ cli/options.hxx | 7 ++++++- cli/options.ixx | 13 +++++++++++-- cli/runtime-header.cxx | 7 ++++++- cli/runtime-inline.cxx | 14 +++++++++++--- cli/runtime-source.cxx | 3 +++ 6 files changed, 40 insertions(+), 7 deletions(-) (limited to 'cli') diff --git a/cli/options.cxx b/cli/options.cxx index b64c26a..809b588 100644 --- a/cli/options.cxx +++ b/cli/options.cxx @@ -91,6 +91,9 @@ namespace cli { os << "invalid value '" << value ().c_str () << "' for option '" << option ().c_str () << "'"; + + if (!message ().empty ()) + os << ": " << message ().c_str (); } const char* invalid_value:: diff --git a/cli/options.hxx b/cli/options.hxx index dae58b6..dce59bd 100644 --- a/cli/options.hxx +++ b/cli/options.hxx @@ -153,7 +153,8 @@ namespace cli ~invalid_value () throw (); invalid_value (const std::string& option, - const std::string& value); + const std::string& value, + const std::string& message = std::string ()); const std::string& option () const; @@ -161,6 +162,9 @@ namespace cli const std::string& value () const; + const std::string& + message () const; + virtual void print (::std::ostream&) const; @@ -170,6 +174,7 @@ namespace cli private: std::string option_; std::string value_; + std::string message_; }; class eos_reached: public exception diff --git a/cli/options.ixx b/cli/options.ixx index 5111030..4c5cfb7 100644 --- a/cli/options.ixx +++ b/cli/options.ixx @@ -84,8 +84,11 @@ namespace cli // inline invalid_value:: invalid_value (const std::string& option, - const std::string& value) - : option_ (option), value_ (value) + const std::string& value, + const std::string& message) + : option_ (option), + value_ (value), + message_ (message) { } @@ -101,6 +104,12 @@ namespace cli return value_; } + inline const std::string& invalid_value:: + message () const + { + return message_; + } + // file_io_failure // inline file_io_failure:: diff --git a/cli/runtime-header.cxx b/cli/runtime-header.cxx index e9e797a..60e90cc 100644 --- a/cli/runtime-header.cxx +++ b/cli/runtime-header.cxx @@ -170,7 +170,8 @@ generate_runtime_header (context& ctx) << "~invalid_value () throw ();" << endl << "invalid_value (const std::string& option," << endl - << "const std::string& value);" + << "const std::string& value," << endl + << "const std::string& message = std::string ());" << endl << "const std::string&" << endl << "option () const;" @@ -178,6 +179,9 @@ generate_runtime_header (context& ctx) << "const std::string&" << endl << "value () const;" << endl + << "const std::string&" << endl + << "message () const;" + << endl << "virtual void" << endl << "print (" << os_type << "&) const;" << endl @@ -187,6 +191,7 @@ generate_runtime_header (context& ctx) << "private:" << endl << "std::string option_;" << "std::string value_;" + << "std::string message_;" << "};"; os << "class eos_reached: public exception" diff --git a/cli/runtime-inline.cxx b/cli/runtime-inline.cxx index 4f381ea..f6b1cee 100644 --- a/cli/runtime-inline.cxx +++ b/cli/runtime-inline.cxx @@ -113,9 +113,11 @@ generate_runtime_inline (context& ctx) << inl << "invalid_value::" << endl << "invalid_value (const std::string& option," << endl - << "const std::string& value)" << endl - << ": option_ (option)," - << " value_ (value)" + << "const std::string& value," << endl + << "const std::string& message)" << endl + << ": option_ (option)," << endl + << " value_ (value)," << endl + << " message_ (message)" << "{" << "}" @@ -129,6 +131,12 @@ generate_runtime_inline (context& ctx) << "value () const" << "{" << "return value_;" + << "}" + + << inl << "const std::string& invalid_value::" << endl + << "message () const" + << "{" + << "return message_;" << "}"; if (ctx.options.generate_file_scanner ()) diff --git a/cli/runtime-source.cxx b/cli/runtime-source.cxx index fedcbe1..d704386 100644 --- a/cli/runtime-source.cxx +++ b/cli/runtime-source.cxx @@ -109,6 +109,9 @@ generate_runtime_source (context& ctx, bool complete) << "os << \"invalid value '\" << value ().c_str () << \"' for " << "option '\"" << endl << " << option ().c_str () << \"'\";" + << endl + << "if (!message ().empty ())" << endl + << "os << \": \" << message ().c_str ();" << "}" << "const char* invalid_value::" << endl -- cgit v1.1