public void CanParseLine_Test() { var args = ArgumentsHelper.Split("git commit -m 'message 123' "); args.ShouldContain("git"); args.ShouldContain("commit"); args.ShouldContain("-m"); args.ShouldContain("message 123"); args.ShouldNotContain(" "); args.ShouldNotContain(string.Empty); }
private async Task Run() { Thread.Sleep(500); foreach (var command in this.commands) { Console.WriteLine($"found command: {command.GetType().GetAttributeValue<VerbAttribute, string>(a => a.Name) ?? "?NAME?"} ({command.GetType()})", Color.Gray); } var parser = new Parser(); ReadLine.AutoCompletionHandler = new AutoCompletionHandler(); ReadLine.HistoryEnabled = true; var path = Path.Combine(Path.GetTempPath(), "naos_console", "history.db"); if (File.Exists(path)) { ReadLine.AddHistory(File.ReadAllLines(path)); } var originalColor = System.Console.ForegroundColor; while (true) { Thread.Sleep(500); System.Console.ForegroundColor = ConsoleColor.Cyan; var inputLine = ReadLine.Read("naos> ").Trim(); System.Console.ForegroundColor = originalColor; if (!inputLine.IsNullOrEmpty()) { foreach (var input in inputLine.Split('|')) { try { var result = parser.ParseArguments( ArgumentsHelper.Split(input.Trim()), this.commands.Select(c => c.GetType()).ToArray()) .WithNotParsed(_ => { if (!input.Contains("--help")) { Console.WriteLine("invalid command", Color.Red); } }); if (!result.TypeInfo.Current.GetAttributeValue <VerbAttribute, string>(a => a.Name).IsNullOrEmpty()) { // command found var command = (result as Parsed <object>)?.Value; if (command != null) { // send the command so it can be handled by a command handler await command.As <IConsoleCommand>().SendAsync(this.mediator).AnyContext(); } else { // command cannot be parsed, invalid options. show help Console.WriteLine( new HelpText { AddDashesToOption = true, AutoHelp = false, AutoVersion = false } .AddPreOptionsLine(result.TypeInfo.Current.GetAttributeValue <VerbAttribute, string>(a => a.HelpText) ?? string.Empty) .AddPreOptionsLine($"Usage: {result.TypeInfo.Current.GetAttributeValue<VerbAttribute, string>(a => a.Name)} [OPTIONS]") //.AddPostOptionsLine($"({result.TypeInfo.Current.PrettyName()})") .AddVerbs(this.commands.Select(c => c.GetType()).ToArray()) .AddOptions(result), Color.Gray); } } else { Console.WriteLine(); // no command found } } catch (Exception ex) { Console.WriteLine($"[{ex.GetType().PrettyName()}] {ex.GetFullMessage()}", Color.Red); Console.WriteLine($"{ex.StackTrace}", Color.Red); } } } } }