private int MainInternal(string[] args, CancellationToken cancellationToken) { var app = new CommandLineApplication(); app.Name = "dotnet-watch"; app.FullName = "Microsoft dotnet File Watcher"; app.HelpOption("-?|-h|--help"); app.OnExecute(() => { var projectToWatch = Path.Combine(Directory.GetCurrentDirectory(), ProjectModel.Project.FileName); var watcher = DotNetWatcher.CreateDefault(_loggerFactory); try { watcher.WatchAsync(projectToWatch, args, cancellationToken).Wait(); } catch (AggregateException ex) { if (ex.InnerExceptions.Count != 1 || !(ex.InnerException is TaskCanceledException)) { throw; } } return(0); }); if (args == null || args.Length == 0 || args[0].Equals("--help", StringComparison.OrdinalIgnoreCase) || args[0].Equals("-h", StringComparison.OrdinalIgnoreCase) || args[0].Equals("-?", StringComparison.OrdinalIgnoreCase)) { app.ShowHelp(); return(1); } return(app.Execute()); }
private int MainInternal(string[] watchArgs, string[] appArgs, CancellationToken cancellationToken) { var app = new CommandLineApplication(); app.Name = "dotnet-watch"; app.FullName = "Microsoft dotnet File Watcher"; app.HelpOption("-?|-h|--help"); var commandArg = app.Option( "--command <COMMAND>", "Optional. The dotnet command to run. Default: 'run'.", CommandOptionType.SingleValue); var workingDirArg = app.Option( "--working-dir <DIR>", "Optional. The working directory. Default: project's directory.", CommandOptionType.SingleValue); var exitOnChangeArg = app.Option( "--exit-on-change", "Optional. The watcher will exit when a file change is detected instead of restarting the process. Default: not set.", CommandOptionType.NoValue); app.OnExecute(() => { var projectToWatch = Path.Combine(Directory.GetCurrentDirectory(), ProjectModel.Project.FileName); var command = commandArg.Value(); if (!commandArg.HasValue()) { // The default command is "run". In this case we always assume the arguments are passed to the application being run. // If you want a different behaviour for run you need to use --command and pass the full arguments // Run is special because it requires a "--" before the arguments being passed to the application, // so the two command below are equivalent and resolve to "dotnet run -- --foo": // 1. dotnet watch --foo // 2. dotnet watch --command run -- -- --foo (yes, there are two "--") if (appArgs.Length > 0) { var newAppArgs = new string[appArgs.Length + 1]; newAppArgs[0] = AppArgumentSeparator; appArgs.CopyTo(newAppArgs, 1); appArgs = newAppArgs; } command = "run"; } var workingDir = workingDirArg.HasValue() ? workingDirArg.Value() : Path.GetDirectoryName(projectToWatch); var watcher = DotNetWatcher.CreateDefault(_loggerFactory); watcher.ExitOnChange = exitOnChangeArg.HasValue(); try { watcher.WatchAsync(projectToWatch, command, appArgs, workingDir, cancellationToken).Wait(); } catch (AggregateException ex) { if (ex.InnerExceptions.Count != 1 || !(ex.InnerException is TaskCanceledException)) { throw; } } return(1); }); return(app.Execute(watchArgs)); }