8000 stop using System.CommandLine types that aren't public anymore by adamsitnik · Pull Request #49181 · dotnet/sdk · GitHub
[go: up one dir, main page]

Skip to content

stop using System.CommandLine types that aren't public anymore #49181

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 10 commits into from
Jun 3, 2025

Conversation

adamsitnik
Copy link
Member
@adamsitnik adamsitnik commented May 28, 2025

First of all, I've tried to eliminate the need of using the Help APIs that were made internal. For example, for options that were shared between multiple commands, we were using WithHelpDescription to set a custom description. The fix was to simply have a dedicated copy of given option for every command, with a unique description. This had a side effect of generating completions with descriptions that are more accurate (hence the test file update).

When I got to the point where it was not worth the effort, I've followed the plan we have established offline: just copy pasting the internal types here. I've added them to the Microsoft.TemplateEngine.Cli, as it defines an interface that is using HelpContext and is being used by the other projects that customize help. That was quite straightforward, all I needed was to update the license header, namespaces and fix some code analysis issues like leading whitespace etc.

Context: dotnet/command-line-api#2563

…ption that is being used by many commands, just create a dedicated copy for each of them
@adamsitnik adamsitnik self-assigned this May 28, 2025
@adamsitnik adamsitnik force-pushed the sclHelpIsInternal branch from 77f255f to 0bbd5dc Compare May 28, 2025 15:16
@adamsitnik adamsitnik force-pushed the sclHelpIsInternal branch from 0bbd5dc to d955427 Compare May 28, 2025 15:24
@adamsitnik adamsitnik changed the title stop using System.CommandLine types that won't be public anymore reduce the need of customizing Help output May 28, 2025
@adamsitnik adamsitnik marked this pull request as ready for review May 28, 2025 15:44
@adamsitnik adamsitnik requested a review from baronfel May 28, 2025 15:46
@adamsitnik adamsitnik requested a review from a team as a code owner May 29, 2025 16:30
/// </summary>
internal static class LocalizationResources
{
private static Lazy<ResourceManager> _resourceManager = new(
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not something new, rather a re-use of a workaround from the past:

private static Lazy<ResourceManager> _resourceManager = new(
() => new ResourceManager("System.CommandLine.Properties.Resources", typeof(System.CommandLine.Symbol).Assembly));

@adamsitnik adamsitnik changed the title reduce the need of customizing Help output stop using System.CommandLine types that aren't public anymore May 29, 2025
Copy link
Member
@MiYanni MiYanni left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

General changes look fine. Just pointed out a few random things to look into.

Comment on lines +98 to +106
public static IEnumerable<Func<HelpContext, bool>> GetLayout()
{
yield return SynopsisSection();
yield return CommandUsageSection();
yield return CommandArgumentsSection();
yield return OptionsSection();
yield return SubcommandsSection();
yield return AdditionalArgumentsSection();
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, this is a bit odd...
A func is a method delegate. Why do you return a method call that creates a delegate? Just return a method itself. Meaning:

Suggested change
public static IEnumerable<Func<HelpContext, bool>> GetLayout()
{
yield return SynopsisSection();
yield return CommandUsageSection();
yield return CommandArgumentsSection();
yield return OptionsSection();
yield return SubcommandsSection();
yield return AdditionalArgumentsSection();
}
public static IEnumerable<Func<HelpContext, bool>> GetLayout()
{
yield return SynopsisSection;
yield return CommandUsageSection;
yield return CommandArgumentsSection;
yield return OptionsSection;
yield return SubcommandsSection;
yield return AdditionalArgumentsSection;
}

And change them to be the method signature. For example:

        public static Func<HelpContext, bool> SynopsisSection() =>
            ctx =>
            {
                ctx.HelpBuilder.WriteHeading(LocalizationResources.HelpDescriptionTitle(), ctx.Command.Description, ctx.Output);
                return true;
            };

Becomes:

        public static bool SynopsisSection(HelpContext context)
        {
            context.HelpBuilder.WriteHeading(LocalizationResources.HelpDescriptionTitle(), context.Command.Description, ctx.Output);
            return true;
        };

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree that it's far from perfect, but it's a copy of System.CommandLine source code and I would prefer to introduce as few changes as possible (in order to make it easier to sync changes if needed in the future)

Comment on lines 196 to 207
if (symbol is Option or Command)
{
return GetOptionOrCommandRow();
}
else if (symbol is Argument argument)
{
return GetCommandArgumentRow(argument);
}
else
{
throw new NotSupportedException($"Symbol type {symbol.GetType()} is not supported.");
}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: If-Else is redundant.

Suggested change
if (symbol is Option or Command)
{
return GetOptionOrCommandRow();
}
else if (symbol is Argument argument)
{
return GetCommandArgumentRow(argument);
}
else
{
throw new NotSupportedException($"Symbol type {symbol.GetType()} is not supported.");
}
if (symbol is Option or Command)
{
return GetOptionOrCommandRow();
}
if (symbol is Argument argument)
{
return GetCommandArgumentRow(argument);
}
throw new NotSupportedException($"Symbol type {symbol.GetType()} is not supported.");

{
return ("/", rawAlias.Substring(1));
}
else if (rawAlias[0] == '-')
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Redundant else.

Comment on lines 40 to 43
int hashCode = -244751520;
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(FirstColumnText);
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(SecondColumnText);
return hashCode;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

? What are these numbers? 629A

@adamsitnik adamsitnik merged commit 1c12907 into main Jun 3, 2025
30 checks passed
@adamsitnik adamsitnik deleted the sclHelpIsInternal branch June 3, 2025 15:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants
0