Skip to content
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

[main] Update dependencies from dotnet/command-line-api #29131

Merged
merged 96 commits into from
Jun 8, 2023
Merged
Show file tree
Hide file tree
Changes from 12 commits
Commits
Show all changes
96 commits
Select commit Hold shift + click to select a range
5a564e2
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Nov 18, 2022
33ae728
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Nov 18, 2022
5981597
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Dec 13, 2022
68dda8a
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Jan 19, 2023
fa49eae
adopt to recent breaking changes: renames, removal of extension methods
adamsitnik Jan 20, 2023
bb036dd
adopt to recent breaking changes: removal of Children property
adamsitnik Jan 20, 2023
76d51bb
reduce lambda allocations
adamsitnik Jan 20, 2023
c12b639
adopt to recent breaking changes: SetDefaultValue is now avilable onl…
adamsitnik Jan 20, 2023
358676c
Merge branch 'main' into darc-main-d950046d-c6ae-4e82-ad57-55ddf2973d18
adamsitnik Jan 20, 2023
4c8a5f4
address one of the discovered problems
adamsitnik Jan 20, 2023
dd5e233
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Jan 20, 2023
03c78e4
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Jan 20, 2023
0be3134
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Jan 23, 2023
397544b
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Feb 2, 2023
8a8e7d8
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Feb 2, 2023
7268413
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Feb 2, 2023
91f6fab
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Feb 3, 2023
83f227b
Merge branch 'main' of https://github.com/dotnet/sdk into darc-main-d…
v-wuzhai Feb 9, 2023
3f1c10c
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Feb 13, 2023
8c12a78
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Feb 18, 2023
fe6e02b
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Feb 22, 2023
4e99936
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Feb 22, 2023
46d264c
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 2, 2023
6bec365
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 7, 2023
ecea358
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 7, 2023
7deda76
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 8, 2023
0f80fca
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 13, 2023
37a7d3c
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 14, 2023
113fe8f
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 14, 2023
1af00d5
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 15, 2023
c45ae0e
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 15, 2023
c145b9e
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 16, 2023
83e6b29
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 16, 2023
e7d3f2d
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 18, 2023
5e200a7
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 20, 2023
c91fc88
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 21, 2023
213425d
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 21, 2023
5188b5d
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 22, 2023
7ba6711
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 24, 2023
a0e0cbc
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 28, 2023
82496ce
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 28, 2023
167fccf
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 28, 2023
d1f4cd3
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 29, 2023
0669ec2
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 29, 2023
32538df
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 29, 2023
70330ff
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 29, 2023
d4f3b5f
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 30, 2023
799d1d6
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 31, 2023
9143238
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 31, 2023
585e936
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 31, 2023
c03f5a5
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 31, 2023
7ddfa93
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Mar 31, 2023
8b78b37
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 3, 2023
ed5f5c5
update to the latest version 1/N
adamsitnik Apr 3, 2023
504c3d8
update to the latest version 2/N
adamsitnik Apr 4, 2023
b58ff13
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 5, 2023
9da3bc4
update to the latest version 3/N
adamsitnik Apr 5, 2023
81bb95b
fix the tests:
adamsitnik Apr 6, 2023
cb2a35f
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 10, 2023
422c6cf
Merge remote-tracking branch 'origin/main' into darc-main-d950046d-c6…
adamsitnik Apr 10, 2023
20ec900
Merge branch 'darc-main-d950046d-c6ae-4e82-ad57-55ddf2973d18' of http…
adamsitnik Apr 10, 2023
87bfeb8
Merge remote-tracking branch 'origin/main' into darc-main-d950046d-c6…
adamsitnik Apr 10, 2023
0b34d1d
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 11, 2023
1e1e775
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 12, 2023
a36ba0b
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 13, 2023
97b5d95
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 13, 2023
1616c72
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 14, 2023
4f6c29b
Merge branch 'main' into darc-main-d950046d-c6ae-4e82-ad57-55ddf2973d18
lewing Apr 15, 2023
00c79b2
adjust to most recent changes
adamsitnik Apr 16, 2023
af5f0fb
Update Versions.props
mmitche Apr 16, 2023
69f8f21
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 19, 2023
4a1ba6f
test fixes:
adamsitnik Apr 19, 2023
a850d5e
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Apr 20, 2023
c4ab1cf
Merge branch 'main' of https://github.com/dotnet/sdk into darc-main-d…
v-wuzhai Apr 21, 2023
2125ad6
Merge branch 'main' into darc-main-d950046d-c6ae-4e82-ad57-55ddf2973d18
lewing Apr 26, 2023
62135f0
Update WorkloadUninstallCommandParser.cs
lewing Apr 26, 2023
db8f5b9
Update SlnListParser.cs
lewing Apr 26, 2023
27a8a30
Fix constructor call
lewing Apr 26, 2023
85fecce
Merge branch 'main' into darc-main-d950046d-c6ae-4e82-ad57-55ddf2973d18
YuliiaKovalova Apr 26, 2023
fb73afe
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] May 17, 2023
fa67214
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] May 19, 2023
69ce4c8
Merge remote-tracking branch 'origin/main' into darc-main-d950046d-c6…
adamsitnik May 31, 2023
08e0ea4
test fixes:
adamsitnik Jun 1, 2023
7ae744f
test fixes:
adamsitnik Jun 2, 2023
e3195bc
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Jun 2, 2023
cb165bf
Merge branch 'main' into darc-main-d950046d-c6ae-4e82-ad57-55ddf2973d18
JL03-Yue Jun 5, 2023
954b461
test fixes:
adamsitnik Jun 5, 2023
4ba2339
test fixes:
adamsitnik Jun 6, 2023
18dff4b
Merge branch 'main' into darc-main-d950046d-c6ae-4e82-ad57-55ddf2973d18
adamsitnik Jun 6, 2023
2c6f23b
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Jun 6, 2023
4daa474
Merge remote-tracking branch 'origin/main' into darc-main-d950046d-c6…
adamsitnik Jun 7, 2023
b21ccca
address code review feedback: restore var to get smaller diff
adamsitnik Jun 7, 2023
6bc18d3
address code review feedback: use HelpName to avoid help output changes
adamsitnik Jun 7, 2023
ceb0610
Merge remote-tracking branch 'origin/main' into darc-main-d950046d-c6…
adamsitnik Jun 7, 2023
d1d3d4e
Update dependencies from https://github.com/dotnet/command-line-api b…
dotnet-maestro[bot] Jun 8, 2023
c5b54eb
test fixes:
adamsitnik Jun 8, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions eng/Version.Details.xml
Original file line number Diff line number Diff line change
Expand Up @@ -246,13 +246,13 @@
<Sha>4512de66bb6e21c548ab0d5a83242b70969ba576</Sha>
<SourceBuild RepoName="roslyn-analyzers" ManagedOnly="true" />
</Dependency>
<Dependency Name="System.CommandLine" Version="2.0.0-beta4.22564.1">
<Dependency Name="System.CommandLine" Version="2.0.0-beta4.23070.2">
<Uri>https://github.com/dotnet/command-line-api</Uri>
<Sha>8374d5fca634a93458c84414b1604c12f765d1ab</Sha>
<Sha>daec22fb523da51206d3b4b0eb43d85cdff2afa8</Sha>
</Dependency>
<Dependency Name="Microsoft.SourceBuild.Intermediate.command-line-api" Version="0.1.356401">
<Dependency Name="Microsoft.SourceBuild.Intermediate.command-line-api" Version="0.1.407002">
<Uri>https://github.com/dotnet/command-line-api</Uri>
<Sha>8374d5fca634a93458c84414b1604c12f765d1ab</Sha>
<Sha>daec22fb523da51206d3b4b0eb43d85cdff2afa8</Sha>
<SourceBuild RepoName="command-line-api" ManagedOnly="true" />
</Dependency>
<Dependency Name="Microsoft.SourceBuild.Intermediate.source-build-externals" Version="8.0.0-alpha.1.23068.1">
Expand Down
2 changes: 1 addition & 1 deletion eng/Versions.props
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
<SystemTextJsonVersion>6.0.0</SystemTextJsonVersion>
<SystemReflectionMetadataLoadContextVersion>8.0.0-alpha.1.23068.8</SystemReflectionMetadataLoadContextVersion>
<SystemManagementPackageVersion>4.6.0</SystemManagementPackageVersion>
<SystemCommandLineVersion>2.0.0-beta4.22564.1</SystemCommandLineVersion>
<SystemCommandLineVersion>2.0.0-beta4.23070.2</SystemCommandLineVersion>
<MicrosoftDeploymentDotNetReleasesVersion>1.0.0-preview5.1.22263.1</MicrosoftDeploymentDotNetReleasesVersion>
<MicrosoftVisualStudioSetupConfigurationInteropVersion>3.0.4496</MicrosoftVisualStudioSetupConfigurationInteropVersion>
</PropertyGroup>
Expand Down
36 changes: 18 additions & 18 deletions src/ApiCompat/Microsoft.DotNet.ApiCompat.Tool/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -114,14 +114,14 @@ static int Main(string[] args)
rootCommand.AddGlobalOption(excludeAttributesFilesOption);
rootCommand.AddGlobalOption(enableRuleCannotChangeParameterNameOption);

