Update of /cvsroot/pclasses/pclasses2/src/App
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv3925/src/App
Modified Files:
CmdLine.cpp
Log Message:
More work on CmdLine (early commit .. sorry)
Index: CmdLine.cpp
===================================================================
RCS file: /cvsroot/pclasses/pclasses2/src/App/CmdLine.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- CmdLine.cpp 19 Jan 2005 13:43:57 -0000 1.2
+++ CmdLine.cpp 20 Jan 2005 11:02:01 -0000 1.3
@@ -35,6 +35,21 @@
{
}
+const std::string& CmdLineOption::shortName() const throw()
+{
+ return _shortOpt;
+}
+
+const std::string& CmdLineOption::longName() const throw()
+{
+ return _longOpt;
+}
+
+bool CmdLineOption::required() const throw()
+{
+ return _required;
+}
+
CmdLineFlag::CmdLineFlag(const std::string& shortOpt,
const std::string& longOpt, bool defaultVal, bool required)
@@ -58,7 +73,7 @@
}
-CmdLineParser::CmdLineParser(CmdLineOption* opts)
+CmdLineParser::CmdLineParser(CmdLineOption* opts[])
: _opts(opts)
{
}
@@ -82,11 +97,74 @@
return parse(os.str());
}
+void parseOptName(std::istream& is, std::string& optName)
+{
+ // break on '=' and ' '
+}
+
+CmdLineOption* findOptShort(CmdLineOption* opts[], std::string& shortOpt)
+{
+ int i = 0;
+ while(opts[i])
+ {
+ if(opts[i]->shortName() == shortOpt)
+ return opts[i];
+
+ ++i;
+ }
+
+ return 0;
+}
+
+CmdLineOption* findOptLong(CmdLineOption* opts[], std::string& longOpt)
+{
+ int i = 0;
+ while(opts[i])
+ {
+ if(opts[i]->longName() == longOpt)
+ return opts[i];
+
+ ++i;
+ }
+
+ return 0;
+}
+
bool CmdLineParser::parse(const std::string& cmdline)
{
std::istringstream is(cmdline);
- //@fixme
+ char ch;
+ while((is >> ch))
+ {
+ if(ch == '-')
+ {
+ std::string optName;
+ CmdLineOption* opt = 0;
+
+ is >> ch;
+
+ if(ch != '-')
+ {
+ is.unget();
+ parseOptName(is, optName);
+ opt = findOptShort(_opts, optName);
+ }
+ else
+ {
+ parseOptName(is, optName);
+ opt = findOptLong(_opts, optName);
+ }
+
+ // unknown option?
+ if(!opt)
+ {
+ //@fixme throw CmdLineError()
+ return false;
+ }
+ }
+ }
+
return false;
}
|