File: RootCommand.cs
Web Access
Project: src\src\command-line-api\src\System.CommandLine\System.CommandLine.csproj (System.CommandLine)
// 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 ??= Environment.GetCommandLineArgs()[0];

        private static string? ToolCommandName
        {
            get
            {
                if (!_toolCommandNameInitialized)
                {
                    _toolCommandName = Assembly.GetEntryAssembly()?
                        .GetCustomAttributes<AssemblyMetadataAttribute>()
                        .FirstOrDefault(a => a.Key == "System.CommandLine.ToolCommandName")?.Value;
                    _toolCommandNameInitialized = true;
                }

                return _toolCommandName;
            }
        }
    }
}