rootCommand.AddOption(leftAssembliesOption);
rootCommand.AddOption(rightAssembliesOption);
rootCommand.AddOption(strictModeOption);
rootCommand.AddOption(leftAssembliesReferencesOption);
rootCommand.AddOption(rightAssembliesReferencesOption);
rootCommand.AddOption(createWorkItemPerAssemblyOption);
rootCommand.AddOption(leftAssembliesTransformationPatternOption);
rootCommand.AddOption(rightAssembliesTransformationPatternOption);
rootCommand.Options.Add(leftAssembliesOption);
rootCommand.Options.Add(rightAssembliesOption);
rootCommand.Options.Add(strictModeOption);
rootCommand.Options.Add(leftAssembliesReferencesOption);
rootCommand.Options.Add(rightAssembliesReferencesOption);
rootCommand.Options.Add(createWorkItemPerAssemblyOption);
rootCommand.Options.Add(leftAssembliesTransformationPatternOption);
rootCommand.Options.Add(rightAssembliesTransformationPatternOption);

rootCommand.SetHandler((InvocationContext context) =>
{
Expand Down Expand Up @@ -212,15 +212,15 @@ static int Main(string[] args)
};

Command packageCommand = new("package", "Validates the compatibility of package assets");
packageCommand.AddArgument(packageArgument);
packageCommand.AddOption(runtimeGraphOption);
packageCommand.AddOption(runApiCompatOption);
packageCommand.AddOption(enableStrictModeForCompatibleTfmsOption);
packageCommand.AddOption(enableStrictModeForCompatibleFrameworksInPackageOption);
packageCommand.AddOption(enableStrictModeForBaselineValidationOption);
packageCommand.AddOption(baselinePackageOption);
packageCommand.AddOption(packageAssemblyReferencesOption);
packageCommand.AddOption(baselinePackageAssemblyReferencesOption);
packageCommand.Arguments.Add(packageArgument);
packageCommand.Options.Add(runtimeGraphOption);
packageCommand.Options.Add(runApiCompatOption);
packageCommand.Options.Add(enableStrictModeForCompatibleTfmsOption);
packageCommand.Options.Add(enableStrictModeForCompatibleFrameworksInPackageOption);
packageCommand.Options.Add(enableStrictModeForBaselineValidationOption);
packageCommand.Options.Add(baselinePackageOption);
packageCommand.Options.Add(packageAssemblyReferencesOption);
packageCommand.Options.Add(baselinePackageAssemblyReferencesOption);
packageCommand.SetHandler((InvocationContext context) =>
{
// If a roslyn assemblies path isn't provided, use the compiled against version from a subfolder.
Expand Down Expand Up @@ -269,7 +269,7 @@ static int Main(string[] args)
roslynResolver.Unregister();
});

rootCommand.AddCommand(packageCommand);
rootCommand.Subcommands.Add(packageCommand);
return rootCommand.Invoke(args);
}

