-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
base: main
Are you sure you want to change the base?
Conversation
…ption that is being used by many commands, just create a dedicated copy for each of them
…alOption that is being used by many commands, just create a dedicated copy for each of them
…t the description to desired text
77f255f
to
0bbd5dc
Compare
src/Cli/dotnet/Commands/Workload/Search/WorkloadSearchVersionsCommandParser.cs
Outdated
Show resolved
Hide resolved
0bbd5dc
to
d955427
Compare
/// </summary> | ||
internal static class LocalizationResources | ||
{ | ||
private static Lazy<ResourceManager> _resourceManager = new( |
There was a problem hiding this comment.
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:
sdk/src/Cli/Microsoft.TemplateEngine.Cli/Commands/create/InstantiateCommand.Help.cs
Lines 19 to 20 in 7a4d106
private static Lazy<ResourceManager> _resourceManager = new( | |
() => new ResourceManager("System.CommandLine.Properties.Resources", typeof(System.CommandLine.Symbol).Assembly)); |
…sion command when asked for parent command
There was a problem hiding this 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.
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(); | ||
} |
There was a problem hiding this comment.
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:
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;
};
There was a problem hiding this comment.
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)
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."); | ||
} |
There was a problem hiding this comment.
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.
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."); |
src/Cli/Microsoft.TemplateEngine.Cli/Help/HelpBuilderExtensions.cs
Outdated
Show resolved
Hide resolved
{ | ||
return ("/", rawAlias.Substring(1)); | ||
} | ||
else if (rawAlias[0] == '-') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: Redundant else.
src/Cli/Microsoft.TemplateEngine.Cli/Help/LocalizationResources.cs
Outdated
Show resolved
Hide resolved
int hashCode = -244751520; | ||
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(FirstColumnText); | ||
hashCode = hashCode * -1521134295 + EqualityComparer<string>.Default.GetHashCode(SecondColumnText); | ||
return hashCode; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
? What are these numbers?
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 usingHelpContext
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