File: CommandBase.cs
Web Access
Project: src\src\sdk\src\Cli\dotnet\dotnet.csproj (dotnet)
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

#nullable disable

using System.CommandLine;
using Microsoft.DotNet.Cli.Extensions;

namespace Microsoft.DotNet.Cli;

public abstract class CommandBase
{
    protected ParseResult _parseResult;

    protected CommandBase(ParseResult parseResult)
    {
        _parseResult = parseResult;
        ShowHelpOrErrorIfAppropriate(parseResult);
    }

    protected CommandBase() { }

    protected virtual void ShowHelpOrErrorIfAppropriate(ParseResult parseResult)
    {
        parseResult.ShowHelpOrErrorIfAppropriate();
    }

    public abstract int Execute();
}

public abstract class CommandBase<TDefinition>(ParseResult parseResult) : CommandBase(parseResult)
    where TDefinition : Command
{
    protected TDefinition Definition { get; } = (TDefinition)parseResult.CommandResult.Command;
}