print_configuration_main.cc
Go to the documentation of this file.
00001 /*
00002  * Copyright 2019 The Cartographer Authors
00003  *
00004  * Licensed under the Apache License, Version 2.0 (the "License");
00005  * you may not use this file except in compliance with the License.
00006  * You may obtain a copy of the License at
00007  *
00008  *      http://www.apache.org/licenses/LICENSE-2.0
00009  *
00010  * Unless required by applicable law or agreed to in writing, software
00011  * distributed under the License is distributed on an "AS IS" BASIS,
00012  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013  * See the License for the specific language governing permissions and
00014  * limitations under the License.
00015  */
00016 
00017 #include <iostream>
00018 #include <vector>
00019 
00020 #include "absl/memory/memory.h"
00021 #include "absl/strings/str_split.h"
00022 #include "cartographer/common/configuration_file_resolver.h"
00023 #include "cartographer/common/lua_parameter_dictionary.h"
00024 #include "gflags/gflags.h"
00025 #include "glog/logging.h"
00026 
00027 DEFINE_string(configuration_directories, "",
00028               "Comma separated list of directories in which configuration files"
00029               " are searched, the last is always the Cartographer installation"
00030               " to allow including files from there.");
00031 DEFINE_string(configuration_basename, "",
00032               "Basename, i.e. not containing any directory prefix, of the "
00033               "configuration file.");
00034 DEFINE_string(subdictionary, "",
00035               "Only print a subdictionary referenced by its Lua ID, e.g.: "
00036               "'--subdictionary trajectory_builder.trajectory_builder_3d'");
00037 
00038 namespace cartographer {
00039 namespace common {
00040 
00041 std::unique_ptr<LuaParameterDictionary> LoadLuaDictionary(
00042     const std::vector<std::string>& configuration_directories,
00043     const std::string& configuration_basename) {
00044   auto file_resolver =
00045       absl::make_unique<ConfigurationFileResolver>(configuration_directories);
00046   const std::string code =
00047       file_resolver->GetFileContentOrDie(configuration_basename);
00048   // We use no reference count because we just want to print the configuration.
00049   return LuaParameterDictionary::NonReferenceCounted(code,
00050                                                      std::move(file_resolver));
00051 }
00052 
00053 void PrintSubdictionaryById(LuaParameterDictionary* lua_dictionary,
00054                             const std::string& subdictionary_id) {
00055   const std::vector<std::string> subdictionary_keys =
00056       absl::StrSplit(subdictionary_id, '.', absl::SkipEmpty());
00057   CHECK(!subdictionary_keys.empty()) << "Failed to parse 'subdictionary_id'.";
00058   // Keep a stack to avoid memory glitches due to unique_ptr deletion.
00059   std::vector<std::unique_ptr<LuaParameterDictionary>> stack;
00060   for (const auto& key : subdictionary_keys) {
00061     if (stack.empty()) {
00062       stack.push_back(lua_dictionary->GetDictionary(key));
00063       continue;
00064     }
00065     stack.push_back(stack.back()->GetDictionary(key));
00066   }
00067   std::cout << subdictionary_id << " = " << stack.back()->ToString()
00068             << std::endl;
00069 }
00070 
00071 }  // namespace common
00072 }  // namespace cartographer
00073 
00074 int main(int argc, char** argv) {
00075   google::InitGoogleLogging(argv[0]);
00076   google::SetUsageMessage(
00077       "Resolves and compiles a Lua configuration and prints it to stdout.\n"
00078       "The output can be restricted to a subdictionary using the optional "
00079       "'--subdictionary' parameter, which can be given in Lua syntax.\n"
00080       "The logs of the configuration file resolver are written to stderr if "
00081       "'--logtostderr' is given.");
00082   google::ParseCommandLineFlags(&argc, &argv, true);
00083 
00084   if (FLAGS_configuration_directories.empty() ||
00085       FLAGS_configuration_basename.empty()) {
00086     google::ShowUsageWithFlagsRestrict(argv[0], "print_configuration_main");
00087     return EXIT_FAILURE;
00088   }
00089 
00090   const std::vector<std::string> configuration_directories =
00091       absl::StrSplit(FLAGS_configuration_directories, ',', absl::SkipEmpty());
00092 
00093   auto lua_dictionary = ::cartographer::common::LoadLuaDictionary(
00094       configuration_directories, FLAGS_configuration_basename);
00095 
00096   if (FLAGS_subdictionary.empty()) {
00097     std::cout << "return " << lua_dictionary->ToString() << std::endl;
00098     return EXIT_SUCCESS;
00099   }
00100   ::cartographer::common::PrintSubdictionaryById(lua_dictionary.get(),
00101                                                  FLAGS_subdictionary);
00102 }


cartographer
Author(s): The Cartographer Authors
autogenerated on Thu May 9 2019 02:27:35