27 #include <ndn-cxx/util/logger.hpp> 44 return os <<
"boolean";
46 return os <<
"non-negative integer";
48 return os <<
"string";
50 return os <<
"ReportFormat";
54 return os <<
"FaceUri";
56 return os <<
"FaceId or FaceUri";
58 return os <<
"FacePersistency";
60 return os <<
"RouteOrigin";
62 return os << static_cast<int>(vt);
107 const std::string& metavar)
109 bool isNew = m_args.emplace(name,
110 Arg{name, valueType,
static_cast<bool>(isRequired),
114 if (static_cast<bool>(isRequired)) {
115 m_requiredArgs.insert(name);
118 if (static_cast<bool>(allowPositional)) {
120 m_positionalArgs.push_back(name);
134 size_t positionalArgIndex = 0;
135 for (
size_t i = start; i < tokens.size(); ++i) {
136 const std::string& token = tokens[i];
139 auto namedArg = m_args.find(token);
140 if (namedArg != m_args.end() && namedArg->second.valueType !=
ArgValueType::ANY) {
141 NDN_LOG_TRACE(token <<
" is a named argument");
142 const Arg& arg = namedArg->second;
145 NDN_LOG_TRACE(token <<
" is a no-param argument");
147 else if (i + 1 >= tokens.size()) {
148 BOOST_THROW_EXCEPTION(
Error(arg.name +
": " + arg.metavar +
" is missing"));
151 const std::string& valueToken = tokens[++i];
152 NDN_LOG_TRACE(arg.name <<
" has value " << valueToken);
154 ca[arg.name] = this->parseValue(arg.valueType, valueToken);
156 catch (
const std::exception& e) {
157 NDN_LOG_TRACE(valueToken <<
" cannot be parsed as " << arg.valueType);
158 BOOST_THROW_EXCEPTION(
Error(arg.name +
": cannot parse '" + valueToken +
"' as " +
159 arg.metavar +
" (" + e.what() +
")"));
161 NDN_LOG_TRACE(valueToken <<
" is parsed as " << arg.valueType);
165 positionalArgIndex = m_positionalArgs.size();
170 for (; positionalArgIndex < m_positionalArgs.size(); ++positionalArgIndex) {
171 const Arg& arg = m_args.at(m_positionalArgs[positionalArgIndex]);
174 std::vector<std::string> values;
175 std::copy(tokens.begin() + i, tokens.end(), std::back_inserter(values));
176 ca[arg.name] = values;
177 NDN_LOG_TRACE((tokens.size() - i) <<
" tokens are consumed for " << arg.name);
183 ca[arg.name] = this->parseValue(arg.valueType, token);
184 NDN_LOG_TRACE(token <<
" is parsed as value for " << arg.name);
187 catch (
const std::exception& e) {
188 if (arg.isRequired) {
189 NDN_LOG_TRACE(token <<
" cannot be parsed as value for " << arg.name);
190 BOOST_THROW_EXCEPTION(
Error(
"cannot parse '" + token +
"' as an argument name or as " +
191 arg.metavar +
" for " + arg.name +
" (" + e.what() +
")"));
195 NDN_LOG_TRACE(token <<
" cannot be parsed as value for " << arg.name);
200 if (positionalArgIndex >= m_positionalArgs.size()) {
203 BOOST_THROW_EXCEPTION(
Error(
"cannot parse '" + token +
"' as an argument name"));
207 ++positionalArgIndex;
210 for (
const std::string& argName : m_requiredArgs) {
211 if (ca.count(argName) == 0) {
212 BOOST_THROW_EXCEPTION(
Error(argName +
": required argument is missing"));
222 if (s ==
"on" || s ==
"true" || s ==
"enabled" || s ==
"yes" || s ==
"1") {
225 if (s ==
"off" || s ==
"false" || s ==
"disabled" || s ==
"no" || s ==
"0") {
228 BOOST_THROW_EXCEPTION(std::invalid_argument(
"unrecognized boolean value '" + s +
"'"));
231 static FacePersistency
234 if (s ==
"persistent") {
235 return FacePersistency::FACE_PERSISTENCY_PERSISTENT;
237 if (s ==
"permanent") {
238 return FacePersistency::FACE_PERSISTENCY_PERMANENT;
240 BOOST_THROW_EXCEPTION(std::invalid_argument(
"unrecognized FacePersistency '" + s +
"'"));
244 CommandDefinition::parseValue(
ArgValueType valueType,
const std::string& token)
const 258 int64_t v = boost::lexical_cast<int64_t>(token);
260 BOOST_THROW_EXCEPTION(std::out_of_range(
"value '" + token +
"' is negative"));
262 return static_cast<uint64_t
>(v);
275 return FaceUri(token);
279 return boost::lexical_cast<uint64_t>(token);
281 catch (
const boost::bad_lexical_cast&) {
282 return FaceUri(token);
289 return boost::lexical_cast<RouteOrigin>(token);
Copyright (c) 2014-2015, Regents of the University of California, Arizona Board of Regents...