diff --git a/src/Microsoft.SqlTools.ServiceLayer/Program.cs b/src/Microsoft.SqlTools.ServiceLayer/Program.cs index 35a659fe..25bc92b1 100644 --- a/src/Microsoft.SqlTools.ServiceLayer/Program.cs +++ b/src/Microsoft.SqlTools.ServiceLayer/Program.cs @@ -23,9 +23,16 @@ namespace Microsoft.SqlTools.ServiceLayer /// internal static void Main(string[] args) { + // read command-line arguments + CommandOptions commandOptions = new CommandOptions(args); + if (commandOptions.ShouldExit) + { + return; + } + // turn on Verbose logging during early development // we need to switch to Normal when preparing for public preview - Logger.Initialize(minimumLogLevel: LogLevel.Verbose); + Logger.Initialize(minimumLogLevel: LogLevel.Verbose, isEnabled: commandOptions.EnableLogging); Logger.Write(LogLevel.Normal, "Starting SQL Tools Service Host"); // set up the host details and profile paths diff --git a/src/Microsoft.SqlTools.ServiceLayer/Utility/CommandOptions.cs b/src/Microsoft.SqlTools.ServiceLayer/Utility/CommandOptions.cs new file mode 100644 index 00000000..0ac371c5 --- /dev/null +++ b/src/Microsoft.SqlTools.ServiceLayer/Utility/CommandOptions.cs @@ -0,0 +1,91 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using System; + +namespace Microsoft.SqlTools.ServiceLayer.Utility +{ + /// + /// The command-line options helper class. + /// + internal class CommandOptions + { + /// + /// Construct and parse command line options from the arguments array + /// + public CommandOptions(string[] args) + { + ErrorMessage = string.Empty; + + try + { + for (int i = 0; i < args.Length; ++i) + { + string arg = args[i]; + if (arg.StartsWith("--") || arg.StartsWith("-")) + { + arg = arg.Substring(1).ToLowerInvariant(); + switch (arg) + { + case "-enable-logging": + EnableLogging = true; + break; + case "h": + case "-help": + ShouldExit = true; + return; + default: + ErrorMessage += String.Format("Unknown argument \"{0}\"" + Environment.NewLine, arg); + break; + } + } + } + } + catch (Exception ex) + { + ErrorMessage += ex.ToString(); + return; + } + finally + { + if (!string.IsNullOrEmpty(ErrorMessage) || ShouldExit) + { + Console.WriteLine(Usage); + ShouldExit = true; + } + } + } + + internal string ErrorMessage { get; private set; } + + + /// + /// Whether diagnostic logging is enabled + /// + public bool EnableLogging { get; private set; } + + /// + /// Whether the program should exit immediately. Set to true when the usage is printed. + /// + public bool ShouldExit { get; private set; } + + /// + /// Get the usage string describing command-line arguments for the program + /// + public string Usage + { + get + { + var str = string.Format("{0}" + Environment.NewLine + + "Microsoft.SqlTools.ServiceLayer.exe " + Environment.NewLine + + " Options:" + Environment.NewLine + + " [--enable-logging]" + Environment.NewLine + + " [--help]" + Environment.NewLine, + ErrorMessage); + return str; + } + } + } +} diff --git a/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/CommandOptionsTests.cs b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/CommandOptionsTests.cs new file mode 100644 index 00000000..0e2cdeec --- /dev/null +++ b/test/Microsoft.SqlTools.ServiceLayer.Test/Utility/CommandOptionsTests.cs @@ -0,0 +1,69 @@ +// +// Copyright (c) Microsoft. All rights reserved. +// Licensed under the MIT license. See LICENSE file in the project root for full license information. +// + +using Microsoft.SqlTools.ServiceLayer.Utility; +using Xunit; + +namespace Microsoft.SqlTools.Test.Utility +{ + /// + /// Tests for the CommandOptions class + /// + public class CommandOptionsTests + { + [Fact] + public void LoggingEnabledWhenFlagProvided() + { + var args = new string[] {"--enable-logging"}; + CommandOptions options = new CommandOptions(args); + Assert.NotNull(options); + + Assert.True(options.EnableLogging); + Assert.False(options.ShouldExit); + } + + [Fact] + public void LoggingDisabledWhenFlagNotProvided() + { + var args = new string[] {}; + CommandOptions options = new CommandOptions(args); + Assert.NotNull(options); + + Assert.False(options.EnableLogging); + Assert.False(options.ShouldExit); + } + + [Fact] + public void UsageIsShownWhenHelpFlagProvided() + { + var args = new string[] {"--help"}; + CommandOptions options = new CommandOptions(args); + Assert.NotNull(options); + + Assert.True(options.ShouldExit); + } + + [Fact] + public void UsageIsShownWhenBadArgumentsProvided() + { + var args = new string[] {"--unknown-argument", "/bad-argument"}; + CommandOptions options = new CommandOptions(args); + Assert.NotNull(options); + + Assert.True(options.ShouldExit); + } + + [Fact] + public void DefaultValuesAreUsedWhenNoArgumentsAreProvided() + { + var args = new string[] {}; + CommandOptions options = new CommandOptions(args); + Assert.NotNull(options); + + Assert.False(options.EnableLogging); + Assert.False(options.ShouldExit); + } + } +}