library of assembled shared sources

http://lass.cocamware.com

arg_parser.inl

Go to the documentation of this file.
00001 /** @file
00002  *  @author Bram de Greve (bramz@users.sourceforge.net)
00003  *  @author Tom De Muer (tomdemuer@users.sourceforge.net)
00004  *
00005  *  *** BEGIN LICENSE INFORMATION ***
00006  *  
00007  *  The contents of this file are subject to the Common Public Attribution License 
00008  *  Version 1.0 (the "License"); you may not use this file except in compliance with 
00009  *  the License. You may obtain a copy of the License at 
00010  *  http://lass.sourceforge.net/cpal-license. The License is based on the 
00011  *  Mozilla Public License Version 1.1 but Sections 14 and 15 have been added to cover 
00012  *  use of software over a computer network and provide for limited attribution for 
00013  *  the Original Developer. In addition, Exhibit A has been modified to be consistent 
00014  *  with Exhibit B.
00015  *  
00016  *  Software distributed under the License is distributed on an "AS IS" basis, WITHOUT 
00017  *  WARRANTY OF ANY KIND, either express or implied. See the License for the specific 
00018  *  language governing rights and limitations under the License.
00019  *  
00020  *  The Original Code is LASS - Library of Assembled Shared Sources.
00021  *  
00022  *  The Initial Developer of the Original Code is Bram de Greve and Tom De Muer.
00023  *  The Original Developer is the Initial Developer.
00024  *  
00025  *  All portions of the code written by the Initial Developer are:
00026  *  Copyright (C) 2004-2007 the Initial Developer.
00027  *  All Rights Reserved.
00028  *  
00029  *  Contributor(s):
00030  *
00031  *  Alternatively, the contents of this file may be used under the terms of the 
00032  *  GNU General Public License Version 2 or later (the GPL), in which case the 
00033  *  provisions of GPL are applicable instead of those above.  If you wish to allow use
00034  *  of your version of this file only under the terms of the GPL and not to allow 
00035  *  others to use your version of this file under the CPAL, indicate your decision by 
00036  *  deleting the provisions above and replace them with the notice and other 
00037  *  provisions required by the GPL License. If you do not delete the provisions above,
00038  *  a recipient may use your version of this file under either the CPAL or the GPL.
00039  *  
00040  *  *** END LICENSE INFORMATION ***
00041  */
00042 
00043 #ifndef LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_INL
00044 #define LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_INL
00045 
00046 #include "io_common.h"
00047 #include "arg_parser.h"
00048 
00049 #include "../util/string_cast.h"
00050 
00051 namespace lass
00052 {
00053 namespace io
00054 {
00055 
00056 // --- ArgValue ------------------------------------------------------------------------------------
00057 
00058 template <typename T>
00059 ArgValue<T>::ArgValue(ArgParser& iParser,
00060                       const std::string& iShortName,
00061                       const std::string& iLongName,
00062                       const std::string& iDescription,
00063                       int iArgMode):
00064     ArgParameter(iParser, iShortName, iLongName, iArgMode),
00065     description_(iDescription)
00066 {
00067     checkMode(iArgMode);
00068 }
00069 
00070 
00071 
00072 template <typename T>
00073 ArgValue<T>::ArgValue(ArgParser& iParser,
00074                       const std::string& iShortName,
00075                       const std::string& iLongName,
00076                       const std::string& iDescription,
00077                       int iArgMode,
00078                       TParam iDefault):
00079     ArgParameter(iParser, iShortName, iLongName, iArgMode),
00080     default_(1, iDefault),
00081     description_(iDescription)
00082 {
00083     checkMode(iArgMode);
00084 }
00085 
00086 
00087 
00088 template <typename T>
00089 ArgValue<T>::ArgValue(ArgParser& iParser,
00090                       const ArgFormat& iFormat):
00091     ArgParameter(iParser, iFormat.shortName, iFormat.longName, iFormat.argMode),
00092     description_(iFormat.description)
00093 {
00094     if (iFormat.hasDefault)
00095     {
00096         default_.push_back(util::stringCast<T>(iFormat.defaultValue));
00097     }
00098     checkMode(iFormat.argMode);
00099 }
00100 
00101 
00102 
00103 /** return all values
00104  */
00105 template <typename T>
00106 const typename ArgValue<T>::TValues& ArgValue<T>::all() const
00107 {
00108     return !values_.empty() ? values_ : default_;
00109 }
00110 
00111 
00112 
00113 /** return number of values that are stored for this argument.
00114  */
00115 template <typename T>
00116 size_t ArgValue<T>::size() const
00117 {
00118     return all().size();
00119 }
00120 
00121 
00122 
00123 /** return iIndex'ed value.
00124  */
00125 template <typename T>
00126 typename ArgValue<T>::TConstReference ArgValue<T>::operator[](size_t iIndex) const
00127 {
00128     LASS_ASSERT(iIndex < size());
00129     return all()[iIndex];
00130 }
00131 
00132 
00133 
00134 /** return iIndex'ed value with range check
00135  */
00136 template <typename T>
00137 typename ArgValue<T>::TConstReference ArgValue<T>::at(size_t iIndex) const
00138 {
00139     return all().at(iIndex);
00140 }
00141 
00142 
00143 
00144 /** return iterator to first value
00145  */
00146 template <typename T>
00147 typename ArgValue<T>::TValueIterator ArgValue<T>::begin() const
00148 {
00149     return all().begin();
00150 }
00151 
00152 
00153 
00154 /** return iterator to last value
00155  */
00156 template <typename T>
00157 typename ArgValue<T>::TValueIterator ArgValue<T>::end() const
00158 {
00159     return all().end();
00160 }
00161 
00162 
00163 
00164 /** check if mode is valid
00165  *  amNoValue can't be set, and exactly one of amOptional or amRequired must be set.
00166  */
00167 template <typename T>
00168 void ArgValue<T>::checkMode(int iArgMode) const
00169 {
00170     if (iArgMode & amNoValue)
00171     {
00172         LASS_THROW("You can't set the mode 'amNoValue' for a ArgValue '" << names() << "'.");
00173     }
00174     const int mask = amOptional | amRequired;
00175     const int field = iArgMode & mask;
00176     if (field != amOptional && field != amRequired)
00177     {
00178         LASS_THROW("For a ArgValue '" << names() << "' you must choose between mode 'amOptional' "
00179             << "or 'amRequired'.  At least one of these must be set, but not both.");
00180     }
00181 }
00182 
00183 
00184 
00185 template <typename T>
00186 const std::string ArgValue<T>::doFormat() const
00187 {
00188     LASS_ASSERT(!(mode() & amNoValue));
00189 
00190     std::ostringstream result;
00191     result << "[" << names() << " ";
00192 
00193     if (mode() & amOptional)
00194     {
00195         result << "[";
00196     }
00197     result << "<" << (description_.empty() ? "value" : description_) << ">";
00198     if (mode() & amMultiple)
00199     {
00200         result << " ...";
00201     }
00202     if (mode() & amOptional)
00203     {
00204         result << "]";
00205     }
00206 
00207     result << "]";
00208     return result.str();
00209 }
00210 
00211 
00212 
00213 template <typename T>
00214 const bool ArgValue<T>::doSetValue(const std::string& iValue)
00215 {
00216     LASS_ASSERT(!(mode() & amNoValue));
00217 
00218     if (iValue == "")
00219     {
00220         if (mode() & amRequired)
00221         {
00222             if (!parserIsQuiet())
00223             {
00224                 LASS_THROW_EX(ArgBadArgument,
00225                     "Bad program arguments: you didn't provide a value for the parameter '"
00226                     << names() << "' as required.\n");
00227             }
00228             return false;
00229         }
00230 
00231         //LASS_LOG("parameter '" << names() << "' is set without value");
00232     }
00233     else
00234     {
00235         if (values_.size() != 0 && !(mode() & amMultiple))
00236         {
00237             if (!parserIsQuiet())
00238             {
00239                 LASS_THROW_EX(ArgBadArgument,
00240                     "Bad program arguments: parameter '" << names() << "' already has a value '"
00241                     << values_[0] << "' assigned, you can't assign another value '" << iValue
00242                     << "' because this parameter cannot be multiple valued.\n");
00243             }
00244             return false;
00245         }
00246         else
00247         {
00248             try
00249             {
00250                 values_.push_back(util::stringCast<T>(iValue));
00251             }
00252             catch (const util::BadStringCast&)
00253             {
00254                 if (!parserIsQuiet())
00255                 {
00256                     LASS_THROW_EX(ArgBadArgument,
00257                         "Bad program arguments: could not interpret '" << iValue
00258                         << "' as a value of type '" << typeid(T).name() 
00259                         << "' for parameter '" << names() << ".\n");
00260                 }
00261                 return false;
00262             }
00263 
00264             //LASS_LOG("parameter '" << names() << "' is set, value == '" << values_.back() << "'");
00265         }
00266     }
00267 
00268     set();
00269     return true;
00270 }
00271 
00272 
00273 
00274 }
00275 
00276 }
00277 
00278 #endif
00279 
00280 // EOF

Generated on Mon Nov 10 14:19:59 2008 for Library of Assembled Shared Sources by doxygen 1.5.7.1
SourceForge.net Logo