29 #include <ndn-cxx/net/face-uri.hpp>
30 #include <ndn-cxx/util/backports.hpp>
31 #include <ndn-cxx/util/logger.hpp>
33 #include <boost/lexical_cast.hpp>
48 return os <<
"boolean";
50 return os <<
"non-negative integer";
52 return os <<
"string";
54 return os <<
"ReportFormat";
58 return os <<
"FaceUri";
60 return os <<
"FaceId or FaceUri";
62 return os <<
"FacePersistency";
64 return os <<
"RouteOrigin";
66 return os << static_cast<int>(vt);
110 const std::string& metavar)
112 bool isNew = m_args.emplace(name,
113 Arg{name, valueType,
static_cast<bool>(isRequired),
117 if (
static_cast<bool>(isRequired)) {
118 m_requiredArgs.insert(name);
121 if (
static_cast<bool>(allowPositional)) {
123 m_positionalArgs.push_back(name);
137 size_t positionalArgIndex = 0;
138 for (
size_t i = start; i < tokens.size(); ++i) {
139 const std::string& token = tokens[i];
142 auto namedArg = m_args.find(token);
143 if (namedArg != m_args.end() && namedArg->second.valueType !=
ArgValueType::ANY) {
144 NDN_LOG_TRACE(token <<
" is a named argument");
145 const Arg& arg = namedArg->second;
148 NDN_LOG_TRACE(token <<
" is a no-param argument");
150 else if (i + 1 >= tokens.size()) {
151 NDN_THROW(
Error(arg.name +
": " + arg.metavar +
" is missing"));
154 const std::string& valueToken = tokens[++i];
155 NDN_LOG_TRACE(arg.name <<
" has value " << valueToken);
157 ca[arg.name] = parseValue(arg.valueType, valueToken);
159 catch (
const std::exception& e) {
160 NDN_LOG_TRACE(valueToken <<
" cannot be parsed as " << arg.valueType);
161 NDN_THROW_NESTED(
Error(arg.name +
": cannot parse '" + valueToken +
"' as " +
162 arg.metavar +
" (" + e.what() +
")"));
164 NDN_LOG_TRACE(valueToken <<
" is parsed as " << arg.valueType);
168 positionalArgIndex = m_positionalArgs.size();
173 for (; positionalArgIndex < m_positionalArgs.size(); ++positionalArgIndex) {
174 const Arg& arg = m_args.at(m_positionalArgs[positionalArgIndex]);
177 std::vector<std::string> values;
178 std::copy(tokens.begin() + i, tokens.end(), std::back_inserter(values));
179 ca[arg.name] = values;
180 NDN_LOG_TRACE((tokens.size() - i) <<
" tokens are consumed for " << arg.name);
186 ca[arg.name] = parseValue(arg.valueType, token);
187 NDN_LOG_TRACE(token <<
" is parsed as value for " << arg.name);
190 catch (
const std::exception& e) {
191 if (arg.isRequired) {
192 NDN_LOG_TRACE(token <<
" cannot be parsed as value for " << arg.name);
193 NDN_THROW_NESTED(
Error(
"cannot parse '" + token +
"' as an argument name or as " +
194 arg.metavar +
" for " + arg.name +
" (" + e.what() +
")"));
198 NDN_LOG_TRACE(token <<
" cannot be parsed as value for " << arg.name);
203 if (positionalArgIndex >= m_positionalArgs.size()) {
206 NDN_THROW(
Error(
"cannot parse '" + token +
"' as an argument name"));
210 ++positionalArgIndex;
213 for (
const auto& argName : m_requiredArgs) {
214 if (ca.count(argName) == 0) {
215 NDN_THROW(
Error(argName +
": required argument is missing"));
225 if (s ==
"on" || s ==
"true" || s ==
"enabled" || s ==
"yes" || s ==
"1") {
228 if (s ==
"off" || s ==
"false" || s ==
"disabled" || s ==
"no" || s ==
"0") {
231 NDN_THROW(std::invalid_argument(
"unrecognized boolean value '" + s +
"'"));
243 NDN_THROW(std::invalid_argument(
"unrecognized ReportFormat '" + s +
"'"));
246 static FacePersistency
249 if (s ==
"persistent") {
250 return FacePersistency::FACE_PERSISTENCY_PERSISTENT;
252 if (s ==
"permanent") {
253 return FacePersistency::FACE_PERSISTENCY_PERMANENT;
255 NDN_THROW(std::invalid_argument(
"unrecognized FacePersistency '" + s +
"'"));
259 CommandDefinition::parseValue(
ArgValueType valueType,
const std::string& token)
271 int64_t v = boost::lexical_cast<int64_t>(token);
273 NDN_THROW(std::out_of_range(
"value '" + token +
"' is negative"));
275 return static_cast<uint64_t
>(v);
288 return ndn::FaceUri(token);
292 return boost::lexical_cast<uint64_t>(token);
294 catch (
const boost::bad_lexical_cast&) {
295 return ndn::FaceUri(token);
302 return boost::lexical_cast<RouteOrigin>(token);