private bool ParseArgs(string[] args, out ApplicationHostOptions options, out string[] outArgs, out int exitCode) { var app = new CommandLineApplication(throwOnUnexpectedArg: false); app.Name = typeof(Program).Namespace; app.FullName = app.Name; var optionWatch = app.Option("--watch", "Watch file changes", CommandOptionType.NoValue); var optionPackages = app.Option("--packages <PACKAGE_DIR>", "Directory containing packages", CommandOptionType.SingleValue); var optionConfiguration = app.Option("--configuration <CONFIGURATION>", "The configuration to run under", CommandOptionType.SingleValue); var optionCompilationServer = app.Option("--port <PORT>", "The port to the compilation server", CommandOptionType.SingleValue); var optionTrace = app.Option("--trace <TRACE_CONFIG>", $"Configure tracing based on the specified settings (uses the same format as the {EnvironmentNames.Trace} environment variable)", CommandOptionType.SingleValue); var runCmdExecuted = false; app.HelpOption("-?|-h|--help"); app.VersionOption("--version", GetVersion); var runCmd = app.Command("run", c => { // We don't actually execute "run" command here // We are adding this command for the purpose of displaying correct help information c.Description = "Run application"; c.OnExecute(() => { runCmdExecuted = true; return 0; }); }, addHelpCommand: false, throwOnUnexpectedArg: false); app.Execute(args); options = null; outArgs = null; exitCode = 0; if (app.IsShowingInformation) { // If help option or version option was specified, exit immediately with 0 exit code return true; } else if (!(app.RemainingArguments.Any() || runCmdExecuted)) { // If no subcommand was specified, show error message // and exit immediately with non-zero exit code Console.Error.WriteLine("Please specify the command to run"); exitCode = 2; return true; } options = new ApplicationHostOptions(); options.WatchFiles = optionWatch.HasValue(); options.PackageDirectory = optionPackages.Value(); options.Trace = optionTrace.Value(); options.Configuration = optionConfiguration.Value() ?? _environment.Configuration ?? "Debug"; options.ApplicationBaseDirectory = _environment.ApplicationBasePath; var portValue = optionCompilationServer.Value() ?? Environment.GetEnvironmentVariable(EnvironmentNames.CompilationServerPort); int port; if (!string.IsNullOrEmpty(portValue) && int.TryParse(portValue, out port)) { options.CompilationServerPort = port; } var remainingArgs = new List<string>(); if (runCmdExecuted) { // Later logic will execute "run" command // So we put this argment back after it was consumed by parser remainingArgs.Add("run"); remainingArgs.AddRange(runCmd.RemainingArguments); } else { remainingArgs.AddRange(app.RemainingArguments); } if (remainingArgs.Any()) { options.ApplicationName = remainingArgs[0]; outArgs = remainingArgs.Skip(1).ToArray(); } else { outArgs = remainingArgs.ToArray(); } return false; }