00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
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
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
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 }
00241
00242 #endif
00243
00244