CmdLineParser.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002 * Copyright (C) 2004 Vintela, Inc. All rights reserved.
00003 * Copyright (C) 2005 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 *
00011 *  - Redistributions in binary form must reproduce the above copyright notice,
00012 *    this list of conditions and the following disclaimer in the documentation
00013 *    and/or other materials provided with the distribution.
00014 *
00015 *  - Neither the name of Vintela, Inc., Novell, Inc., nor the names of its
00016 *    contributors may be used to endorse or promote products derived from this
00017 *    software without specific prior written permission.
00018 *
00019 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS ``AS IS''
00020 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022 * ARE DISCLAIMED. IN NO EVENT SHALL Vintela, Inc., Novell, Inc., OR THE 
00023 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 
00024 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 
00025 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 
00026 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
00027 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 
00028 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 
00029 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00030 *******************************************************************************/
00031 
00032 
00037 #ifndef BLOCXX_CMD_LINE_PARSER_HPP_INCLUDE_GUARD_
00038 #define BLOCXX_CMD_LINE_PARSER_HPP_INCLUDE_GUARD_
00039 #include "blocxx/BLOCXX_config.h"
00040 #include "blocxx/String.hpp"
00041 #include "blocxx/SortedVectorMap.hpp"
00042 #include "blocxx/Array.hpp"
00043 #include "blocxx/Exception.hpp"
00044 
00045 namespace BLOCXX_NAMESPACE
00046 {
00047 
00048 BLOCXX_DECLARE_APIEXCEPTION(CmdLineParser, BLOCXX_COMMON_API)
00049 
00050 
00057 class BLOCXX_COMMON_API CmdLineParser
00058 {
00059 public:
00060 
00065    enum EArgumentTypeFlag
00066    {
00068       E_NO_ARG,
00070       E_REQUIRED_ARG,
00072       E_OPTIONAL_ARG
00073    };
00074 
00078    enum EErrorCodes
00079    {
00081       E_INVALID_OPTION,
00086       E_MISSING_ARGUMENT,
00088       E_INVALID_NON_OPTION_ARG,
00090       E_MISSING_OPTION          // the option wasn't specified
00091    };
00092 
00096    struct Option
00097    {
00099       int id;
00101       char shortopt;
00103       const char* longopt;
00105       EArgumentTypeFlag argtype;
00110       const char* defaultValue;
00112       const char* description;
00113    };
00114 
00119    enum EAllowNonOptionArgsFlag
00120    {
00122       E_NON_OPTION_ARGS_ALLOWED,
00124       E_NON_OPTION_ARGS_INVALID
00125    };
00126 
00136    CmdLineParser(int argc, char const* const* const argv, const Option* options, EAllowNonOptionArgsFlag allowNonOptionArgs);
00137 
00145    String getOptionValue(int id, const char* defaultValue = "") const;
00146    
00157    String mustGetOptionValue(int id, const char* exceptionMessage = "") const;
00158    
00164    StringArray getOptionValueList(int id) const;
00165    
00174    StringArray mustGetOptionValueList(int id, const char* exceptionMessage = "") const;
00175    
00184    bool isSet(int id) const;
00185    
00189    size_t getNonOptionCount () const;
00190    
00196    String getNonOptionArg(size_t n) const;
00197 
00202    StringArray getNonOptionArgs() const;
00203    
00219    static String getUsage(const Option* options, unsigned int maxColumns = 80);
00220 
00221 
00222 private:
00223 
00224 #ifdef BLOCXX_WIN32
00225 #pragma warning (push)
00226 #pragma warning (disable: 4251)
00227 #endif
00228 
00229    // key is Option::id, value is the value(s) specified by the user
00230    typedef SortedVectorMap<int, StringArray> optionsMap_t;
00231    optionsMap_t m_parsedOptions;
00232    StringArray m_nonOptionArgs;
00233 
00234 #ifdef BLOCXX_WIN32
00235 #pragma warning (pop)
00236 #endif
00237 
00238 };
00239 
00240 } // end namespace BLOCXX_NAMESPACE
00241 
00242 #endif
00243 
00244 

Generated on Fri Jun 16 15:39:08 2006 for blocxx by  doxygen 1.4.6