|
| 1 | +/** |
| 2 | + * @brief Command line tool to generate TreeNodesModel XML. |
| 3 | + * |
| 4 | + * This tool outputs the TreeNodesModel XML for nodes registered in the factory, |
| 5 | + * optionally including builtin nodes and loading additional plugins. |
| 6 | + * |
| 7 | + * Usage: |
| 8 | + * bt_nodes_model [--include-builtin] [--plugin <path>]... |
| 9 | + * |
| 10 | + * Options: |
| 11 | + * --include-builtin Include builtin nodes in the output |
| 12 | + * --plugin <path> Load a plugin from the specified path (can be repeated) |
| 13 | + * -h, --help Show this help message |
| 14 | + */ |
| 15 | + |
| 16 | +#include "behaviortree_cpp/bt_factory.h" |
| 17 | +#include "behaviortree_cpp/xml_parsing.h" |
| 18 | + |
| 19 | +#include <cstdio> |
| 20 | +#include <cstring> |
| 21 | +#include <iostream> |
| 22 | +#include <string> |
| 23 | +#include <vector> |
| 24 | + |
| 25 | +void printUsage(const char* program_name) |
| 26 | +{ |
| 27 | + std::printf("Usage: %s [OPTIONS]\n\n", program_name); |
| 28 | + std::printf("Generate TreeNodesModel XML for BehaviorTree.CPP nodes.\n\n"); |
| 29 | + std::printf("Options:\n"); |
| 30 | + std::printf(" --include-builtin Include builtin nodes in the output\n"); |
| 31 | + std::printf(" --plugin <path> Load a plugin from the specified path\n"); |
| 32 | + std::printf(" (can be specified multiple times)\n"); |
| 33 | + std::printf(" -h, --help Show this help message\n\n"); |
| 34 | + std::printf("Examples:\n"); |
| 35 | + std::printf(" %s --include-builtin\n", program_name); |
| 36 | + std::printf(" %s --plugin ./libmy_nodes.so\n", program_name); |
| 37 | + std::printf(" %s --include-builtin --plugin ./libplugin1.so --plugin " |
| 38 | + "./libplugin2.so\n", |
| 39 | + program_name); |
| 40 | +} |
| 41 | + |
| 42 | +int main(int argc, char* argv[]) |
| 43 | +{ |
| 44 | + bool include_builtin = false; |
| 45 | + std::vector<std::string> plugins; |
| 46 | + |
| 47 | + // Parse command line arguments |
| 48 | + for(int i = 1; i < argc; ++i) |
| 49 | + { |
| 50 | + if(std::strcmp(argv[i], "--include-builtin") == 0) |
| 51 | + { |
| 52 | + include_builtin = true; |
| 53 | + } |
| 54 | + else if(std::strcmp(argv[i], "--plugin") == 0) |
| 55 | + { |
| 56 | + if(i + 1 >= argc) |
| 57 | + { |
| 58 | + std::fprintf(stderr, "Error: --plugin requires a path argument\n"); |
| 59 | + return 1; |
| 60 | + } |
| 61 | + plugins.push_back(argv[++i]); |
| 62 | + } |
| 63 | + else if(std::strcmp(argv[i], "-h") == 0 || std::strcmp(argv[i], "--help") == 0) |
| 64 | + { |
| 65 | + printUsage(argv[0]); |
| 66 | + return 0; |
| 67 | + } |
| 68 | + else |
| 69 | + { |
| 70 | + std::fprintf(stderr, "Error: Unknown option '%s'\n", argv[i]); |
| 71 | + printUsage(argv[0]); |
| 72 | + return 1; |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + BT::BehaviorTreeFactory factory; |
| 77 | + |
| 78 | + // Load plugins |
| 79 | + for(const auto& plugin_path : plugins) |
| 80 | + { |
| 81 | + try |
| 82 | + { |
| 83 | + factory.registerFromPlugin(plugin_path); |
| 84 | + } |
| 85 | + catch(const std::exception& e) |
| 86 | + { |
| 87 | + std::fprintf(stderr, "Error loading plugin '%s': %s\n", plugin_path.c_str(), |
| 88 | + e.what()); |
| 89 | + return 1; |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + // Generate and print the TreeNodesModel XML |
| 94 | + std::string xml = BT::writeTreeNodesModelXML(factory, include_builtin); |
| 95 | + std::cout << xml << std::endl; |
| 96 | + |
| 97 | + return 0; |
| 98 | +} |
0 commit comments