[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp...
From: |
Jean-Philippe Aumasson |
Subject: |
[oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp... |
Date: |
Fri, 03 Jun 2005 06:37:32 -0400 |
CVSROOT: /cvsroot/ometah
Module name: ometah
Branch:
Changes by: Jean-Philippe Aumasson <address@hidden> 05/06/03 10:37:32
Modified files:
interface : itsArgument.cpp itsArgument.hpp ometah.cpp
ometah.hpp
Log message:
* comments added //! ...
* usage is now a method of ArgumentParser
* syntax checking : values missing / unneeded, unknown flags, etc...
CVSWeb URLs:
http://savannah.gnu.org/cgi-bin/viewcvs/ometah/ometah/interface/itsArgument.cpp.diff?tr1=1.9&tr2=1.10&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/ometah/ometah/interface/itsArgument.hpp.diff?tr1=1.8&tr2=1.9&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/ometah/ometah/interface/ometah.cpp.diff?tr1=1.27&tr2=1.28&r1=text&r2=text
http://savannah.gnu.org/cgi-bin/viewcvs/ometah/ometah/interface/ometah.hpp.diff?tr1=1.6&tr2=1.7&r1=text&r2=text
Patches:
Index: ometah/interface/itsArgument.cpp
diff -u ometah/interface/itsArgument.cpp:1.9
ometah/interface/itsArgument.cpp:1.10
--- ometah/interface/itsArgument.cpp:1.9 Fri Jun 3 07:57:23 2005
+++ ometah/interface/itsArgument.cpp Fri Jun 3 10:37:32 2005
@@ -1,5 +1,5 @@
/***************************************************************************
- * $Id: itsArgument.cpp,v 1.9 2005/06/03 07:57:23 jpa Exp $
+ * $Id: itsArgument.cpp,v 1.10 2005/06/03 10:37:32 jpa Exp $
* Copyright : Université Paris 12 Val-de-Marne
* Author : Johann Dréo <address@hidden>
* Author : Jean-Philippe Aumasson <address@hidden>
@@ -32,12 +32,11 @@
// ITS ARGUMENT
-// destructor
itsArgument::~itsArgument(){
}
-// constructor...
+
itsArgument::itsArgument(string flagShort, string flagLong, string usage,
bool hasValue, string type, string value)
{
@@ -50,19 +49,19 @@
this->value = value;
}
-// return default key (ie -v )
+
string itsArgument::getKey()
{
return this->flagShort;
}
-// return long key (ie --version)
+
string itsArgument::getLongKey()
{
return this->flagLong;
}
-// return the argument as a string
+
string itsArgument::getValue()
{
if (this->hasValue)
@@ -72,29 +71,32 @@
string itsArgument::getUsage()
{
-
return this->usage;
}
+bool itsArgument::getHasValue()
+{
+ return this->hasValue;
+}
+
// ITSPARSER
-// destructor
+
itsArgumentParser::~itsArgumentParser()
{
}
-// constructor, with argv as the command line values
+
itsArgumentParser::itsArgumentParser(int argc, vector<string> argv)
{
this->argv = argv;
this->argc = argc;
}
-void itsArgumentParser::searchEndFlags() {
- // termination flags : if there, don't execute program
- // following order <=> flags' priorities
+void itsArgumentParser::searchEndFlags()
+{
if (this->defArg("-V","--version","check version", false, "", "")){
throw VERSION;
}
@@ -119,11 +121,12 @@
if (hasValue){
- if ((++i) < this->argv.size())
+ // if not end of line AND next word isnt a flag
+ if ( (++i) < this->argv.size() && (this->argv.at(i)[0] != '-' ) )
value = this->argv.at(i);
else { // ONLY CASE OF LAST PARAM !!
stringstream s;
- s << "parameter value missing for " << flagShort << endl;
+ s << "Value missing for " << flagShort << endl;
throw (s.str()).c_str();
}
}
@@ -199,11 +202,12 @@
return i;
}
-bool itsArgumentParser::getBoolValue(string key){
+bool itsArgumentParser::getBoolValue(string key)
+{
vector<itsArgument>::iterator iter;
iter = arguments.begin();
- while (iter != arguments.end()){
+ while ( iter != arguments.end() ) {
if (iter->getKey() == key)
return (iter->getValue() != "");
iter++;
@@ -214,14 +218,98 @@
return false;
}
-// return the vector of arguments, so as to be printed in usage()
-vector<itsArgument> itsArgumentParser::getArguments(){
- return this->arguments;
+void itsArgumentParser::usage()
+{
+
+ cout << "Usage: " << this->argv.at(0) << "[options]" << endl; // TODO
+ vector<itsArgument>::iterator iter;
+ iter = this->arguments.begin();
+ while (iter != this->arguments.end()){
+ cout << "\t" << iter->getKey()
+ << ", " << iter->getLongKey()
+ << "\t\t" << iter->getUsage()
+ << endl;
+ iter++;
+ }
+}
+
+
+// check if all parameters ( "-? | --* like ) have been checked
+bool itsArgumentParser::syntaxCorrect(){
+
+ // check all -? | --* are in arguments
+ // -> search for each one in vector arguments
+
+ // ( value missing verification -> DONE IN DEFARGS )
+ // -> nothing to do
+
+ // Error if word followin flag with no value != -? | --*
+ // -> err if !iter->hasValue() and arv[i+1] [0] != '-' (and i+1 < argc)
+
+ // How ?
+ // throw "explicit error" for each type error, including flag name, etc...
+ // return false
+
+ stringstream s;
+ vector<string>::iterator iter;
+ iter = this->argv.begin();
+
+ iter ++; // reach first word after "./ometah"
+
+ // check if all flags are known
+ while (iter != this->argv.end()){
+ if ((*iter)[0] == '-'){ // if is a flag (known or not)
+
+ if (!isAnArgument(*iter)) { // if is not a known flag
+ s << "Unknown argument: " << *iter << endl;
+ throw (s.str()).c_str();
+ return false;
+ }
+ else { // is a correct flag (defArg-ed)
+ if (!this->hasValue(*iter)){
+ iter++;
+ if ((iter != this->argv.end()) && ((*iter)[0] != '-')){
+ s << "Value not needed for " << *iter << endl;
+ throw (s.str()).c_str();
+ }
+ }
+ }
+ }
+
+ iter++;
+ }
+ return true;
+}
+
+
+// returns true if s is known as a long or short flag
+bool itsArgumentParser::isAnArgument(string s)
+{
+ if (s[0] != '-') // if doesn't start with '-', cannot be a flag
+ return false;
+ vector<itsArgument>::iterator iter;
+ iter = this->arguments.begin();
+ while (iter != this->arguments.end()){
+ if ((s == iter->getKey()) || (s == iter->getLongKey()))
+ return true;
+ iter++;
+ }
+ return false;
}
-// return the name of argv's first word ( ~ ometah )
-string itsArgumentParser::getCommand(){
- return this->argv.at(0);
+bool itsArgumentParser::hasValue(string flag)
+{
+ vector<itsArgument>::iterator iter;
+ iter = arguments.begin();
+ while (iter != arguments.end()){
+ if ((flag == iter->getKey()) || (flag == iter->getLongKey())){
+ return iter->getHasValue();
+ }
+ }
+ stringstream s;
+ s << "Unexpected error in hasValue()" << endl;
+ throw (s.str()).c_str();
+ return false;
}
Index: ometah/interface/itsArgument.hpp
diff -u ometah/interface/itsArgument.hpp:1.8
ometah/interface/itsArgument.hpp:1.9
--- ometah/interface/itsArgument.hpp:1.8 Fri Jun 3 07:57:23 2005
+++ ometah/interface/itsArgument.hpp Fri Jun 3 10:37:32 2005
@@ -1,5 +1,5 @@
/***************************************************************************
- * $Id: itsArgument.hpp,v 1.8 2005/06/03 07:57:23 jpa Exp $
+ * $Id: itsArgument.hpp,v 1.9 2005/06/03 10:37:32 jpa Exp $
* Copyright : Université Paris 12 Val-de-Marne
* Author : Johann Dréo <address@hidden>
* Author : Jean-Philippe Aumasson <address@hidden>
@@ -30,13 +30,17 @@
#include "../common/logic.hpp"
-#define DEBUG 1
+// #define DEBUG 1
#define VERSION "OpenMetaheuristics version pre-alpha -10"
#define USAGE "usage" // string value doesn't matter
using namespace std;
+/*!
+ An argument is a command line specification, like an option,
+ including the flag and its value, it needed.
+*/
class itsArgument
{
protected:
@@ -52,10 +56,10 @@
//! Usage instructions
string usage;
- //! Type of the associated value, if there
+ //! Type of the associated value, boolean if has no value
string type;
- // default value
+ //! Value of the argument, empty if argument has no value
string value;
@@ -68,28 +72,43 @@
itsArgument(string flagShort, string flagLong, string usage,
bool hasValue=false, string type="", string valueDefault="");
+ //! Returns default key (short)
string getKey();
+ //! Returns alternative key (long)
string getLongKey();
+ //! Returns the value
string getValue();
+ //! Returns usage instructions
string getUsage();
+ bool getHasValue();
+
};
+/*!
+ An argument parser works on the command line arguments and analyze them,
+ look for syntax errors, get values from flags, etc..
+ */
class itsArgumentParser
{
protected:
+ //! Main's argc, number of command line words
int argc;
+ //! Main's argv, vector containing the command line words
vector<string> argv;
- // flag (short or long) -> associated argument
+ //! A vector of itsArguments objects, corresponding to the arguments read on
argv
vector<itsArgument> arguments;
+ //! From a supposed flag, short or long, returns true if is a known argument
flag
+ bool isAnArgument(string flag);
+
public:
//! Destructor
@@ -97,21 +116,32 @@
//! Constructor
itsArgumentParser(int, vector<string>);
-
+
+ //! Looks for end flags, that is flags like -V for version checking, which
terminate the execution
void searchEndFlags();
+ //! Define an option of the program, affecting default value if not
specified in command line
bool defArg(string flagShort, string flagLong, string usage,
bool hasValue=false, string type="", string valueDefault="");
-
+
+ //! Accessor for the string value of the given flag (short)
string getStringValue(string flag);
+ //! Accessor for the double value of the given flag (short)
double getDoubleValue(string flag);
+ //! Accessor for the int value of the given flag (short)
int getIntValue(string flag);
+ //! Accessor for the boolean value of the given flag (short)
bool getBoolValue(string flag);
- vector<itsArgument> getArguments();
+ //! Prints usage instructions, from defArgs definitions
+ void usage();
+
+ //! Checks for syntax correctness, throws exception when an error occurs
+ bool syntaxCorrect();
- string getCommand();
+ //! Returns true if the matching argument has a value
+ bool hasValue(string flag);
};
Index: ometah/interface/ometah.cpp
diff -u ometah/interface/ometah.cpp:1.27 ometah/interface/ometah.cpp:1.28
--- ometah/interface/ometah.cpp:1.27 Fri Jun 3 07:57:23 2005
+++ ometah/interface/ometah.cpp Fri Jun 3 10:37:32 2005
@@ -1,5 +1,5 @@
/***************************************************************************
- * $Id: ometah.cpp,v 1.27 2005/06/03 07:57:23 jpa Exp $
+ * $Id: ometah.cpp,v 1.28 2005/06/03 10:37:32 jpa Exp $
* Copyright : Université Paris 12 Val-de-Marne
* Author : Johann Dréo <address@hidden>
* Author : Jean-Philippe Aumasson <address@hidden>
@@ -29,24 +29,13 @@
using namespace std;
-void usage(string cmd, vector<itsArgument> arguments){
-
- cout << "Usage: " << cmd << "[options]" << endl; // TODO
- vector<itsArgument>::iterator iter;
- iter = arguments.begin();
- while (iter != arguments.end()){
- cout << "\t" << iter->getKey()
- << ", " << iter->getLongKey()
- << "\t\t" << iter->getUsage()
- << endl;
- iter++;
- }
-
-}
-
int main(int argc, char ** argv)
{
+#ifdef DEBUG
+ cout << "Enter debug mode" << endl;
+#endif
+
// differents sets of objects
itsSet<itsMetaheuristic*> setMetaheuristic;
itsSet<itsProblem*> setProblem;
@@ -107,24 +96,24 @@
itsArgumentParser argumentParser(argc, argumentsVector);
try {
- // arguments definitions
- argumentParser.defArg("-p", "--problem",
- "problem's name", true, "string", "Rosenbrock");
- argumentParser.defArg("-m", "--metah",
- "metaheuristic's name", true, "string", "CEDA");
- argumentParser.defArg("-C", "--com-client",
- "communication client mode" ,true, "string", "Embedded");
- argumentParser.defArg("-S", "--com-server",
- "communication server mode" ,true, "string", "Embedded");
- argumentParser.defArg("-D", "--debug",
- "debug key" ,true, "string", "");
-
- argumentParser.defArg("-i", "--iterations",
- "maximum number of iterations" ,true, "int", "10");
- argumentParser.defArg("-s", "--sample-size",
- "number of points in the sample" ,true, "int", "10");
- argumentParser.defArg("-d", "--dimension",
- "dimension of the problem" ,true, "int", "1");
+ // arguments definitions (and not elsewhere !!)
+ argumentParser.defArg("-p", "--problem",
+ "problem's name", true, "string", "Rosenbrock");
+ argumentParser.defArg("-m", "--metah",
+ "metaheuristic's name", true, "string", "CEDA");
+ argumentParser.defArg("-C", "--com-client",
+ "communication client mode" ,true, "string",
"Embedded");
+ argumentParser.defArg("-S", "--com-server",
+ "communication server mode" ,true, "string",
"Embedded");
+ argumentParser.defArg("-D", "--debug",
+ "debug key" ,true, "string", "");
+
+ argumentParser.defArg("-i", "--iterations",
+ "maximum number of iterations" ,true, "int", "10");
+ argumentParser.defArg("-s", "--sample-size",
+ "number of points in the sample" ,true, "int", "10");
+ argumentParser.defArg("-d", "--dimension",
+ "dimension of the problem" ,true, "int", "1");
}
catch(const char * s) {
cerr << s;
@@ -132,21 +121,34 @@
}
- // look for end flags (-v, -h, etc...)
- try{
+ // look for end flags (-V, -h, etc...)
+ try {
argumentParser.searchEndFlags();
}
- catch (const char * s){
- if (!strcmp(VERSION, s)){
+ catch (const char * s) {
+ if ( !strcmp(VERSION, s) ) {
cerr << s << endl;
return -1;
}
- else if (!strcmp(USAGE, s)){
- usage(argumentParser.getCommand(), argumentParser.getArguments());
+ else if (!strcmp(USAGE, s)) {
+ argumentParser.usage();
return -1;
}
}
+
+ try {
+ if (argumentParser.syntaxCorrect()){
+#ifdef DEBUG
+ cout << "syntax ok" << endl;
+#endif
+ }
+ }
+ catch (const char * s) {
+ cerr << s;
+ return -1;
+ }
+
#ifdef DEBUG
cout << "\ngetValues :\n-----" << "\n problem: " <<
argumentParser.getStringValue("-p")
@@ -242,9 +244,9 @@
// Starting the optimization
- clog << "Launching " << setMetaheuristic.item()->getName()
- << " on " << setProblem.item()->getName()
- << " using " << setCommunicationClient.item()->getKey() << endl;
+ clog << "Launching " << setMetaheuristic.item()->getName()
+ << " on " << setProblem.item()->getName()
+ << " using " << setCommunicationClient.item()->getKey() << endl;
//clog << setProblem.item()->getName() << " description:" << endl;
@@ -263,7 +265,5 @@
catch (...) {
cerr << "Unknown error" << endl;
}
-
-
}
Index: ometah/interface/ometah.hpp
diff -u ometah/interface/ometah.hpp:1.6 ometah/interface/ometah.hpp:1.7
--- ometah/interface/ometah.hpp:1.6 Fri Jun 3 07:57:23 2005
+++ ometah/interface/ometah.hpp Fri Jun 3 10:37:32 2005
@@ -1,5 +1,5 @@
/***************************************************************************
- * $Id: ometah.hpp,v 1.6 2005/06/03 07:57:23 jpa Exp $
+ * $Id: ometah.hpp,v 1.7 2005/06/03 10:37:32 jpa Exp $
* Copyright : Université Paris 12 Val-de-Marne
* Author : Johann Dréo <address@hidden>
* Author : Jean-Philippe Aumasson <address@hidden>
@@ -51,5 +51,3 @@
using namespace std;
-
-void usage(string, vector<itsArgument>);
- [oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp..., Jean-Philippe Aumasson, 2005/06/02
- [oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp..., Jean-Philippe Aumasson, 2005/06/02
- [oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp..., Jean-Philippe Aumasson, 2005/06/02
- [oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp..., Jean-Philippe Aumasson, 2005/06/03
- [oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp...,
Jean-Philippe Aumasson <=
- [oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp..., Jean-Philippe Aumasson, 2005/06/03
- [oMetah-devel] ometah/interface itsArgument.cpp itsArgument.hp..., Jean-Philippe Aumasson, 2005/06/03