1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
|
/*
* This source code is released into the public domain.
*/
module;
#include <iostream>
#include <print>
#include <ranges>
#include <string>
#include <vector>
module nihil.cli;
namespace nihil {
command_tree_node::command_tree_node()
: m_this_word("")
, m_command(std::make_shared<command_node>(command_node("")))
{
}
command_tree_node::command_tree_node(command_tree_node *parent,
std::string_view this_word)
: m_parent(parent)
, m_this_word(this_word)
{
}
command_tree_node::command_tree_node(command_tree_node *parent,
std::string_view this_word,
std::shared_ptr<command_node> command)
: m_parent(parent)
, m_this_word(this_word)
, m_command(std::move(command))
{
}
auto command_tree_node::get_child(this command_tree_node const &self,
std::string_view child)
-> command_tree_node const *
{
if (auto it = self.m_children.find(std::string(child));
it != self.m_children.end())
return &it->second;
return nullptr;
}
auto command_tree_node::get_child(this command_tree_node &self,
std::string_view child)
-> command_tree_node *
{
if (auto it = self.m_children.find(std::string(child));
it != self.m_children.end())
return &it->second;
return nullptr;
}
auto command_tree_node::get_or_create_child(this command_tree_node &self,
std::string_view child)
-> command_tree_node *
{
// Return the existing child, if there is one.
if (auto ptr = self.get_child(child); ptr != nullptr)
return ptr;
// Insert a new child.
auto [it, ok] = self.m_children.emplace(
child,
command_tree_node(&self, child));
// Give the child a dummy command.
auto path = self.m_parent != nullptr
? std::format("{} {}", self.m_parent->path(), child)
: std::string(child);
it->second.m_command = std::make_shared<command_node>(path);
return &it->second;
}
auto command_tree_node::command(this command_tree_node const &self)
-> std::shared_ptr<command_node> const &
{
return self.m_command;
}
auto command_tree_node::command(this command_tree_node &self,
std::shared_ptr<command_node> command)
-> void
{
// TODO: Put this check back without tripping from the dummy command.
//if (self.m_command != nullptr)
// throw std::logic_error("duplicate command");
self.m_command = std::move(command);
}
auto command_tree_node::print_commands(this command_tree_node const &self)
-> void
{
auto prefix = std::string(self.path());
for (auto &&[name, node] : self.m_children) {
auto command = prefix.empty()
? name
: (prefix + ' ' + name);
std::print(std::cerr, " {}\n", command);
}
}
auto command_tree_node::path(this command_tree_node const &self)
-> std::string_view
{
return self.m_command->path();
}
auto command_tree::insert(this command_tree &self,
std::vector<std::string_view> const &path,
std::shared_ptr<command_node> command)
-> void
{
auto *this_node = &self.m_root_node;
// Find the node for this key.
for (auto &&this_word : path)
this_node = this_node->get_or_create_child(this_word);
// Set the new value.
this_node->command(std::move(command));
}
auto command_tree::find(this command_tree const &self, int &argc, char **&argv)
-> command_tree_node const *
{
auto *this_node = &self.m_root_node;
// Iterate until we don't find a child command, then return that node.
while (argv[0] != nullptr) {
auto *next_node = this_node->get_child(argv[0]);
if (next_node == nullptr)
return this_node;
this_node = next_node;
--argc;
++argv;
}
// We ran out of path without finding a valid command. Return this
// node; the caller will notice the missing command.
return this_node;
}
auto build_command_tree() -> command_tree
{
auto const &commands = get_registered_commands();
auto tree = command_tree();
for (auto &&command : commands) {
auto split_path = std::vector<std::string_view>(
std::from_range,
command->path()
| std::views::split(' ')
| std::views::transform([] (auto &&r) {
return std::string_view(r);
}));
// Throws std::logic_error on duplicates.
tree.insert(split_path, command);
}
return tree;
}
} // namespace nihil
|