add minimal documentation for DirectoryNode::link()
[senf.git] / Scheduler / Console / ProgramOptions.cc
index 6eb3107..0efbd7e 100644 (file)
@@ -29,6 +29,7 @@
 // Custom includes
 #include <boost/algorithm/string/predicate.hpp>
 #include <boost/format.hpp>
+#include "../../Utils/range.hh"
 
 //#include "ProgramOptions.mpp"
 #define prefix_
@@ -49,7 +50,7 @@ prefix_ void senf::console::detail::ProgramOptionsSource::v_parse(RestrictedExec
         nonOptions_->clear();
     if (argc_ <= 1)
         return;
-    char ** argp (argv_+1);
+    char const ** argp (argv_+1);
     int n (argc_-1);
     for (; n; --n, ++argp) {
         std::string arg (*argp);
@@ -59,7 +60,13 @@ prefix_ void senf::console::detail::ProgramOptionsSource::v_parse(RestrictedExec
             break;
         }
         else if (boost::algorithm::starts_with(arg, std::string("--")) && arg.size() > 2)
-            parseLongOption(arg.substr(2), executor);
+            try {
+                parseLongOption(arg.substr(2), executor);
+            }
+            catch (senf::ExceptionMixin & e) {
+                e << "\nwhile parsing command line option: " << arg;
+                throw;
+            }
         else if (boost::algorithm::starts_with(arg, std::string("-")) && arg.size() > 1) {
             for (std::string::size_type i (1); i<arg.size(); ++i) {
                 char opt (arg[i]);
@@ -86,7 +93,13 @@ prefix_ void senf::console::detail::ProgramOptionsSource::v_parse(RestrictedExec
                 }
                 if (boost::algorithm::starts_with(longOpt, std::string("--")))
                     longOpt = longOpt.substr(2);
-                parseLongOption(longOpt, executor);
+                try {
+                    parseLongOption(longOpt, executor);
+                }
+                catch (senf::ExceptionMixin & e) {
+                    e << "\nwhile parsing command line option: -" << opt << ' ' << param;
+                    throw;
+                }
             }
         }
         else
@@ -106,7 +119,7 @@ senf::console::detail::ProgramOptionsSource::parseLongOption(std::string const &
 
     ParseCommandInfo cmd;
     Path path;
-    
     DirectoryNode::ptr cwd (executor.root().thisptr());
     std::string::size_type b (0);
     while (b < name.size()) {
@@ -115,10 +128,12 @@ senf::console::detail::ProgramOptionsSource::parseLongOption(std::string const &
             std::string key (name.substr(b,e-b));
             if (! cwd->hasChild(key)) {
                 DirectoryNode::ChildrenRange completions (cwd->completions(key));
-                if (completions.size() == 1)
+                if (has_one_elt(completions))
                     key = completions.begin()->first;
-                else
+                else {
+                    e -= 1;
                     continue;
+                }
             }
             path.push_back(WordToken(key));
             if (e < name.size())
@@ -134,7 +149,7 @@ senf::console::detail::ProgramOptionsSource::parseLongOption(std::string const &
             b = name.size();
         }
     }
-    
+
     cmd.command(path);
     parser_.parseArguments(value, cmd);
     executor(std::cerr, cmd);
@@ -151,7 +166,7 @@ senf::console::detail::ProgramOptionsSource::parseNonOption(std::string const &
 
 ///////////////////////////////////////////////////////////////////////////
 
-prefix_ void senf::console::parseOptions(int argc, char ** argv, DirectoryNode & root)
+prefix_ void senf::console::parseOptions(int argc, char const ** argv, DirectoryNode & root)
 {
     ProgramOptions opts (argc, argv, root);
     opts.parse();