示例#1
0
        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);
                        }
                    });
                });
            }
        }
示例#2
0
 public void AddCommand(ICliCommand command)
 {
     if (command.Command.Contains(" "))
     {
         throw new Exception("Command cannot contain spaces.");
     }
     _commands.Add(command.Command, command);
 }
示例#3
0
文件: CliCore.cs 项目: yurikus/certes
        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);
        }
示例#4
0
 public void RemoveCommand(ICliCommand command)
 {
     _commands.Remove(command.Command);
 }
示例#5
0
 private void PrintCommand(ICliCommand cliCommand)
 {
     Console.WriteLine($"[{cliCommand.CommandKey}] {cliCommand.Name}");
 }
示例#6
0
 public CliCommandHelpText(ICliCommand command) => _command = command;
示例#7
0
 public static string Name(this ICliCommand cmd) => cmd.Info.Name;
示例#8
0
        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
        }
示例#9
0
        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;
            }
        }