private static void ConfigureCommands(CommandLineApplication app) { var commands = new ICliCommand[] { new HostCommand(), new ConvertCommand() }; foreach (var command in commands) { app.Command(command.CommandName, cla => { cla.ThrowOnUnexpectedArgument = false; cla.Description = command.CommandDescription; cla.HelpOption(); var builder = new CliOptionsBuilder(cla); command.ConfigureOptions(builder); cla.OnExecute(() => { builder.ExecuteCallbacks(); var services = BuildServiceCollection(); command.ConfigureServices(services); using (var serviceProvider = services.BuildServiceProvider()) { MigrateDatabase(serviceProvider); command.Run(cla.RemainingArguments.ToArray(), serviceProvider); } }); }); } }
public void AddCommand(ICliCommand command) { if (command.Command.Contains(" ")) { throw new Exception("Command cannot contain spaces."); } _commands.Add(command.Command, command); }
private (ICliCommand Command, ArgumentSyntax Syntax)? MatchCommand( string[] args, IEnumerable <ICliCommand> groupCommands, ArgumentSyntax groupSyntax) { string helpText = null; try { var isHelpRequested = IsHelpRequested(args); ICliCommand matchCommand = null; var cmdSyntax = ArgumentSyntax.Parse(args.Skip(1).ToArray(), s => { s.HandleErrors = false; s.HandleHelp = false; s.ErrorOnUnexpectedArguments = false; s.ApplicationName = $"{groupSyntax.ApplicationName} {groupSyntax.ActiveCommand}"; foreach (var cmd in groupCommands) { var arg = cmd.Define(s); if (arg.IsActive) { matchCommand = cmd; } } if (isHelpRequested) { helpText = s.GetHelpText(); } }); if (!isHelpRequested) { return(matchCommand, cmdSyntax); } } catch (ArgumentSyntaxException) { if (!IsHelpRequested(args)) { throw; } } PrintVersion(); consoleLogger.Info(helpText); return(null); }
public void RemoveCommand(ICliCommand command) { _commands.Remove(command.Command); }
private void PrintCommand(ICliCommand cliCommand) { Console.WriteLine($"[{cliCommand.CommandKey}] {cliCommand.Name}"); }
public CliCommandHelpText(ICliCommand command) => _command = command;
public static string Name(this ICliCommand cmd) => cmd.Info.Name;
public static void StartAppLoop(ICliCommand[] commands, ILeafCliCommand quitCommand) { Cmd.WriteHeader("Ready"); Cmd.WriteLine("'?' for help"); Type currentCommandType = null; do { var path = GetCommandInheritanceChain(currentCommandType, commands) .Reverse() .Aggregate(string.Empty, (current, commandType) => $"{current}.{commandType.Name.Replace("Command", string.Empty)}"); if (path.StartsWith(".")) { path = path.Substring(1); } var prompt = Cmd.Prompt($".{path}"); var type = currentCommandType; var childCommands = commands.Where(x => x.ParentCommandType == type) .Union(new[] { quitCommand }); if (prompt.IsRoughly("?", "help")) { ShowHelp(childCommands, currentCommandType != null); continue; } if (prompt.IsRoughly("..")) { if (currentCommandType != null) { var currentCommand = commands.Single(x => x.GetType() == currentCommandType); currentCommandType = commands.Where(x => x.GetType() == currentCommand.ParentCommandType) .Select(x => x.GetType()) .SingleOrDefault(); } continue; } var command = childCommands.SingleOrDefault(x => x.CanHandle(prompt)); if (command == null) { Cmd.WriteWarningLine("Command not recognised"); continue; } if (command is ILeafCliCommand) { try { (command as ILeafCliCommand).Execute(prompt).Wait(); } catch (Exception ex) { Cmd.WriteException(ex); } } else { currentCommandType = command.GetType(); } } while (true); // ReSharper disable once FunctionNeverReturns }
private static IEnumerable<Type> GetCommandInheritanceChain(Type currentType, ICliCommand[] commands) { while (true) { if (currentType == null) { yield break; } yield return currentType; var currentCommand = commands.Single(x => x.GetType() == currentType); currentType = currentCommand.ParentCommandType; } }