-
Notifications
You must be signed in to change notification settings - Fork 10.3k
/
Copy pathCommandLineApplicationExtensions.cs
53 lines (42 loc) · 1.99 KB
/
CommandLineApplicationExtensions.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Reflection;
using System.Threading.Tasks;
namespace Microsoft.Extensions.CommandLineUtils;
internal static class CommandLineApplicationExtensions
{
public static CommandOption HelpOption(this CommandLineApplication app)
=> app.HelpOption("-?|-h|--help");
public static CommandOption VerboseOption(this CommandLineApplication app)
=> app.Option("-v|--verbose", "Show verbose output", CommandOptionType.NoValue, inherited: true);
public static void OnExecute(this CommandLineApplication app, Action action)
=> app.OnExecute(() =>
{
action();
return 0;
});
public static CommandOption Option(this CommandLineApplication command, string template, string description)
=> command.Option(
template,
description,
#if NETCOREAPP
template.Contains('<')
#else
template.IndexOf('<') != -1
#endif
? template.EndsWith(">...", StringComparison.Ordinal) ? CommandOptionType.MultipleValue : CommandOptionType.SingleValue
: CommandOptionType.NoValue);
public static void VersionOptionFromAssemblyAttributes(this CommandLineApplication app)
=> app.VersionOptionFromAssemblyAttributes(typeof(CommandLineApplicationExtensions).Assembly);
public static void VersionOptionFromAssemblyAttributes(this CommandLineApplication app, Assembly assembly)
=> app.VersionOption("--version", GetInformationalVersion(assembly));
private static string GetInformationalVersion(Assembly assembly)
{
var attribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
var versionAttribute = attribute == null
? assembly.GetName().Version.ToString()
: attribute.InformationalVersion;
return versionAttribute;
}
}