This repository was archived by the owner on May 14, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhelp.hpp
More file actions
68 lines (62 loc) · 2.02 KB
/
help.hpp
File metadata and controls
68 lines (62 loc) · 2.02 KB
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
#pragma once
#include <all.hpp>
class HelpCommand : public liteshell::BaseCommand
{
public:
HelpCommand()
: liteshell::BaseCommand(
"help",
"Get all commands or get help for a specific command",
"Provides help information about shell commands.\n"
"To get help for a specific command, specify its name as the first argument (e.g. \"help help\").",
liteshell::CommandConstraint("command", "The command to get help for", false))
{
}
DWORD run(const liteshell::Context &context)
{
try
{
auto name = context.get("command");
auto wrapper = context.client->get_optional_command(name);
if (wrapper.has_value())
{
std::cout << (*wrapper)->help();
}
else
{
auto error = liteshell::CommandNotFound(name, context.client->fuzzy_command_search(name).c_str());
throw std::invalid_argument(error.message);
}
}
catch (liteshell::ArgumentMissingError &)
{
auto commands = context.client->walk_commands();
for (auto iter = commands.begin(); iter != commands.end();)
{
if ((*iter)->name[0] == '_') // hidden command
{
iter = commands.erase(iter);
}
else
{
iter++;
}
}
std::size_t max_width = 0;
for (auto &wrapper : commands)
{
max_width = std::max(max_width, 3 + wrapper->name.size());
}
for (auto &wrapper : commands)
{
std::cout << wrapper->name;
for (auto i = wrapper->name.size(); i < max_width; i++)
{
std::cout << " ";
}
std::cout << wrapper->description << std::endl;
}
}
return 0;
}
};