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
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
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
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 }
00207
00208 #endif
00209
00210