[mlpack-svn] r13317 - mlpack/trunk/src/mlpack/core/util

fastlab-svn at coffeetalk-1.cc.gatech.edu fastlab-svn at coffeetalk-1.cc.gatech.edu
Thu Aug 2 12:47:57 EDT 2012


Author: trironk3
Date: 2012-08-02 12:47:57 -0400 (Thu, 02 Aug 2012)
New Revision: 13317

Modified:
   mlpack/trunk/src/mlpack/core/util/cli.cpp
   mlpack/trunk/src/mlpack/core/util/cli.hpp
Log:
I added in logic to remove duplicate flag parameters in response to Ticket #231.
http://trac.research.cc.gatech.edu/fastlab/ticket/231



Modified: mlpack/trunk/src/mlpack/core/util/cli.cpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/cli.cpp	2012-08-02 16:43:40 UTC (rev 13316)
+++ mlpack/trunk/src/mlpack/core/util/cli.cpp	2012-08-02 16:47:57 UTC (rev 13317)
@@ -378,10 +378,19 @@
   // Parse the command line, place the options & values into vmap
   try
   {
-    po::store(po::parse_command_line(argc, line, desc), vmap);
+    // Get the basic_parsed_options
+    po::basic_parsed_options<char> bpo(
+      po::parse_command_line(argc, line, desc));
+
+    // Look for any duplicate parameters, removing duplicate flags
+    RemoveDuplicateFlags(bpo);
+
+    // Record the basic_parsed_options
+    po::store(bpo, vmap);
   }
   catch (std::exception& ex)
   {
+    Log::Fatal << "Caught exception from parsing command line:\t";
     Log::Fatal << ex.what() << std::endl;
   }
 
@@ -392,6 +401,43 @@
   RequiredOptions();
 }
 
+/*
+ * Removes duplicate flags.
+ *
+ * @param bpo The basic_program_options to remove duplicate flags from. 
+ */
+void CLI::RemoveDuplicateFlags(po::basic_parsed_options<char>& bpo)
+{
+  // Iterate over all the program_options, looking for duplicate parameters
+  for (unsigned int i = 0; i < bpo.options.size(); i++)
+  {
+    for (unsigned int j = i + 1; j < bpo.options.size(); j++)
+    {
+      if (bpo.options[i].string_key == bpo.options[j].string_key)
+      {
+        // If a duplicate is found, check to see if either one has a value
+        if (bpo.options[i].value.size() == 0 &&
+            bpo.options[j].value.size() == 0)
+        {
+          // If neither has a value, consider it a duplicate flag and remove the
+          // duplicate. It's important to not break out of this loop because
+          // there might be another duplicate later on in the vector.
+          bpo.options.erase(bpo.options.begin()+j);
+        }
+        else
+        {
+          // If one or both has a value, produce an error and politely
+          // terminate. We pull the name from the original_tokens, rather than
+          // from the string_key, because the string_key is the parameter after
+          // aliases have been expanded.
+          Log::Fatal << "\"" << bpo.options[j].original_tokens[0] << "\""
+              << " is defined multiple times." << std::endl;
+        }
+      }
+    }
+  }
+}
+
 /**
  * Parses a stream for arguments
  *
@@ -469,7 +515,7 @@
     {
       // We don't know how to print this, or it's a timeval which is printed
       // later.
-      Log::Info << "(unknown data type)";
+      Log::Info << "(Unknown data type - " << data.tname << ")";
     }
 
     Log::Info << std::endl;

Modified: mlpack/trunk/src/mlpack/core/util/cli.hpp
===================================================================
--- mlpack/trunk/src/mlpack/core/util/cli.hpp	2012-08-02 16:43:40 UTC (rev 13316)
+++ mlpack/trunk/src/mlpack/core/util/cli.hpp	2012-08-02 16:47:57 UTC (rev 13317)
@@ -626,6 +626,13 @@
   static void ParseCommandLine(int argc, char** argv);
 
   /**
+   * Removes duplicate flags.
+   *
+   * @param bpo The basic_program_options to remove duplicate flags from. 
+   */
+  static void RemoveDuplicateFlags(po::basic_parsed_options<char>& bpo);
+
+ /**
    * Parses a stream for arguments.
    *
    * @param stream The stream to be parsed.




More information about the mlpack-svn mailing list