Expand Down
2 changes: 1 addition & 1 deletion src/BuiltInTools/dotnet-watch/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ internal static RootCommand CreateRootCommand(Func<CommandLineOptions, Task<int>
new[] { "--verbose", "-v" },
"Show verbose output");

verbose.AddValidator(v =>
verbose.Validators.Add(v =>
{
if (v.FindResultFor(quiet) is not null && v.FindResultFor(verbose) is not null)
{
Expand Down
20 changes: 19 additions & 1 deletion src/Cli/Microsoft.TemplateEngine.Cli/CliTemplateParameter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.CommandLine;
using System.CommandLine.Help;
using System.CommandLine.Parsing;
using System.Diagnostics;
using System.Globalization;
using System.Text;
using Microsoft.TemplateEngine.Abstractions;
Expand Down Expand Up @@ -150,7 +151,24 @@ internal Option GetOption(IReadOnlySet<string> aliases)
if (!string.IsNullOrWhiteSpace(DefaultValue)
|| (Type == ParameterType.String || Type == ParameterType.Choice) && DefaultValue != null)
{
option.SetDefaultValue(DefaultValue);
switch (option)
Copy link
Member

Choose a reason for hiding this comment

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

the non-generic overload for setting default value was removed, as it was allowing for introducing silent bugs. Example: defining an CliOption<int> and passing bool as default value.

I know that this switch is far from perfect, I am open to suggestions about how it can be improved.

Copy link
Member

Choose a reason for hiding this comment

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

this is probably ok for the template engine - it has a relatively constrained set of types that are allowable as part of the grammar for template options (choice, string, bool, int, etc), and this just becomes an explicit mapping that needs to occur.

@dotnet/templating-engine-maintainers can you take a look and verify that all of our data types are manually mapped here now?

{
case Option<string> stringOption:
stringOption.SetDefaultValue(DefaultValue);
break;
case Option<bool> booleanOption:
booleanOption.SetDefaultValue(bool.Parse(DefaultValue));
break;
case Option<long> integerOption:
integerOption.SetDefaultValue(long.Parse(DefaultValue));
break;
case Option<float> floatOption:
floatOption.SetDefaultValue(float.Parse(DefaultValue));
break;
default:
Debug.Fail($"Unexpected Option type: {option.GetType()}");
break;
}
Copy link
Member

@vlada-shubina vlada-shubina Jan 21, 2023

Choose a reason for hiding this comment

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

imo we should move this code to GetBaseOption and do proper validation there. DefaultValue may be not a valid type and then this method can throw. It's safer to use TryParse. GetBaseOption works with typed Option and then this switch is not needed.

}
}
option.Description = GetOptionDescription();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ protected IReadOnlyDictionary<FilterOptionDefinition, Option> SetupFilterOptions
foreach (FilterOptionDefinition filterDef in filtersToSetup)
{
Option newOption = GetFilterOption(filterDef);
this.AddOption(newOption);
this.Options.Add(newOption);
options[filterDef] = newOption;
}
return options;
Expand All @@ -203,8 +203,8 @@ protected IReadOnlyDictionary<FilterOptionDefinition, Option> SetupFilterOptions
/// </summary>
protected void SetupTabularOutputOptions(ITabularOutputCommand command)
{
this.AddOption(command.ColumnsAllOption);
this.AddOption(command.ColumnsOption);
this.Options.Add(command.ColumnsAllOption);
this.Options.Add(command.ColumnsOption);
}

private static async Task HandleGlobalOptionsAsync(
Expand Down
26 changes: 9 additions & 17 deletions src/Cli/Microsoft.TemplateEngine.Cli/Commands/Extensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

using System.CommandLine;
using System.CommandLine.Parsing;
using System.Diagnostics.CodeAnalysis;
using Microsoft.TemplateEngine.Abstractions;
using Microsoft.TemplateEngine.Abstractions.Constraints;
using Microsoft.TemplateEngine.Edge;
Expand All @@ -25,24 +26,15 @@ internal static class Extensions
/// <summary>
/// Checks if <paramref name="parseResult"/> contains an error for <paramref name="option"/>.
/// </summary>
internal static bool HasErrorFor(this ParseResult parseResult, Option option)
internal static bool HasErrorFor(this ParseResult parseResult, Option option, [NotNullWhen(true)] out ParseError? error)
Copy link
Member

Choose a reason for hiding this comment

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

this is also a perf optimization: previously we were iterating the errors up to three times, now it's only once and the caller does not need to search for the error again

{
if (!parseResult.Errors.Any())
{
return false;
}
error = parseResult.Errors.FirstOrDefault(e => IsOptionResult(e.SymbolResult, option)
|| IsOptionResult(e.SymbolResult?.Parent, option));

if (parseResult.Errors.Any(e => e.SymbolResult?.Symbol == option))
{
return true;
}

if (parseResult.Errors.Any(e => e.SymbolResult?.Parent?.Symbol == option))
{
return true;
}
return error is not null;

return false;
static bool IsOptionResult(SymbolResult? symbolResult, Option option)
=> symbolResult is OptionResult optionResult && optionResult.Option == option;
}

/// <summary>
Expand All @@ -51,8 +43,8 @@ internal static bool HasErrorFor(this ParseResult parseResult, Option option)
internal static void FromAmongCaseInsensitive(this Option<string> option, string[]? allowedValues = null, string? allowedHiddenValue = null)
{
allowedValues ??= Array.Empty<string>();
option.AddValidator(optionResult => ValidateAllowedValues(optionResult, allowedValues, allowedHiddenValue));
option.AddCompletions(allowedValues);
option.Validators.Add(optionResult => ValidateAllowedValues(optionResult, allowedValues, allowedHiddenValue));
option.CompletionSources.Add(allowedValues);
}

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@ internal void AddNoLegacyUsageValidators(Command command, params Symbol[] except
{
if (!except.Contains(option))
{
command.AddValidator(symbolResult => ValidateOptionUsage(symbolResult, option));
command.Validators.Add(symbolResult => ValidateOptionUsage(symbolResult, option));
}
}

foreach (Argument argument in argumentsToVerify)
{
if (!except.Contains(argument))
{
command.AddValidator(symbolResult => ValidateArgumentUsage(symbolResult, argument));
command.Validators.Add(symbolResult => ValidateArgumentUsage(symbolResult, argument));
}
}
}
Expand All @@ -79,7 +79,12 @@ internal void ValidateArgumentsAreNotUsed(CommandResult commandResult)

internal void ValidateOptionUsage(CommandResult commandResult, Option option)
{
OptionResult? optionResult = commandResult.Parent?.Children.FirstOrDefault(symbol => symbol.Symbol == option) as OptionResult;
if (commandResult.Parent is not CommandResult parentResult)
Copy link
Member

Choose a reason for hiding this comment

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

A parent of CommandResult can be only another CommandResult. In case it's null it means it's RootCommand.

{
return;
}

OptionResult? optionResult = parentResult.Children.OfType<OptionResult>().FirstOrDefault(result => result.Option == option);
if (optionResult != null)
{
List<string> wrongTokens = new List<string>();
Expand All @@ -101,10 +106,15 @@ internal void ValidateOptionUsage(CommandResult commandResult, Option option)

private static void ValidateArgumentUsage(CommandResult commandResult, params Argument[] arguments)
{
if (commandResult.Parent is not CommandResult parentResult)
{
return;
}

List<string> wrongTokens = new List<string>();
foreach (Argument argument in arguments)
{
var newCommandArgument = commandResult.Parent?.Children.FirstOrDefault(symbol => symbol.Symbol == argument) as ArgumentResult;
var newCommandArgument = parentResult.Children.OfType<ArgumentResult>().FirstOrDefault(result => result.Argument == argument);
if (newCommandArgument == null)
{
continue;
Expand All @@ -126,22 +136,22 @@ private static void ValidateArgumentUsage(CommandResult commandResult, params Ar

private void BuildLegacySymbols(Func<ParseResult, ITemplateEngineHost> hostBuilder)
{
this.AddArgument(ShortNameArgument);
this.AddArgument(RemainingArguments);
this.Arguments.Add(ShortNameArgument);
this.Arguments.Add(RemainingArguments);

//legacy options
Dictionary<FilterOptionDefinition, Option> options = new Dictionary<FilterOptionDefinition, Option>();
foreach (var filterDef in LegacyFilterDefinitions)
{
options[filterDef] = filterDef.OptionFactory().AsHidden();
this.AddOption(options[filterDef]);
this.Options.Add(options[filterDef]);
}
LegacyFilters = options;

this.AddOption(InteractiveOption);
this.AddOption(AddSourceOption);
this.AddOption(ColumnsAllOption);
this.AddOption(ColumnsOption);
this.Options.Add(InteractiveOption);
this.Options.Add(AddSourceOption);
this.Options.Add(ColumnsAllOption);
this.Options.Add(ColumnsOption);

this.TreatUnmatchedTokensAsErrors = true;

Expand Down
12 changes: 6 additions & 6 deletions src/Cli/Microsoft.TemplateEngine.Cli/Commands/NewCommand.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ internal NewCommand(
this.AddGlobalOption(DebugRebuildCacheOption);
this.AddGlobalOption(DebugShowConfigOption);

this.AddOption(SharedOptions.OutputOption);
this.AddOption(SharedOptions.NameOption);
this.AddOption(SharedOptions.DryRunOption);
this.AddOption(SharedOptions.ForceOption);
this.AddOption(SharedOptions.NoUpdateCheckOption);
this.AddOption(SharedOptions.ProjectPathOption);
this.Options.Add(SharedOptions.OutputOption);
this.Options.Add(SharedOptions.NameOption);
this.Options.Add(SharedOptions.DryRunOption);
this.Options.Add(SharedOptions.ForceOption);
this.Options.Add(SharedOptions.NoUpdateCheckOption);
this.Options.Add(SharedOptions.ProjectPathOption);
}

internal static Option<string?> DebugCustomSettingsLocationOption { get; } = new("--debug:custom-hive")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,23 @@ internal InstantiateCommand(
Func<ParseResult, ITemplateEngineHost> hostBuilder)
: base(hostBuilder, "create", SymbolStrings.Command_Instantiate_Description)
{
this.AddArgument(ShortNameArgument);
this.AddArgument(RemainingArguments);
this.Arguments.Add(ShortNameArgument);
this.Arguments.Add(RemainingArguments);

this.AddOption(SharedOptions.OutputOption);
this.AddOption(SharedOptions.NameOption);
this.AddOption(SharedOptions.DryRunOption);
this.AddOption(SharedOptions.ForceOption);
this.AddOption(SharedOptions.NoUpdateCheckOption);
this.AddOption(SharedOptions.ProjectPathOption);
this.Options.Add(SharedOptions.OutputOption);
this.Options.Add(SharedOptions.NameOption);
this.Options.Add(SharedOptions.DryRunOption);
this.Options.Add(SharedOptions.ForceOption);
this.Options.Add(SharedOptions.NoUpdateCheckOption);
this.Options.Add(SharedOptions.ProjectPathOption);

parentCommand.AddNoLegacyUsageValidators(this);
this.AddValidator(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.OutputOption));
this.AddValidator(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.NameOption));
this.AddValidator(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.DryRunOption));
this.AddValidator(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.ForceOption));
this.AddValidator(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.NoUpdateCheckOption));
this.AddValidator(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.ProjectPathOption));
this.Validators.Add(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.OutputOption));
this.Validators.Add(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.NameOption));
this.Validators.Add(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.DryRunOption));
this.Validators.Add(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.ForceOption));
this.Validators.Add(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.NoUpdateCheckOption));
this.Validators.Add(symbolResult => parentCommand.ValidateOptionUsage(symbolResult, SharedOptions.ProjectPathOption));
}

