1 //
2 // Copyright (c) 2010-2018 Antmicro
3 // Copyright (c) 2011-2015 Realtime Embedded
4 //
5 // This file is licensed under the MIT License.
6 // Full license text is available in 'licenses/MIT.txt'.
7 //
8 using System;
9 using System.Collections.Generic;
10 using Antmicro.Renode.UserInterface.Tokenizer;
11 using AntShell.Commands;
12 using System.Linq;
13 
14 namespace Antmicro.Renode.UserInterface.Commands
15 {
16     public class HelpCommand : Command
17     {
18         [Runnable]
GeneralHelp(ICommandInteraction writer)19         public void GeneralHelp(ICommandInteraction writer)
20         {
21             writer.WriteLine("Available commands:");
22             writer.WriteLine(string.Format("{0,-18}| {1}", "Name", "Description"));
23             writer.WriteLine("================================================================================");
24             foreach(var item in GetCommands().OrderBy(x=>x.Name))
25             {
26                 writer.WriteLine(string.Format("{0,-18}: {1}", item.Name, item.Description));
27             }
28             writer.WriteLine();
29             writer.WriteLine("You can also provide a device name to access its methods.");
30             writer.WriteLine("Use <TAB> for auto-completion.");
31         }
32 
33         [Runnable]
CommandHelp(ICommandInteraction writer, LiteralToken commandName)34         public void CommandHelp(ICommandInteraction writer, LiteralToken commandName)
35         {
36             if(!GetCommands().Any(x => x.Name == commandName.Value))
37             {
38                 writer.WriteError(String.Format("No such command: {0}.", commandName.Value));
39                 return;
40             }
41             var command = GetCommands().First(x => x.Name == commandName.Value);
42             command.PrintHelp(writer);
43         }
44 
45         private readonly Func<IEnumerable<ICommandDescription>> GetCommands;
46 
HelpCommand(Monitor monitor, Func<IEnumerable<ICommandDescription>> getCommands)47         public HelpCommand(Monitor monitor, Func<IEnumerable<ICommandDescription>> getCommands) : base(monitor, "help", "prints this help message or info about specified command.", "?", "h")
48         {
49             GetCommands = getCommands;
50         }
51     }
52 }
53 
54