diff --git a/src/Microsoft.DotNet.Watcher.Tools/CommandLineOptions.cs b/src/Microsoft.DotNet.Watcher.Tools/CommandLineOptions.cs index aea719428d..0b09620e00 100644 --- a/src/Microsoft.DotNet.Watcher.Tools/CommandLineOptions.cs +++ b/src/Microsoft.DotNet.Watcher.Tools/CommandLineOptions.cs @@ -3,6 +3,7 @@ using System; using System.Collections.Generic; +using System.Reflection; using Microsoft.DotNet.Watcher.Tools; using Microsoft.Extensions.CommandLineUtils; using Microsoft.Extensions.Tools.Internal; @@ -61,6 +62,7 @@ Examples: CommandOptionType.NoValue); var optVerbose = app.Option("-v|--verbose", "Show verbose output", CommandOptionType.NoValue); + app.VersionOptionFromAssemblyAttributes(typeof(Program).GetTypeInfo().Assembly); app.OnExecute(() => { diff --git a/src/Microsoft.Extensions.Caching.SqlConfig.Tools/Program.cs b/src/Microsoft.Extensions.Caching.SqlConfig.Tools/Program.cs index eb46dcb0a8..c0294c3c41 100644 --- a/src/Microsoft.Extensions.Caching.SqlConfig.Tools/Program.cs +++ b/src/Microsoft.Extensions.Caching.SqlConfig.Tools/Program.cs @@ -4,6 +4,7 @@ using System; using System.Data; using System.Data.SqlClient; +using System.Reflection; using Microsoft.Extensions.CommandLineUtils; using Microsoft.Extensions.Logging; @@ -36,11 +37,14 @@ namespace Microsoft.Extensions.Caching.SqlConfig.Tools var description = "Creates table and indexes in Microsoft SQL Server database " + "to be used for distributed caching"; - var app = new CommandLineApplication(); - app.Name = "dotnet-sql-cache"; - app.Description = description; - + var app = new CommandLineApplication + { + Name = "dotnet sql-cache", + FullName = "SQL Server Cache Command Line Tool", + Description = description, + }; app.HelpOption("-?|-h|--help"); + app.VersionOptionFromAssemblyAttributes(typeof(Program).GetTypeInfo().Assembly); app.Command("create", command => { diff --git a/src/Microsoft.Extensions.Caching.SqlConfig.Tools/project.json b/src/Microsoft.Extensions.Caching.SqlConfig.Tools/project.json index 3ff33ed3bd..546d267e97 100644 --- a/src/Microsoft.Extensions.Caching.SqlConfig.Tools/project.json +++ b/src/Microsoft.Extensions.Caching.SqlConfig.Tools/project.json @@ -4,7 +4,8 @@ "outputName": "dotnet-sql-cache", "emitEntryPoint": true, "warningsAsErrors": true, - "keyFile": "../../tools/Key.snk" + "keyFile": "../../tools/Key.snk", + "compile": "../Shared/CommandLineApplicationExtensions.cs" }, "description": "Command line tool to create tables and indexes in a Microsoft SQL Server database for distributed caching.", "packOptions": { diff --git a/src/Microsoft.Extensions.SecretManager.Tools/Internal/CommandLineOptions.cs b/src/Microsoft.Extensions.SecretManager.Tools/CommandLineOptions.cs similarity index 76% rename from src/Microsoft.Extensions.SecretManager.Tools/Internal/CommandLineOptions.cs rename to src/Microsoft.Extensions.SecretManager.Tools/CommandLineOptions.cs index 9a397eee59..d8e4979838 100644 --- a/src/Microsoft.Extensions.SecretManager.Tools/Internal/CommandLineOptions.cs +++ b/src/Microsoft.Extensions.SecretManager.Tools/CommandLineOptions.cs @@ -3,18 +3,19 @@ using System.Reflection; using Microsoft.Extensions.CommandLineUtils; +using Microsoft.Extensions.SecretManager.Tools.Internal; using Microsoft.Extensions.Tools.Internal; -namespace Microsoft.Extensions.SecretManager.Tools.Internal +namespace Microsoft.Extensions.SecretManager.Tools { public class CommandLineOptions { - public string Id { get; set; } - public bool IsVerbose { get; set; } - public bool IsHelp { get; set; } - public string Project { get; set; } public ICommand Command { get; set; } - public string Configuration { get; set; } + public string Configuration { get; private set; } + public string Id { get; private set; } + public bool IsHelp { get; private set; } + public bool IsVerbose { get; private set; } + public string Project { get; private set; } public static CommandLineOptions Parse(string[] args, IConsole console) { @@ -28,7 +29,7 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal }; app.HelpOption(); - app.VersionOption("--version", GetInformationalVersion()); + app.VersionOptionFromAssemblyAttributes(typeof(Program).GetTypeInfo().Assembly); var optionVerbose = app.Option("-v|--verbose", "Verbose output", CommandOptionType.NoValue, inherited: true); @@ -68,17 +69,5 @@ namespace Microsoft.Extensions.SecretManager.Tools.Internal return options; } - - private static string GetInformationalVersion() - { - var assembly = typeof(Program).GetTypeInfo().Assembly; - var attribute = assembly.GetCustomAttribute(); - - var versionAttribute = attribute == null ? - assembly.GetName().Version.ToString() : - attribute.InformationalVersion; - - return versionAttribute; - } } } \ No newline at end of file diff --git a/src/Microsoft.Extensions.SecretManager.Tools/Internal/CommandLineApplicationExtensions.cs b/src/Microsoft.Extensions.SecretManager.Tools/Internal/CommandLineApplicationExtensions.cs deleted file mode 100644 index 386a5ddf30..0000000000 --- a/src/Microsoft.Extensions.SecretManager.Tools/Internal/CommandLineApplicationExtensions.cs +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright (c) .NET Foundation. All rights reserved. -// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. - -using System; - -namespace Microsoft.Extensions.CommandLineUtils -{ - public static class UserSecretsCommandLineExtensions - { - public static CommandOption HelpOption(this CommandLineApplication app) - { - return app.HelpOption("-?|-h|--help"); - } - - public static void OnExecute(this CommandLineApplication app, Action action) - { - app.OnExecute(() => - { - action(); - return 0; - }); - } - } -} \ No newline at end of file diff --git a/src/Shared/CommandLineApplicationExtensions.cs b/src/Shared/CommandLineApplicationExtensions.cs new file mode 100644 index 0000000000..0a7e16a5bf --- /dev/null +++ b/src/Shared/CommandLineApplicationExtensions.cs @@ -0,0 +1,35 @@ +// Copyright (c) .NET Foundation. All rights reserved. +// Licensed under the Apache License, Version 2.0. See License.txt in the project root for license information. + +using System; +using System.Reflection; + +namespace Microsoft.Extensions.CommandLineUtils +{ + public static class CommandLineApplicationExtensions + { + public static CommandOption HelpOption(this CommandLineApplication app) + => app.HelpOption("-?|-h|--help"); + + public static void OnExecute(this CommandLineApplication app, Action action) + => app.OnExecute(() => + { + action(); + return 0; + }); + + public static void VersionOptionFromAssemblyAttributes(this CommandLineApplication app, Assembly assembly) + => app.VersionOption("--version", GetInformationalVersion(assembly)); + + private static string GetInformationalVersion(Assembly assembly) + { + var attribute = assembly.GetCustomAttribute(); + + var versionAttribute = attribute == null + ? assembly.GetName().Version.ToString() + : attribute.InformationalVersion; + + return versionAttribute; + } + } +} \ No newline at end of file