コード例 #1
0
        public static int DispatchCommand(IEnumerable <ConsoleCommand> commands, string[] arguments, TextWriter consoleOut, bool skipExeInExpectedUsage = false)
        {
            ConsoleCommand selectedCommand = null;

            TextWriter console = consoleOut;

            IEnumerable <ConsoleCommand> consoleCommands = commands as ConsoleCommand[] ?? commands.ToArray();

            foreach (var command in consoleCommands)
            {
                ValidateConsoleCommand(command);
            }

            try
            {
                List <string> remainingArguments;

                if (consoleCommands.Count() == 1)
                {
                    selectedCommand = consoleCommands.First();

                    if (arguments.Any() && (arguments.First().ToLower() == selectedCommand.Command.ToLower()))
                    {
                        remainingArguments = selectedCommand.GetActualOptions().Parse(arguments.Skip(1));
                    }
                    else
                    {
                        remainingArguments = selectedCommand.GetActualOptions().Parse(arguments);
                    }
                }
                else
                {
                    if (!arguments.Any())
                    {
                        throw new ConsoleHelpAsException("No arguments specified.");
                    }

                    if (arguments[0].Equals("help", StringComparison.InvariantCultureIgnoreCase))
                    {
                        selectedCommand = GetMatchingCommand(consoleCommands, arguments.Skip(1).FirstOrDefault());

                        if (selectedCommand == null)
                        {
                            ConsoleHelp.ShowSummaryOfCommands(consoleCommands, console);
                        }
                        else
                        {
                            ConsoleHelp.ShowCommandHelp(selectedCommand, console, skipExeInExpectedUsage);
                        }

                        return(-1);
                    }

                    selectedCommand = GetMatchingCommand(consoleCommands, arguments.First());

                    if (selectedCommand == null)
                    {
                        throw new ConsoleHelpAsException("Command name not recognized.");
                    }

                    remainingArguments = selectedCommand.GetActualOptions().Parse(arguments.Skip(1));
                }

                selectedCommand.CheckRequiredArguments();

                CheckRemainingArguments(remainingArguments, selectedCommand.RemainingArgumentsCount);

                var preResult = selectedCommand.OverrideAfterHandlingArgumentsBeforeRun(remainingArguments.ToArray());

                if (preResult.HasValue)
                {
                    return(preResult.Value);
                }

                ConsoleHelp.ShowParsedCommand(selectedCommand, console);

                return(selectedCommand.Run(remainingArguments.ToArray()));
            }
            catch (ConsoleHelpAsException e)
            {
                return(DealWithException(e, console, skipExeInExpectedUsage, selectedCommand, consoleCommands));
            }
            catch (NDesk.Options.OptionException e)
            {
                return(DealWithException(e, console, skipExeInExpectedUsage, selectedCommand, consoleCommands));
            }
        }
コード例 #2
0
        private static (ConsoleCommand, string[], int?) GetSelectedCommand(IEnumerable <ConsoleCommand> commands, string[] arguments, TextWriter consoleOut, bool skipExeInExpectedUsage = false)
        {
            ConsoleCommand selectedCommand = null;
            List <string>  remainingArguments;

            TextWriter console = consoleOut;

            foreach (var command in commands)
            {
                ValidateConsoleCommand(command);
            }

            try
            {
                if (commands.Count() == 1)
                {
                    selectedCommand = commands.First();

                    if (arguments.Count() > 0 && CommandMatchesArgument(selectedCommand, arguments.First()))
                    {
                        remainingArguments = selectedCommand.GetActualOptions().Parse(arguments.Skip(1));
                    }
                    else
                    {
                        remainingArguments = selectedCommand.GetActualOptions().Parse(arguments);
                    }
                }
                else
                {
                    if (arguments.Count() < 1)
                    {
                        throw new ConsoleHelpAsException("No arguments specified.");
                    }

                    if (arguments[0].Equals("help", StringComparison.InvariantCultureIgnoreCase))
                    {
                        selectedCommand = GetMatchingCommand(commands, arguments.Skip(1).FirstOrDefault());

                        if (selectedCommand == null)
                        {
                            ConsoleHelp.ShowSummaryOfCommands(commands, console);
                        }
                        else
                        {
                            ConsoleHelp.ShowCommandHelp(selectedCommand, console, skipExeInExpectedUsage);
                        }

                        return(null, null, -1);
                    }

                    selectedCommand = GetMatchingCommand(commands, arguments.First());

                    if (selectedCommand == null)
                    {
                        throw new ConsoleHelpAsException("Command name not recognized.");
                    }

                    remainingArguments = selectedCommand.GetActualOptions().Parse(arguments.Skip(1));
                }

                selectedCommand.CheckRequiredArguments();

                CheckRemainingArguments(remainingArguments, selectedCommand.RemainingArgumentsCountMin, selectedCommand.RemainingArgumentsCountMax);

                var preResult = selectedCommand.OverrideAfterHandlingArgumentsBeforeRun(remainingArguments.ToArray());

                if (preResult.HasValue)
                {
                    return(null, null, preResult);
                }

                ConsoleHelp.ShowParsedCommand(selectedCommand, console);
                return(selectedCommand, remainingArguments.ToArray(), null);
            }
            catch (ConsoleHelpAsException e)
            {
                return(null, null, DealWithException(e, console, skipExeInExpectedUsage, selectedCommand, commands));
            }
            catch (Mono.Options.OptionException e)
            {
                return(null, null, DealWithException(e, console, skipExeInExpectedUsage, selectedCommand, commands));
            }
        }