new static methods to simplify access to options

master
Marc Wäckerlin 20 years ago
parent 7316f2fdd9
commit 3ee566fddc
  1. 5
      mrw/arg.cpp
  2. 167
      mrw/arg.hpp

@ -9,6 +9,9 @@
@license LGPL, see file <a href="license.html">COPYING</a> @license LGPL, see file <a href="license.html">COPYING</a>
$Log$ $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 Revision 1.2 2004/11/25 18:26:04 marc
Constness corrected Constness corrected
@ -65,7 +68,7 @@ namespace mrw {
<<i<<'<'<<_params.size())).str()); <<i<<'<'<<_params.size())).str());
} }
Args& Args::operator<<(const mrw::Opt& opt) throw(std::invalid_argument) { Args& Args::operator<<(const mrw::Opt& opt) throw(std::exception) {
// twice the same, but other sort order // twice the same, but other sort order
Options::iterator it(_options.insert(_options.end(), opt)); Options::iterator it(_options.insert(_options.end(), opt));
if (!_shortopts.insert(ShortOpts::value_type(opt._shortname, it)).second) if (!_shortopts.insert(ShortOpts::value_type(opt._shortname, it)).second)

@ -9,6 +9,9 @@
@license LGPL, see file <a href="license.html">COPYING</a> @license LGPL, see file <a href="license.html">COPYING</a>
$Log$ $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 Revision 1.5 2004/11/25 18:26:04 marc
Constness corrected Constness corrected
@ -106,13 +109,14 @@ namespace mrw {
... ...
// example usage of simple option // example usage of simple option
if (mrw::Args::instance().find('v')) // be verbose here 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 // 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 // example usage of option with two parameter
int x = mrw::Args::instance().find('c')[0]->toInt().c_str()); int x = mrw::Args::instance().find('c')[0]->toInt(); // first integer
int y = mrw::Args::instance().find('c')[1]->toInt().c_str()); int y = mrw::Args::toInt('c', 1); // second; alternative, simpler access
... ...
return 0 return 0
} }
@ -420,10 +424,11 @@ namespace mrw {
/** @brief find out, whether this option was set by the user /** @brief find out, whether this option was set by the user
Example: Check whether the user has set the @c -v option for Example: Check whether the user has set the @c -v option for
verbose output: verbose output, there are two ways for doing it:
@code @code
if (mrw::Args::instance().find('v')) // -v is set if (mrw::Args::instance().find('v')) // -v is set
if (mrw::Args::have('v')) // -v is set
@endcode @endcode
@return @return
@ -494,6 +499,156 @@ namespace mrw {
return _instance; 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 /** @brief setup an acceptable option
Setup an acceptable user option. Setup an acceptable user option.
@ -504,8 +659,10 @@ namespace mrw {
mrw::Args::instance() mrw::Args::instance()
<<mrw::Opt('v', "--verbose", "print more information"); <<mrw::Opt('v', "--verbose", "print more information");
@endcode @endcode
@throw mrw::invalid_argument if opt is not setup correctly
*/ */
Args& operator<<(const mrw::Opt& opt) throw(std::invalid_argument); Args& operator<<(const mrw::Opt& opt) throw(std::exception);
/** @brief setup the number of arguments /** @brief setup the number of arguments

Loading…
Cancel
Save