From 3ee566fddc63f8f4acb701f89df84a201b294dd2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Marc=20W=C3=A4ckerlin?= Date: Tue, 8 Feb 2005 12:31:36 +0000 Subject: [PATCH] new static methods to simplify access to options --- mrw/arg.cpp | 5 +- mrw/arg.hpp | 167 ++++++++++++++++++++++++++++++++++++++++++++++++++-- 2 files changed, 166 insertions(+), 6 deletions(-) diff --git a/mrw/arg.cpp b/mrw/arg.cpp index b8c960a..de8447f 100644 --- a/mrw/arg.cpp +++ b/mrw/arg.cpp @@ -9,6 +9,9 @@ @license LGPL, see file COPYING $Log$ + Revision 1.3 2005/02/08 12:31:36 marc + new static methods to simplify access to options + Revision 1.2 2004/11/25 18:26:04 marc Constness corrected @@ -65,7 +68,7 @@ namespace mrw { <COPYING $Log$ + Revision 1.6 2005/02/08 12:31:36 marc + new static methods to simplify access to options + Revision 1.5 2004/11/25 18:26:04 marc Constness corrected @@ -106,13 +109,14 @@ namespace mrw { ... // example usage of simple option if (mrw::Args::instance().find('v')) // be verbose here + if (mrw::Args::have('v')) // the same, simpler: be verbose here ... // example usage of option with one parameter - ifstream file(mrw::Args::instance().find('o')[0]->toString().c_str()); + ifstream file(mrw::Args::toString('o').c_str()); ... // example usage of option with two parameter - int x = mrw::Args::instance().find('c')[0]->toInt().c_str()); - int y = mrw::Args::instance().find('c')[1]->toInt().c_str()); + int x = mrw::Args::instance().find('c')[0]->toInt(); // first integer + int y = mrw::Args::toInt('c', 1); // second; alternative, simpler access ... return 0 } @@ -420,10 +424,11 @@ namespace mrw { /** @brief find out, whether this option was set by the user Example: Check whether the user has set the @c -v option for - verbose output: + verbose output, there are two ways for doing it: @code if (mrw::Args::instance().find('v')) // -v is set + if (mrw::Args::have('v')) // -v is set @endcode @return @@ -494,6 +499,156 @@ namespace mrw { return _instance; } + /** @brief check a simple option + + This is a shortcut. Instead of the expression: + @code + if (mrw::Args::instance().find('x')) ... + @endcode + you can simply write the expression: + @code + if (mrw::Args::have('x')) ... + @endcode + It is exactly the same. + + @param c the short name of the parameter + + @throw mrw::out_of_range if the parameter is not available + (this would be a coding error) + */ + static bool have(char c) throw(std::exception) { + return mrw::Args::instance().find(c); + } + + /** @brief get a simple string parameter + + This is a shortcut. Instead of the expression: + @code + mrw::Args::instance().find('x')[0].toString() + @endcode + you can simply write the expression: + @code + mrw::Args::toString('x') + @endcode + It is exactly the same. + + @param c the short name of the parameter + + @throw mrw::bad_cast if the parameter is not a string + @throw mrw::out_of_range if the parameter is empty or not available + */ + static const std::string& toString(char c) throw(std::exception) { + return mrw::Args::instance().find(c)[0]->toString(); + } + + /** @brief get a simple integer parameter + + This is a shortcut. Instead of the expression: + @code + mrw::Args::instance().find('x')[0].toInt() + @endcode + you can simply write the expression: + @code + mrw::Args::toInt('x') + @endcode + It is exactly the same. + + @param c the short name of the parameter + + @throw mrw::bad_cast if the parameter is not an int + @throw mrw::out_of_range if the parameter is empty or not available + */ + static int toInt(char c) throw(std::exception) { + return mrw::Args::instance().find(c)[0]->toInt(); + } + + /** @brief get a simple boolean parameter + + This is a shortcut. Instead of the expression: + @code + mrw::Args::instance().find('x')[0].toBool() + @endcode + you can simply write the expression: + @code + mrw::Args::toBool('x') + @endcode + It is exactly the same. + + @param c the short name of the parameter + + @throw mrw::bad_cast if the parameter is not an bool + @throw mrw::out_of_range if the parameter is empty or not available + */ + static bool toBool(char c) throw(std::exception) { + return mrw::Args::instance().find(c)[0]->toBool(); + } + + /** @brief get the n-th string parameter + + This is a shortcut. Instead of the expression: + @code + mrw::Args::instance().find('x')[n].toString() + @endcode + you can simply write the expression: + @code + mrw::Args::toString('x', n) + @endcode + It is exactly the same. + + @param c the short name of the parameter + @param n the number of the parameter (starting at 0) + + @throw mrw::bad_cast if the parameter is not a string + @throw mrw::out_of_range if the parameter is not available + */ + static const std::string& toString(char c, int n) throw(std::exception) { + return mrw::Args::instance().find(c)[n]->toString(); + } + + /** @brief get the n-th integer parameter + + This is a shortcut. Instead of the expression: + @code + mrw::Args::instance().find('x')[n].toInt() + @endcode + you can simply write the expression: + @code + mrw::Args::toInt('x', n) + @endcode + It is exactly the same. + + @param c the short name of the parameter + @param n the number of the parameter (starting at 0) + + @throw mrw::bad_cast if the parameter is not an int + @throw mrw::out_of_range if the parameter is not available + */ + static int toInt(char c, int n) throw(std::exception) { + return mrw::Args::instance().find(c)[n]->toInt(); + } + + /** @brief get the n-th boolean parameter + + This is a shortcut. Instead of the expression: + @code + mrw::Args::instance().find('x')[n].toBool() + @endcode + you can simply write the expression: + @code + mrw::Args::toBool('x', n) + @endcode + It is exactly the same. + + @param c the short name of the parameter + @param n the number of the parameter (starting at 0) + + @throw mrw::bad_cast if the parameter is not an bool + @throw mrw::out_of_range if the parameter not available + */ + static bool toBool(char c, int n) throw(std::exception) { + return mrw::Args::instance().find(c)[n]->toBool(); + } + /** @brief setup an acceptable option Setup an acceptable user option. @@ -504,8 +659,10 @@ namespace mrw { mrw::Args::instance() <