forked from dotnet/command-line-api
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathVersionOption.cs
More file actions
97 lines (83 loc) · 3.67 KB
/
Copy pathVersionOption.cs
File metadata and controls
97 lines (83 loc) · 3.67 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
// Copyright (c) .NET Foundation and contributors. All rights reserved.
// Licensed under the MIT license. See LICENSE file in the project root for full license information.
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.Linq;
using System.Reflection;
namespace System.CommandLine
{
/// <summary>
/// Represents a standard option that indicates that version information should be displayed for the app.
/// </summary>
public sealed class VersionOption : Option
{
private CommandLineAction? _action;
/// <summary>
/// Initializes a new instance of <see cref="VersionOption" />.
/// </summary>
/// <remarks>
/// When added to a <see cref="Command"/>, it enables the use of a <c>--version</c> option, which, when specified in command line input, short circuits normal command handling and instead writes out version information before exiting.
/// </remarks>
public VersionOption() : this("--version")
{
}
/// <summary>
/// Initializes a new instance of <see cref="VersionOption" />.
/// </summary>
/// <remarks>
/// When added to a <see cref="Command"/>, it enables the use of a provided option name and aliases, which, when specified in command line input, short circuits normal command handling and instead writes out version information before exiting.
/// </remarks>
public VersionOption(string name, params string[] aliases)
: base(name, aliases)
{
Description = LocalizationResources.VersionOptionDescription();
AddValidators();
Arity = ArgumentArity.Zero;
}
/// <inheritdoc />
public override CommandLineAction? Action
{
get => _action ??= new VersionOptionAction();
set => _action = value ?? throw new ArgumentNullException(nameof(value));
}
private void AddValidators()
{
Validators.Add(static result =>
{
if (result.Parent is CommandResult parent &&
parent.Children.Any(r =>
r is not OptionResult { Option: VersionOption } &&
r is not OptionResult { Implicit: true }))
{
result.AddError(LocalizationResources.VersionOptionCannotBeCombinedWithOtherArguments(result.IdentifierToken?.Value ?? result.Option.Name));
}
});
}
internal override bool Greedy => false;
internal override Argument Argument => Argument.None;
/// <inheritdoc />
public override Type ValueType => typeof(void);
private sealed class VersionOptionAction : SynchronousCommandLineAction
{
public override int Invoke(ParseResult parseResult)
{
parseResult.InvocationConfiguration.Output.WriteLine(GetExecutableVersion());
return 0;
}
public override bool ClearsParseErrors => true;
private static string GetExecutableVersion()
{
var assembly = Assembly.GetEntryAssembly() ?? Assembly.GetExecutingAssembly();
var assemblyVersionAttribute = assembly.GetCustomAttribute<AssemblyInformationalVersionAttribute>();
if (assemblyVersionAttribute is null)
{
return assembly.GetName().Version?.ToString() ?? "";
}
else
{
return assemblyVersionAttribute.InformationalVersion;
}
}
}
}
}