示例#1
0
        public static async Task <int> Run <T>(string[] args, T commandLineBinding) where T : ICommandLineBinding
        {
            var initialisationInformation = new InitialisationInformation();

            initialisationInformation.AddMessage(MessageType.Information, $"Received command line {string.Join(" ", args.Select(x => $"[{x}]"))}");
            var consoleHost = new ConsoleHost();

            var parserType        = typeof(CommandLineParser <>).MakeGenericType(commandLineBinding.CommandLineType);
            var commandLineParser = (ICommandLineParser)Activator.CreateInstance(parserType);

            var parseResult = commandLineParser.Parse(args, initialisationInformation);

            switch (parseResult.ParseResult)
            {
            case ParseResult.Failed:
                consoleHost.ReportInitialisation(initialisationInformation);
                return(ReturnCodes.CommandLineParsingFailed);

            case ParseResult.SuccessfulAndExit:
                consoleHost.ReportInitialisation(initialisationInformation);
                return(ReturnCodes.Success);

            default:
                var          applicationBootstrapper = commandLineBinding.CreateBootstrapper(parseResult.CommandLine);
                IApplication application             = null;

                try
                {
                    application = applicationBootstrapper.Bootstrap();
                }
                catch (Exception ex)
                {
                    initialisationInformation.AddMessage(MessageType.Error, $"Failed to bootstrap{Environment.NewLine}{ex}");
                    consoleHost.ReportInitialisation(initialisationInformation);
                    return(ReturnCodes.BoostrapFailed);
                }

                var returnCode = await consoleHost.Run(application, initialisationInformation);

                return((int)returnCode);
            }
        }
示例#2
0
        public (ParseResult ParseResult, object CommandLine) Parse(string[] args, InitialisationInformation initialisationInformation)
        {
            var stringBuilder = new StringBuilder();

            using (var writer = new StringWriter(stringBuilder))
                using (var parser = new Parser(x =>
                {
                    x.IgnoreUnknownArguments = true;
                    x.HelpWriter = writer;
                }))
                {
                    var parserResult = parser.ParseArguments <TCommandLine>(args);

                    if (parserResult.Tag != ParserResultType.Parsed)
                    {
                        if (parserResult.IsRequestToShowHelp() || parserResult.IsRequestToShowVerbHelp() ||
                            parserResult.IsRequestToShowVersion())
                        {
                            initialisationInformation.AddMessage(MessageType.Information, stringBuilder.ToString());
                            return(ParseResult.SuccessfulAndExit, null);
                        }

                        initialisationInformation.AddMessage(MessageType.Error, "Failed to parse command line arguments");
                        initialisationInformation.AddMessage(MessageType.Information, stringBuilder.ToString());
                        return(ParseResult.Failed, null);
                    }

                    object commandLine = null;
                    parserResult.WithParsed(parsedCommandLine =>
                    {
                        commandLine           = parsedCommandLine;
                        var formatCommandLine = parser.FormatCommandLine(commandLine);
                        initialisationInformation.AddMessage(MessageType.Information, $"Command line interpreted as [{formatCommandLine}]");
                    });
                    return(ParseResult.Successful, commandLine);
                }
        }