diff --git a/README.md b/README.md index 56a93fb..caf53bd 100644 --- a/README.md +++ b/README.md @@ -54,7 +54,9 @@ Usage: dotnet serve [options] Options: --version Show version information. -d|--directory The root directory to serve. [Current directory] - -o|--open-browser Open a web browser when the server starts. [false] + -o|--open-browser[:] Open a web browser when the server starts. [Default false] + You can also provide the subpath to launch by using -o:. + Example: -o:/path/index.html -p|--port Port to use [8080]. Use 0 for a dynamic port. -a|--address
Address to use. [Default = localhost]. Accepts IP addresses, diff --git a/src/dotnet-serve/CommandLineOptions.cs b/src/dotnet-serve/CommandLineOptions.cs index fa9836f..7045b19 100644 --- a/src/dotnet-serve/CommandLineOptions.cs +++ b/src/dotnet-serve/CommandLineOptions.cs @@ -23,8 +23,8 @@ internal class CommandLineOptions [DirectoryExists] public string Directory { get; internal set; } - [Option(Description = "Open a web browser when the server starts. [false]")] - public bool? OpenBrowser { get; internal set; } + [Option("-o|--open-browser[:]", CommandOptionType.SingleOrNoValue, Description = "Open a web browser when the server starts. [Default = false]\nYou can also provide the subpath to launch by using -o:.\nExample: -o:/path/index.html")] + public (bool hasValue, string path) OpenBrowser { get; internal set; } [Option(Description = "Port to use [8080]. Use 0 for a dynamic port.")] [Range(0, 65535, ErrorMessage = "Invalid port. Ports must be in the range of 0 to 65535.")] diff --git a/src/dotnet-serve/Program.cs b/src/dotnet-serve/Program.cs index 3126459..5f77c85 100644 --- a/src/dotnet-serve/Program.cs +++ b/src/dotnet-serve/Program.cs @@ -66,7 +66,25 @@ private static void ReadConfig(ConfigSection config, CommandLineOptions model) { model.Port ??= (int?)config.GetNumber("port"); model.Directory ??= config.GetString("directory"); - model.OpenBrowser ??= config.GetBoolean("open-browser"); + if (!model.OpenBrowser.hasValue) + { + try + { + if (config.TryGetBoolean("open-browser", out var openBrowser) && openBrowser) + { + model.OpenBrowser = (hasValue: true, null); + } + } + catch (FormatException) + { + // Raised when the value is not a boolean + var openBrowserPath = config.GetString("open-browser"); + if (!string.IsNullOrEmpty(openBrowserPath)) + { + model.OpenBrowser = (hasValue: true, openBrowserPath); + } + } + } model.Quiet ??= config.GetBoolean("quiet"); model.Verbose ??= config.GetBoolean("verbose"); model.CertPemPath ??= config.GetNormalizedPath("cert"); @@ -146,9 +164,16 @@ private static void WriteConfig(ConfigSection config, CommandLineOptions model) { config.SetString("directory", model.Directory); } - if (model.OpenBrowser != null) + if (model.OpenBrowser.hasValue) { - config.SetBoolean("open-browser", model.OpenBrowser.Value); + if (!string.IsNullOrEmpty(model.OpenBrowser.path)) + { + config.SetString("open-browser", model.OpenBrowser.path); + } + else + { + config.SetBoolean("open-browser", true); + } } if (model.Quiet != null) { diff --git a/src/dotnet-serve/SimpleServer.cs b/src/dotnet-serve/SimpleServer.cs index 22f3080..709f2f7 100644 --- a/src/dotnet-serve/SimpleServer.cs +++ b/src/dotnet-serve/SimpleServer.cs @@ -132,16 +132,20 @@ private void AfterServerStart(IWebHost host, ILogger logger) _console.WriteLine(""); _console.WriteLine("Press CTRL+C to exit"); - if (_options.OpenBrowser == true) + if (_options.OpenBrowser.hasValue) { - var url = NormalizeToLoopbackAddress(addresses.Addresses.First()); + var uri = new Uri(NormalizeToLoopbackAddress(addresses.Addresses.First())); - if (!string.IsNullOrEmpty(pathBase)) + if (!string.IsNullOrWhiteSpace(_options.OpenBrowser.path)) { - url += pathBase; + uri = new Uri(uri, _options.OpenBrowser.path); + } + else if (!string.IsNullOrEmpty(pathBase)) + { + uri = new Uri(uri, pathBase); } - LaunchBrowser(url); + LaunchBrowser(uri.ToString()); } static string GetListeningAddressText(IServerAddressesFeature addresses) diff --git a/test/dotnet-serve.Tests/ConfigTests.cs b/test/dotnet-serve.Tests/ConfigTests.cs index f6dbcb2..548199e 100644 --- a/test/dotnet-serve.Tests/ConfigTests.cs +++ b/test/dotnet-serve.Tests/ConfigTests.cs @@ -51,7 +51,7 @@ public void ConfigurationProvidesDefaultOptions() Assert.Equal(4242, options.Port); Assert.Equal(Path.GetTempPath(), options.Directory); - Assert.True(options.OpenBrowser); + Assert.True(options.OpenBrowser.hasValue); Assert.True(options.Quiet); Assert.True(options.Verbose); Assert.Equal(certFile, options.CertPemPath); @@ -123,7 +123,7 @@ public void CommandLineOverridesConfiguration() Assert.Equal(4242, options.Port); Assert.Equal(Path.GetTempPath(), options.Directory); - Assert.True(options.OpenBrowser); + Assert.True(options.OpenBrowser.hasValue); Assert.True(options.Quiet); Assert.True(options.Verbose); Assert.Equal(certFile, options.CertPemPath);