6 using Core.Commands.Results;
14 bool Predicate(
ICommand x) => x.
Name.Equals(cmdName, StringComparison.CurrentCulture);
17 outCommand = Commands.FirstOrDefault(Predicate);
19 if (outCommand !=
null) {
return true; }
22 outCommand = plugin.PluginCommands.FirstOrDefault(Predicate);
24 if (outCommand !=
null) {
32 List<ICommand> Commands => m_commands ??=
new List<ICommand> {
34 "clear",
"Clears the terminal of any text",
35 "Clears the internal buffers of the terminal.",
39 "quit",
"Gracefully terminates Hermod.",
40 "Unloads all plugins, shuts down internal services,\n" +
41 "and gracfully shuts Hermod down. Useful for maintenance.",
45 "help",
"Displays a help text",
46 "Prints general help about all commands found\n" +
47 "in Hermod and any loaded plugins.\n" +
48 "Type help <command> for detailled information.",
52 "load-plugin",
"Loads one or more plugins",
53 "Loads one or more plugins from disk and places\n" +
54 "them in the application's namespace.\n" +
56 "\tload-plugin <plugin-file [<plugin-file>]>",
60 "unload-plugin",
"Unloads one or more plugins from Hermod",
61 "Unloads one or more plugins from Hermod's namespace.\n" +
62 "Plugins are gracefully stopped before being unloaded.\n" +
64 "\tunload-plugin <plugin-name> # unload a single plugin\n" +
65 "\tunload-plugin <plugin-name [<plugin-name>]> # unload multiple plugins\n" +
66 "\tunload-plugin --all, -a # unload all plugins",
68 new getopt.net.Option { Name =
"all", ArgumentType = getopt.net.ArgumentType.None, Value =
'a' }
73 if (args.Length > 0) {
return HandleDisplayCommandHelp(args.First()); }
75 var sBuilder =
new StringBuilder();
77 void DumpCommandShortHelp(
ICommand command) {
78 sBuilder.AppendLine($
"{ command.Name,-30 }{ command.ShortDescription,-80 }");
81 sBuilder.AppendLine(
"Built-ins:");
83 foreach (var command
in Commands) {
84 DumpCommandShortHelp(command);
86 sBuilder.AppendLine();
88 sBuilder.AppendLine(
"Plugin provided:");
90 sBuilder.AppendLine($
"{ plugin.PluginName }:");
91 foreach (var command
in plugin.PluginCommands) {
92 DumpCommandShortHelp(command);
94 sBuilder.AppendLine();
103 .FirstOrDefault(c => c.Name.Equals(arg, StringComparison.InvariantCulture));
104 if (command is
null) {
112 if (args.Length == 0) {
113 return new CommandErrorResult(
"Missing input parameters!",
new ArgumentNullException(nameof(args),
"Input args must not be null or empty!"));
116 foreach (var arg
in args) {
117 if (
string.IsNullOrEmpty(arg)) {
118 m_appLogger.Warning($
"Encountered empty argument in { nameof(HandleLoadPlugin) }. Ignoring...");
123 var fInfo =
new FileInfo(arg);
126 }
catch (Exception ex) {
131 return new CommandResult($
"Successfully loaded plugin(s).",
null);
139 var sBuilder =
new StringBuilder();
140 PluginRegistry.Instance.Plugins
141 .Select(x => $
"{ x.PluginName } v{ x.PluginVersion }")
143 .ForEach(x => sBuilder.AppendLine(x));
A ICommandResult which implicates the command encountered an error.
The most basic of CommandResult implementations.
static CommandResult Empty
Provides an empty CommandResult for commands which do not require a result.
Represents a single command that can only be executed on the (interactive) terminal.
ICommandResult HandleUnloadPlugin(params string[] args)
ICommandResult HandleDisplayHelp(params string[] args)
ICommandResult HandleGetPlugins(params string[] args)
ICommandResult HandleDisplayCommandHelp(string arg)
ICommandResult HandleLoadPlugin(params string[] args)
bool TryGetCommand(string cmdName, out ICommand? outCommand)
Handles the loading, unloading, and general management of plugins.
List< IPlugin > Plugins
Gets a list of all loaded IPlugin instances.
IPlugin? LastRegisteredPlugin
Gets or sets the last IPlugin to be registered.
void LoadPlugin(FileInfo pluginFile)
Loads one or plugins from an Assembly on disk.
static PluginRegistry Instance
Gets the current instance of this object.
List< ICommand > GetAllCommands()
Gets a list containing all ICommand instances known to the application at the current time.
Specialised variation of ICommand with generic typing.
string Name
The (callable) name for the command.
A generic, object-based variation of ICommandResult<T>.
void OnStart()
Method that is called once Hermod has completed its startup procedures and is ready to run.