library of assembled shared sources

http://lass.cocamware.com

arg_parser.h

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 
00044 /** @defgroup ArgParser ArgParser
00045  *  @brief library to parse GNU style program arguments
00046  *  @author Bram de Greve [BdG]
00047  */
00048 
00049 
00050 
00051 #ifndef LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_H
00052 #define LASS_GUARDIAN_OF_INCLUSION_IO_ARG_PARSER_H
00053 
00054 #include "io_common.h"
00055 #include "../util/call_traits.h"
00056 #include "../num/safe_bool.h"
00057 
00058 namespace lass
00059 {
00060 namespace io
00061 {
00062 
00063 class ArgParameter;
00064 
00065 
00066 
00067 /** @ingroup ArgParser
00068  */
00069 enum ArgMode
00070 {
00071     amNoValue = 1,          /**< argument takes no value (argument is flag) */
00072     amRequired = 2,         /**< if argument is used, a value is required */
00073     amOptional = 4,         /**< if argument is used, it can take a value, but is not required */
00074     amMultiple = 8,         /**< argument can have multiple values. */
00075 
00076     amDefault = amRequired  /**< default mode for arguments. */
00077 };
00078 
00079 
00080 
00081 /** @ingroup ArgParser
00082  *  @brief structure to store ArgFlag and ArgValue formats
00083  */
00084 struct ArgFormat
00085 {
00086     std::string shortName;      /**< short name of ArgFlag or ArgValue */
00087     std::string longName;       /**< long name of ArgFlag or ArgValue */
00088     std::string description;    /**< set description of value, unused by ArgFlag */
00089     int argMode;                /**< set argument mode for value, unused by ArgFlag */
00090     bool hasDefault;            /**< set true if ArgValue has default value, unused by ArgFlag */
00091     std::string defaultValue;   /**< only valid if @e hasDefault is true, unused by ArgFlag */
00092 };
00093 
00094 
00095 
00096 /** @ingroup ArgParser
00097  *  @brief the parser itself
00098  */
00099 class LASS_DLL ArgParser
00100 {
00101 public:
00102 
00103     typedef std::vector<std::string> TArguments;
00104 
00105     ArgParser();
00106     ArgParser(const std::string& iProgramName,
00107               const std::string& iVersion = "",
00108               const std::string& iPositionalArguments = "");
00109 
00110     bool parse(const std::string& iArguments, TArguments* oPositionals = 0);
00111     bool parse(const TArguments& iArguments, TArguments* oPositionals = 0);
00112     bool parse(int iArgc, char* iArgv[], TArguments* oPositionals = 0);
00113 
00114     std::string usage() const;
00115 
00116 private:
00117 
00118     friend class ArgParameter;
00119 
00120     typedef std::vector<ArgParameter*> TParameters;
00121     typedef TParameters::size_type TSize;
00122 
00123     void subscribe(ArgParameter& iArg);
00124     bool parseShort(const TArguments& iArguments, TSize& ioIndex);
00125     bool parseLong(const TArguments& iArguments, TSize iIndex);
00126     bool isValidLongName(const std::string& iLongName) const;
00127     bool writeVersionOrHelp(const std::string& iArgument) const;
00128     void writeVersion() const;
00129     void writeHelp() const;
00130 
00131     std::string programName_;
00132     std::string programVersion_;
00133     std::string positionals_;
00134     TParameters parameters_;
00135     bool isQuiet_;
00136 };
00137 
00138 
00139 
00140 /** @ingroup ArgParser
00141  */
00142 class LASS_DLL ArgParameter
00143 {
00144 public:
00145 
00146     virtual ~ArgParameter();
00147 
00148     const std::string& shortName() const;
00149     const std::string& longName() const;
00150     const int mode() const;
00151     const std::string format() const;
00152 
00153     bool operator!() const;
00154     operator num::SafeBool() const;
00155 
00156 protected:
00157 
00158     ArgParameter(ArgParser& iParser,
00159                  const std::string& iShortName,
00160                  const std::string& iLongName,
00161                  int iArgMode = amDefault);
00162 
00163     const std::string names() const;
00164     const bool parserIsQuiet() const;
00165     const bool setValue(const std::string& iValue);
00166     void set();
00167 
00168 private:
00169 
00170     friend class ArgParser;
00171 
00172     virtual const std::string doFormat() const;
00173     virtual const bool doSetValue(const std::string& iValue);
00174 
00175     ArgParser& parser_;
00176     std::string shortName_;
00177     std::string longName_;
00178     int mode_;
00179     bool isSet_;
00180 };
00181 
00182 
00183 
00184 /** @ingroup ArgParser
00185  */
00186 class LASS_DLL ArgFlag: public ArgParameter
00187 {
00188 public:
00189 
00190     ArgFlag(ArgParser& iParser,
00191             const std::string& iShortName,
00192             const std::string& iLongName);
00193     ArgFlag(ArgParser& iParser,
00194             const ArgFormat& iFormat);
00195 };
00196 
00197 
00198 
00199 /** @ingroup ArgParser
00200  */
00201 template <typename T>
00202 class ArgValue: public ArgParameter
00203 {
00204 public:
00205 
00206     typedef typename util::CallTraits<T>::TValue TValue;
00207     typedef typename util::CallTraits<T>::TParam TParam;
00208     typedef typename util::CallTraits<T>::TConstReference TConstReference;
00209     typedef std::vector<TValue> TValues;
00210     typedef typename TValues::const_iterator TValueIterator;
00211 
00212     ArgValue(ArgParser& iParser,
00213              const std::string& iShortName,
00214              const std::string& iLongName,
00215              const std::string& iDescription = "",
00216              int iArgMode = amDefault);
00217     ArgValue(ArgParser& iParser,
00218              const std::string& iShortName,
00219              const std::string& iLongName,
00220              const std::string& iDescription,
00221              int iArgMode,
00222              TParam iDefault);
00223     ArgValue(ArgParser& iParser,
00224              const ArgFormat& iFormat);
00225 
00226     const TValues& all() const;
00227     size_t size() const;
00228     typename ArgValue::TConstReference operator[](size_t iIndex) const;
00229     typename ArgValue::TConstReference at(size_t iIndex) const;
00230     TValueIterator begin() const;
00231     TValueIterator end() const;
00232 
00233 private:
00234 
00235 
00236     virtual const std::string doFormat() const;
00237     virtual const bool doSetValue(const std::string& iValue);
00238 
00239     void checkMode(int iArgMode) const;
00240 
00241     TValues values_;
00242     TValues default_;
00243     std::string description_;
00244 };
00245 
00246 
00247 
00248 
00249 /** @ingroup ArgParser
00250  *  Exception thrown in case of bad arguments.
00251  */
00252 class ArgBadArgument: public util::Exception
00253 {
00254 public:
00255     ArgBadArgument(const std::string& msg, const std::string& loc): Exception(msg, loc) {}
00256 private:
00257     LASS_UTIL_EXCEPTION_PRIVATE_IMPL(ArgBadArgument)
00258 };
00259 
00260 
00261 
00262 
00263 }
00264 
00265 }
00266 
00267 #include "arg_parser.inl"
00268 
00269 #endif
00270 
00271 // 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