Go to the documentation of this file.00001
00002
00003
00004
00005
00008
00009 #ifndef lgrAppCmdLine_H_
00010 #define lgrAppCmdLine_H_
00011
00012 #include <cmdLine/cmdLine.h>
00013 #include "lgrConsole.h"
00014
00015 namespace MAKLogger
00016 {
00019 class DT_DLL_LGRAPP DtLgrCmdLine : public DtCmdLine::CmdLine
00020 {
00021 public:
00022 explicit DtLgrCmdLine(const std::string& message,
00023 const char delimiter = ' ',
00024 const std::string& version = "none");
00025
00030 void addUnlabeled( DtCmdLine::Arg* a );
00031
00035 inline bool parseInput(int argc, char* const * argv);
00036
00037 protected:
00038
00040 DtCmdLine::Arg* myUnlabeledArgument;
00041 };
00042
00043 DtLgrCmdLine::DtLgrCmdLine(const std::string& message,
00044 const char delimiter, const std::string& version) :
00045 CmdLine(message,delimiter,version),
00046 myUnlabeledArgument(0)
00047 {
00048 }
00049
00050 inline void DtLgrCmdLine::addUnlabeled( DtCmdLine::Arg* a )
00051 {
00052 myUnlabeledArgument = a;
00053 add(a);
00054 }
00055
00056 inline bool DtLgrCmdLine::parseInput(int argc, char* const * argv)
00057 {
00058 try
00059 {
00060 std::vector<std::string> args;
00061 int requiredCount = 0;
00062 int i=1;
00063
00064 _progName = argv[0];
00065
00067 for (i = 1; i < argc; i++)
00068 args.push_back(argv[i]);
00069
00070 for (i = 0; static_cast<unsigned>(i) < args.size(); i++)
00071 {
00072 bool matched = false;
00073 for (DtCmdLine::ArgListIterator it = _argList.begin(); !matched && it != _argList.end(); it++)
00074 {
00075 if ( (*it)->processArg( &i, args ) )
00076 {
00077 if ((*it) == myUnlabeledArgument
00078 && args[i].size() > 0 && args[i][0] == myUnlabeledArgument->flagStartChar())
00079 {
00080 throw(DtCmdLine::CmdLineParseException("Is not recognized!",
00081 args[i]));
00082 }
00083 requiredCount += _xorHandler.check( *it );
00084 matched = true;
00085 }
00086 }
00087
00090 if ( !matched && _emptyCombined( args[i] ) )
00091 matched = true;
00092
00093 if ( !matched && !DtCmdLine::Arg::ignoreRest() )
00094 throw(DtCmdLine::CmdLineParseException("Couldn't find match for argument",
00095 args[i]));
00096 }
00097
00098 if ( requiredCount < _numRequired )
00099 throw(DtCmdLine::CmdLineParseException("One or more required arguments missing!"));
00100
00101 if ( requiredCount > _numRequired )
00102 throw(DtCmdLine::CmdLineParseException("Too many arguments!"));
00103
00104 return true;
00105
00106 }
00107 catch ( DtCmdLine::ArgException& e )
00108 {
00109 DtLgrConsole::allocateConsole();
00110 _output->failure(*this,e);
00111
00112 int oldNotifyLevel = DtNotifyLevel;
00113 DtNotifyLevel = 3;
00114
00115 if (myPressAnyKey)
00116 {
00117 DtInfo << "Press any key to continue" << std::endl;
00118
00119 while (DtPollBlockingInputChar() == '\0')
00120 {
00121 DtSleep(0.1);
00122 }
00123 }
00124
00125 DtNotifyLevel = oldNotifyLevel;
00126 return false;
00127 }
00128 }
00129 }
00130
00131 #endif