blocxx

CmdLineParser.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2005, Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2006, Novell, Inc. All rights reserved.
00004 * 
00005 * Redistribution and use in source and binary forms, with or without
00006 * modification, are permitted provided that the following conditions are met:
00007 * 
00008 *     * Redistributions of source code must retain the above copyright notice,
00009 *       this list of conditions and the following disclaimer.
00010 *     * Redistributions in binary form must reproduce the above copyright
00011 *       notice, this list of conditions and the following disclaimer in the
00012 *       documentation and/or other materials provided with the distribution.
00013 *     * Neither the name of 
00014 *       Vintela, Inc., 
00015 *       nor Novell, Inc., 
00016 *       nor the names of its contributors or employees may be used to 
00017 *       endorse or promote products derived from this software without 
00018 *       specific prior written permission.
00019 * 
00020 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00021 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00022 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00023 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00024 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00025 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00026 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00027 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00028 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00029 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00030 * POSSIBILITY OF SUCH DAMAGE.
00031 *******************************************************************************/
00032 
00033 
00038 #ifndef BLOCXX_CMD_LINE_PARSER_HPP_INCLUDE_GUARD_
00039 #define BLOCXX_CMD_LINE_PARSER_HPP_INCLUDE_GUARD_
00040 #include "blocxx/BLOCXX_config.h"
00041 #include "blocxx/String.hpp"
00042 #include "blocxx/SortedVectorMap.hpp"
00043 #include "blocxx/Array.hpp"
00044 #include "blocxx/Exception.hpp"
00045 
00046 namespace BLOCXX_NAMESPACE
00047 {
00048 
00049 BLOCXX_DECLARE_APIEXCEPTION(CmdLineParser, BLOCXX_COMMON_API)
00050 
00051 
00058 class BLOCXX_COMMON_API CmdLineParser
00059 {
00060 public:
00061    enum EArgumentTypeFlag
00062    {
00063       E_NO_ARG,       
00064       E_REQUIRED_ARG, 
00065       E_OPTIONAL_ARG  
00066    };
00067 
00068    // errors codes that may be specified when a CmdLineParserException is thrown
00069    enum EErrorCodes
00070    {
00071       E_INVALID_OPTION,         
00072       E_MISSING_ARGUMENT,       
00073       E_INVALID_NON_OPTION_ARG, 
00074       E_MISSING_OPTION          
00075    };
00076 
00077    struct Option
00078    {
00079       int id;                    
00080       char shortopt;             
00081       const char* longopt;       
00082       EArgumentTypeFlag argtype; 
00083       const char* defaultValue;  
00084       const char* description;   
00085    };
00086 
00087    enum EAllowNonOptionArgsFlag
00088    {
00089       E_NON_OPTION_ARGS_ALLOWED, 
00090       E_NON_OPTION_ARGS_INVALID  
00091    };
00092 
00102    CmdLineParser(int argc, char const* const* const argv, const Option* options, EAllowNonOptionArgsFlag allowNonOptionArgs);
00103 
00112    String getOptionValue(int id, const char* defaultValue = "") const;
00113    
00125    String mustGetOptionValue(int id, const char* exceptionMessage = "") const;
00126    
00133    StringArray getOptionValueList(int id) const;
00134    
00144    StringArray mustGetOptionValueList(int id, const char* exceptionMessage = "") const;
00145    
00149    bool isSet(int id) const;
00150    
00154    size_t getNonOptionCount () const;
00155    
00160    String getNonOptionArg(size_t n) const;
00161 
00165    StringArray getNonOptionArgs() const;
00166    
00185    static String getUsage(const Option* options, unsigned int maxColumns = 80);
00186 
00187 
00188 private:
00189 
00190 #ifdef BLOCXX_WIN32
00191 #pragma warning (push)
00192 #pragma warning (disable: 4251)
00193 #endif
00194 
00195    // key is Option::id, value is the value(s) specified by the user
00196    typedef SortedVectorMap<int, StringArray> optionsMap_t;
00197    optionsMap_t m_parsedOptions;
00198    StringArray m_nonOptionArgs;
00199 
00200 #ifdef BLOCXX_WIN32
00201 #pragma warning (pop)
00202 #endif
00203 
00204 };
00205 
00206 } // end namespace BLOCXX_NAMESPACE
00207 
00208 #endif
00209 
00210