internal static Argument<string> ShortNameArgument { get; } = new Argument<string>("template-short-name")
Expand Down Expand Up @@ -272,7 +272,7 @@ private static async Task<NewCommandStatus> HandleTemplateInstantationAsync(
if (candidates.Count == 1)
{
TemplateCommand templateCommandToRun = candidates.Single();
args.Command.AddCommand(templateCommandToRun);
args.Command.Subcommands.Add(templateCommandToRun);

ParseResult updatedParseResult = args.ParseResult.Parser.Parse(args.ParseResult.Tokens.Select(t => t.Value).ToList());
return await candidates.Single().InvokeAsync(updatedParseResult, cancellationToken).ConfigureAwait(false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,8 @@ public bool Equals(InvalidTemplateOptionResult? other)
return this.Equals(other as object);
}

internal static new InvalidTemplateOptionResult FromParseResult(TemplateOption option, ParseResult parseResult)
internal static InvalidTemplateOptionResult FromParseError(TemplateOption option, ParseResult parseResult, ParseError error)
Copy link
Member

Choose a reason for hiding this comment

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

perf: we don't need to search for the error if the caller can provide it

{
if (!parseResult.HasErrorFor(option.Option))
{
throw new ArgumentException($"{nameof(option)} does not have an error in {nameof(parseResult)}");
}

OptionResult? optionResult = parseResult.FindResultFor(option.Option);
if (optionResult == null)
{
Expand All @@ -103,23 +98,7 @@ public bool Equals(InvalidTemplateOptionResult? other)
optionValue = string.Join(", ", optionResult.Tokens.Select(t => t.Value));
}

string? errorMessage = null;
if (!string.IsNullOrWhiteSpace(optionResult.ErrorMessage))
{
errorMessage = optionResult.ErrorMessage;
}
else
{
foreach (var result in optionResult.Children)
{
if (string.IsNullOrWhiteSpace(result.ErrorMessage))
{
continue;
}
errorMessage = result.ErrorMessage;
break;
}
}
string errorMessage = !string.IsNullOrWhiteSpace(optionResult.ErrorMessage) ? optionResult.ErrorMessage : error.Message;

return new InvalidTemplateOptionResult(
option,
Expand Down
Loading