4 // Fraunhofer Institute for Open Communication Systems (FOKUS)
5 // Competence Center NETwork research (NET), St. Augustin, GERMANY
6 // Stefan Bund <g0dil@berlios.de>
8 // This program is free software; you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation; either version 2 of the License, or
11 // (at your option) any later version.
13 // This program is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
18 // You should have received a copy of the GNU General Public License
19 // along with this program; if not, write to the
20 // Free Software Foundation, Inc.,
21 // 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 \brief ProgramOptions non-inline non-template implementation */
26 #include "ProgramOptions.hh"
27 #include "ProgramOptions.ih"
30 #include <boost/algorithm/string/predicate.hpp>
31 #include <boost/format.hpp>
33 //#include "ProgramOptions.mpp"
35 ///////////////////////////////cc.p////////////////////////////////////////
37 ///////////////////////////////////////////////////////////////////////////
38 // senf::console::detail::ProgramOptionsSource::NonOptionContainer
40 prefix_ senf::console::detail::ProgramOptionsSource::NonOptionContainer::~NonOptionContainer()
43 ///////////////////////////////////////////////////////////////////////////
44 // senf::console::detail::ProgramOptionsSource
46 prefix_ void senf::console::detail::ProgramOptionsSource::v_parse(RestrictedExecutor & executor)
52 char ** argp (argv_+1);
54 for (; n; --n, ++argp) {
55 std::string arg (*argp);
57 for (; n; --n, ++argp)
58 parseNonOption(arg, executor);
61 else if (boost::algorithm::starts_with(arg, std::string("--")) && arg.size() > 2)
62 parseLongOption(arg.substr(2), executor);
63 else if (boost::algorithm::starts_with(arg, std::string("-")) && arg.size() > 1) {
64 for (std::string::size_type i (1); i<arg.size(); ++i) {
66 ShortOptions::iterator j (shortOptions_.find(opt));
67 if (j == shortOptions_.end())
68 throw SyntaxErrorException(
69 (boost::format("invalid short option '%c'") % opt).str());
71 if (j->second.withArg) {
72 if (i >= arg.size()-1) {
79 param = arg.substr(i+1);
82 std::string longOpt (j->second.longOpt);
83 if (! param.empty() ) {
87 if (boost::algorithm::starts_with(longOpt, std::string("--")))
88 longOpt = longOpt.substr(2);
89 parseLongOption(longOpt, executor);
93 parseNonOption(arg, executor);
98 senf::console::detail::ProgramOptionsSource::parseLongOption(std::string const & arg,
99 RestrictedExecutor & executor)
101 std::string::size_type ix (arg.find('='));
102 std::string name (arg.substr(0,ix));
103 std::string value (ix==std::string::npos ? std::string() : arg.substr(ix+1));
105 typedef std::vector<Token> Path;
107 ParseCommandInfo cmd;
110 DirectoryNode::ptr cwd (executor.root().thisptr());
111 std::string::size_type b (0);
112 while (b < name.size()) {
113 std::string::size_type e (name.size());
114 for (;e != std::string::npos && e > b; e = name.rfind('-', e)) {
115 std::string key (name.substr(b,e-b));
116 if (! cwd->hasChild(key)) {
117 DirectoryNode::ChildrenRange completions (cwd->completions(key));
118 if (completions.size() == 1)
119 key = completions.begin()->first;
123 path.push_back(WordToken(key));
125 cwd = cwd->getDirectory(key).thisptr();
130 if (e == std::string::npos || e <= b) {
131 // This will produce a correct error message later or will skip the node,
132 // if parsing is restricted to a subtree
133 path.push_back(WordToken(name.substr(b)));
139 parser_.parseArguments(value, cmd);
140 executor(std::cerr, cmd);
144 senf::console::detail::ProgramOptionsSource::parseNonOption(std::string const & arg,
145 RestrictedExecutor & executor)
148 throw SyntaxErrorException("invalid non-option argument");
149 nonOptions_->push_back(arg);
152 ///////////////////////////////////////////////////////////////////////////
154 prefix_ void senf::console::parseOptions(int argc, char ** argv, DirectoryNode & root)
156 ProgramOptions opts (argc, argv, root);
160 ///////////////////////////////cc.e////////////////////////////////////////
162 //#include "ProgramOptions.mpp"
168 // comment-column: 40
169 // c-file-style: "senf"
170 // indent-tabs-mode: nil
171 // ispell-local-dictionary: "american"
172 // compile-command: "scons -u test"