]> AND Private Git Repository - loba.git/commitdiff
Logo AND Algorithmique Numérique Distribuée

Private GIT Repository
Named options may now be selected by prefix if there is no ambiguity.
authorArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 4 Jan 2011 14:48:23 +0000 (15:48 +0100)
committerArnaud Giersch <arnaud.giersch@iut-bm.univ-fcomte.fr>
Tue, 4 Jan 2011 14:48:23 +0000 (15:48 +0100)
options.cpp

index 142056600f93c83bfe2ec94e1c16cefe496191f0..2cf607c841e729c5dec3b9f0fd0714a71ab7d498 100644 (file)
@@ -1,6 +1,7 @@
 #include <iomanip>
 #include <iostream>
 #include <sstream>
 #include <iomanip>
 #include <iostream>
 #include <sstream>
+#include <stack>
 #include <unistd.h>             // getopt
 #include <xbt/log.h>
 
 #include <unistd.h>             // getopt
 #include <xbt/log.h>
 
@@ -75,6 +76,43 @@ namespace opt {
 
 } // namespace opt
 
 
 } // namespace opt
 
+namespace {
+
+    template <typename T>
+    bool nol_find_prefix(const T& nol, const char* descr, std::string& name)
+    {
+        bool result = nol.exists(name);
+        if (!result) {
+            std::stack<std::string> candidates;
+            for (typename T::iterator it = nol.begin() ; it != nol.end() ; ++it) {
+                const std::string& fullname = nol.get_name(it);
+                if (fullname.compare(0, name.length(), name) == 0)
+                    candidates.push(fullname);
+            }
+            switch (candidates.size()) {
+            case 0:
+                ERROR2("unknownw %s -- %s", descr, name.c_str());
+                break;
+            case 1:
+                name = candidates.top();
+                candidates.pop();
+                result = true;
+                DEBUG2("infered %s -- %s", descr, name.c_str());
+                break;
+            default:
+                ERROR2("ambiguous %s -- %s", descr, name.c_str());
+                while (!candidates.empty()) {
+                    ERROR1("  candidates are -- %s", candidates.top().c_str());
+                    candidates.pop();
+                }
+                break;
+            }
+        }
+        return result;
+    }
+
+} // namespace
+
 bool opt::parse_args(int* argc, char* argv[])
 {
     bool result = true;
 bool opt::parse_args(int* argc, char* argv[])
 {
     bool result = true;
@@ -88,11 +126,9 @@ bool opt::parse_args(int* argc, char* argv[])
         switch (c) {
         case 'a':
             opt::loba_algo = optarg;
         switch (c) {
         case 'a':
             opt::loba_algo = optarg;
-            if (!opt::loba_algorithms.exists(opt::loba_algo)) {
-                ERROR1("unknownw load balancing algorithm -- %s",
-                       opt::loba_algo.c_str());
-                result = false;
-            }
+            result = nol_find_prefix(opt::loba_algorithms,
+                                     "load balancing algorithm",
+                                     opt::loba_algo);
             break;
         case 'b':
             opt::bookkeeping = true;
             break;
         case 'b':
             opt::bookkeeping = true;
@@ -122,12 +158,9 @@ bool opt::parse_args(int* argc, char* argv[])
             std::istringstream(optarg) >> opt::auto_depl::nhosts;
             break;
         case 'T':
             std::istringstream(optarg) >> opt::auto_depl::nhosts;
             break;
         case 'T':
-            opt::auto_depl::topology = optarg; 
-            if (!opt::topologies.exists(opt::auto_depl::topology)) {
-                ERROR1("unknownw topology -- %s",
-                       opt::auto_depl::topology.c_str());
-                result = false;
-            }
+            opt::auto_depl::topology = optarg;
+            result = nol_find_prefix(opt::topologies, "topology",
+                                     opt::auto_depl::topology);
             break;
         case 'v':
             // nothing to do: this option is checked at the very
             break;
         case 'v':
             // nothing to do: this option is checked at the very