Blender V5.3
blender_cli_command.cc
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2001-2002 NaN Holding BV. All rights reserved.
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
25
26#include <iostream>
27
28#include "BLI_vector.hh"
29
30#include "BKE_blender_cli_command.hh" /* own include */
31
32namespace blender {
33
34/* -------------------------------------------------------------------- */
37
38using CommandHandlerPtr = std::unique_ptr<CommandHandler>;
39
45{
46 static Vector<CommandHandlerPtr> command_handlers;
47 return command_handlers;
48}
49
50static CommandHandler *blender_cli_command_lookup(const std::string &id)
51{
52 for (CommandHandlerPtr &cmd_iter : cli_command_handlers()) {
53 if (id == cmd_iter->id) {
54 return cmd_iter.get();
55 }
56 }
57 return nullptr;
58}
59
61{
62 int index = 0;
63 for (CommandHandlerPtr &cmd_iter : cli_command_handlers()) {
64 if (cmd_iter.get() == cmd) {
65 return index;
66 }
67 index++;
68 }
69 return -1;
70}
71
73
74/* -------------------------------------------------------------------- */
77
78void BKE_blender_cli_command_register(std::unique_ptr<CommandHandler> cmd)
79{
80 bool is_duplicate = false;
81 if (CommandHandler *cmd_exists = blender_cli_command_lookup(cmd->id)) {
82 std::cerr << "warning: registered duplicate command \"" << cmd->id
83 << "\", this will be inaccessible" << std::endl;
84 cmd_exists->is_duplicate = true;
85 is_duplicate = true;
86 }
87 cmd->is_duplicate = is_duplicate;
88 cli_command_handlers().append(std::move(cmd));
89}
90
92{
93 const int cmd_index = blender_cli_command_index(cmd);
94 if (cmd_index == -1) {
95 std::cerr << "failed to unregister command handler" << std::endl;
96 return false;
97 }
98
99 /* Update duplicates after removal. */
100 if (cmd->is_duplicate) {
101 CommandHandler *cmd_other = nullptr;
102 for (CommandHandlerPtr &cmd_iter : cli_command_handlers()) {
103 /* Skip self. */
104 if (cmd == cmd_iter.get()) {
105 continue;
106 }
107 if (cmd_iter->is_duplicate && (cmd_iter->id == cmd->id)) {
108 if (cmd_other) {
109 /* Two or more found, clear and break. */
110 cmd_other = nullptr;
111 break;
112 }
113 cmd_other = cmd_iter.get();
114 }
115 }
116 if (cmd_other) {
117 cmd_other->is_duplicate = false;
118 }
119 }
120
121 cli_command_handlers().remove_and_reorder(cmd_index);
122
123 return true;
124}
125
126int BKE_blender_cli_command_exec(bContext *C, const char *id, const int argc, const char **argv)
127{
129 if (cmd == nullptr) {
130 std::cerr << "Unrecognized command: \"" << id << "\" (see 'help')" << std::endl;
131 return EXIT_FAILURE;
132 }
133 if (cmd->is_duplicate) {
134 std::cerr << "Command: \"" << id
135 << "\" was registered multiple times, must be resolved, aborting!" << std::endl;
136 return EXIT_FAILURE;
137 }
138
139 return cmd->exec(C, argc, argv);
140}
141
143{
144 /* As `cli_command_handlers` isn't ordered, sorting in-place is acceptable. */
145 std::ranges::sort(
147 [](const CommandHandlerPtr &a, const CommandHandlerPtr &b) { return a->id < b->id; });
148
149 for (int pass = 0; pass < 2; pass++) {
150 std::cout << ((pass == 0) ? "Blender Command Listing:" :
151 "Duplicate Command Listing (ignored):")
152 << std::endl;
153
154 const bool show_duplicates = pass > 0;
155 bool found = false;
156 bool has_duplicate = false;
157 for (CommandHandlerPtr &cmd_iter : cli_command_handlers()) {
158 if (cmd_iter->is_duplicate) {
159 has_duplicate = true;
160 }
161 if (cmd_iter->is_duplicate != show_duplicates) {
162 continue;
163 }
164
165 std::cout << "\t" << cmd_iter->id << std::endl;
166 found = true;
167 }
168
169 if (!found) {
170 std::cout << "\tNone found" << std::endl;
171 }
172 /* Don't print that no duplicates are found as it's not helpful. */
173 if (pass == 0 && !has_duplicate) {
174 break;
175 }
176 }
177}
178
183
185
186} // namespace blender
Blender CLI Generic --command Support.
#define C
Definition RandGen.cpp:29
virtual int exec(struct bContext *C, int argc, const char **argv)=0
BMesh Iterators.
void BKE_blender_cli_command_free_all()
static CommandHandler * blender_cli_command_lookup(const std::string &id)
int BKE_blender_cli_command_exec(struct bContext *C, const char *id, const int argc, const char **argv)
void BKE_blender_cli_command_register(std::unique_ptr< CommandHandler > cmd)
static Vector< CommandHandlerPtr > & cli_command_handlers()
static int blender_cli_command_index(const CommandHandler *cmd)
std::unique_ptr< CommandHandler > CommandHandlerPtr
void BKE_blender_cli_command_print_help()
bool BKE_blender_cli_command_unregister(CommandHandler *cmd)