-
Notifications
You must be signed in to change notification settings - Fork 425
Expand file tree
/
Copy pathRootCommand.cs
More file actions
98 lines (87 loc) · 3.66 KB
/
RootCommand.cs
File metadata and controls
98 lines (87 loc) · 3.66 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
98
// 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.Collections.Generic;
using System.CommandLine.Completions;
using System.CommandLine.Help;
using System.IO;
using System.Linq;
using System.Reflection;
namespace System.CommandLine
{
/// <summary>
/// Represents the main action that the application performs.
/// </summary>
/// <remarks>
/// Use the RootCommand object without any subcommands for applications that perform one action. Add subcommands
/// to the root for applications that require actions identified by specific strings. For example, `dir` does not
/// use any subcommands. See <see cref="Command"/> for applications with multiple actions.
/// </remarks>
public class RootCommand : Command
{
private static string? _executablePath;
private static string? _executableName;
private static string? _toolCommandName;
private static bool _toolCommandNameInitialized;
private string? _helpName;
private bool _helpNameSet;
/// <param name="description">The description of the command, shown in help.</param>
public RootCommand(string description = "") : base(ExecutableName, description)
{
Options.Add(new HelpOption());
Options.Add(new VersionOption());
Directives = new ChildSymbolList<Directive>(this)
{
new SuggestDirective()
};
}
/// <summary>
/// Gets or sets the name used for the root command in help output.
/// </summary>
/// <remarks>
/// If not explicitly set, defaults to the <c>ToolCommandName</c> MSBuild property value
/// (when available via assembly metadata), or <c>null</c> to fall back to <see cref="Symbol.Name"/>.
/// </remarks>
public string? HelpName
{
get => _helpNameSet ? _helpName : ToolCommandName;
set
{
_helpName = value;
_helpNameSet = true;
}
}
/// <summary>
/// Represents all of the directives that are valid under the root command.
/// </summary>
public IList<Directive> Directives { get; }
/// <summary>
/// Adds a <see cref="Directive"/> to the command.
/// </summary>
public void Add(Directive directive) => Directives.Add(directive);
/// <summary>
/// The name of the currently running executable.
/// </summary>
public static string ExecutableName
=> _executableName ??= Path.GetFileNameWithoutExtension(ExecutablePath).Replace(" ", "");
/// <summary>
/// The path to the currently running executable.
/// </summary>
public static string ExecutablePath => _executablePath ??= GetExecutablePath(Environment.GetCommandLineArgs());
private static string GetExecutablePath(IReadOnlyList<string> commandLineArgs)
=> commandLineArgs.Count > 0 ? commandLineArgs[0] : string.Empty;
private static string? ToolCommandName
{
get
{
if (!_toolCommandNameInitialized)
{
_toolCommandName = Assembly.GetEntryAssembly()?
.GetCustomAttributes<AssemblyMetadataAttribute>()
.FirstOrDefault(a => a.Key == "System.CommandLine.ToolCommandName")?.Value;
_toolCommandNameInitialized = true;
}
return _toolCommandName;
}
}
}
}