Skip to content

Commit 342cc15

Browse files
authored
update System.CommandLine to the latest version (#6340)
1 parent 48614cc commit 342cc15

File tree

11 files changed

+88
-86
lines changed

11 files changed

+88
-86
lines changed

eng/Version.Details.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,9 @@
1919
<Uri>https://github.com/dotnet/runtime</Uri>
2020
<Sha>eaa9717d90115cea43b4cdd7a2a49e6d3c3d780e</Sha>
2121
</Dependency>
22-
<Dependency Name="System.CommandLine" Version="2.0.0-beta4.23165.3">
22+
<Dependency Name="System.CommandLine" Version="2.0.0-beta4.23307.1">
2323
<Uri>https://github.com/dotnet/command-line-api</Uri>
24-
<Sha>42c58533cdf478b15120542be76f7cbaacaab024</Sha>
24+
<Sha>02fe27cd6a9b001c8feb7938e6ef4b3799745759</Sha>
2525
</Dependency>
2626
</ProductDependencies>
2727
<ToolsetDependencies>

eng/Versions.props

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
<PropertyGroup>
1717
<!-- Maestro-managed Package Versions - Ordered by repo name -->
1818
<!-- Dependencies from https://github.com/dotnet/command-line-api -->
19-
<SystemCommandLinePackageVersion>2.0.0-beta4.23165.3</SystemCommandLinePackageVersion>
19+
<SystemCommandLinePackageVersion>2.0.0-beta4.23307.1</SystemCommandLinePackageVersion>
2020
<!-- Dependencies from https://github.com/dotnet/runtime -->
2121
<MicrosoftNETCoreAppRefPackageVersion>8.0.0-preview.7.23325.2</MicrosoftNETCoreAppRefPackageVersion>
2222
<MicrosoftNETCoreAppRuntimewinx64PackageVersion>8.0.0-preview.7.23325.2</MicrosoftNETCoreAppRuntimewinx64PackageVersion>

test/Microsoft.TemplateEngine.Authoring.CLI.UnitTests/ValidateCommandTests.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public class ValidateCommandTests : TestBase
1212
[Fact]
1313
public async Task ValidateCommand_BasicTest_InvalidTemplate()
1414
{
15-
RootCommand root = new()
15+
CliRootCommand root = new()
1616
{
1717
new ValidateCommand()
1818
};
@@ -26,7 +26,7 @@ public async Task ValidateCommand_BasicTest_InvalidTemplate()
2626
[Fact]
2727
public async Task ValidateCommand_BasicTest_ValidTemplate()
2828
{
29-
RootCommand root = new()
29+
CliRootCommand root = new()
3030
{
3131
new ValidateCommand()
3232
};

tools/Microsoft.TemplateEngine.Authoring.CLI/Commands/ExecutableCommand.cs

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -2,36 +2,22 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33

44
using System.CommandLine;
5-
using System.CommandLine.Invocation;
65
using Microsoft.Extensions.Logging;
76
using Microsoft.Extensions.Logging.Console;
87

98
namespace Microsoft.TemplateEngine.Authoring.CLI.Commands
109
{
1110
/// <summary>
12-
/// Represents a <see cref="Command"/> together with its handler.
11+
/// Represents a <see cref="CliCommand"/> together with its action.
1312
/// </summary>
14-
internal abstract class ExecutableCommand<TModel> : Command, ICommandHandler where TModel : class
13+
internal abstract class ExecutableCommand<TModel> : CliCommand where TModel : class
1514
{
1615
internal ExecutableCommand(string name, string? description = null)
1716
: base(name, description)
1817
{
19-
Handler = this;
18+
Action = new CommandAction(this);
2019
}
2120

22-
/// <inheritdoc/>
23-
public async Task<int> InvokeAsync(InvocationContext context, CancellationToken cancellationToken = default)
24-
{
25-
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole(c => c.ColorBehavior = LoggerColorBehavior.Disabled));
26-
TModel arguments = ParseContext(context.ParseResult);
27-
28-
//exceptions are handled by parser itself
29-
return await ExecuteAsync(arguments, loggerFactory, cancellationToken).ConfigureAwait(false);
30-
}
31-
32-
/// <inheritdoc/>
33-
public int Invoke(InvocationContext context) => InvokeAsync(context).GetAwaiter().GetResult();
34-
3521
/// <summary>
3622
/// Parses the context from <see cref="ParseResult"/>.
3723
/// </summary>
@@ -42,5 +28,22 @@ public async Task<int> InvokeAsync(InvocationContext context, CancellationToken
4228
/// </summary>
4329
protected abstract Task<int> ExecuteAsync(TModel args, ILoggerFactory loggerFactory, CancellationToken cancellationToken);
4430

31+
private sealed class CommandAction : CliAction
32+
{
33+
private readonly ExecutableCommand<TModel> _command;
34+
35+
public CommandAction(ExecutableCommand<TModel> command) => _command = command;
36+
37+
public override int Invoke(ParseResult parseResult) => InvokeAsync(parseResult).GetAwaiter().GetResult();
38+
39+
public override async Task<int> InvokeAsync(ParseResult parseResult, CancellationToken cancellationToken = default)
40+
{
41+
using ILoggerFactory loggerFactory = LoggerFactory.Create(builder => builder.AddSimpleConsole(c => c.ColorBehavior = LoggerColorBehavior.Disabled));
42+
TModel arguments = _command.ParseContext(parseResult);
43+
44+
//exceptions are handled by parser itself
45+
return await _command.ExecuteAsync(arguments, loggerFactory, cancellationToken).ConfigureAwait(false);
46+
}
47+
}
4548
}
4649
}

tools/Microsoft.TemplateEngine.Authoring.CLI/Commands/Verify/VerifyCommand.cs

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,72 +12,72 @@ internal class VerifyCommand : ExecutableCommand<VerifyCommandArgs>
1212
{
1313
private const string CommandName = "verify";
1414

15-
private readonly Argument<string> _templateNameArgument = new("template-short-name")
15+
private readonly CliArgument<string> _templateNameArgument = new("template-short-name")
1616
{
1717
Description = LocalizableStrings.command_verify_help_templateName_description,
1818
// 0 for case where only path is specified
1919
Arity = new ArgumentArity(1, 1)
2020
};
2121

22-
private readonly Option<string> _remainingArguments = new Option<string>("template-args", new[] { "--template-args" })
22+
private readonly CliOption<string> _remainingArguments = new("--template-args")
2323
{
2424
Description = "Template specific arguments - all joined into single enquoted string. Any needed quotations of actual arguments has to be escaped.",
2525
Arity = new ArgumentArity(0, 1)
2626
};
2727

28-
private readonly Option<string> _templatePathOption = new("template-path", new[] { "-p", "--template-path" })
28+
private readonly CliOption<string> _templatePathOption = new("--template-path", "-p")
2929
{
3030
Description = LocalizableStrings.command_verify_help_templatePath_description,
3131
};
3232

33-
private readonly Option<string> _templateOutputPathOption = new("output", new[] { "-o", "--output" })
33+
private readonly CliOption<string> _templateOutputPathOption = new("--output", "-o")
3434
{
3535
Description = LocalizableStrings.command_verify_help_outputPath_description,
3636
};
3737

38-
private readonly Option<string> _snapshotsDirectoryOption = new("snapshots-directory", new[] { "-d", "--snapshots-directory" })
38+
private readonly CliOption<string> _snapshotsDirectoryOption = new("--snapshots-directory", "-d")
3939
{
4040
Description = LocalizableStrings.command_verify_help_snapshotsDirPath_description,
4141
};
4242

43-
private readonly Option<string> _scenarioNameOption = new("scenario-name", new[] { "--scenario-name" })
43+
private readonly CliOption<string> _scenarioNameOption = new("--scenario-name")
4444
{
4545
Description = LocalizableStrings.command_verify_help_scenarioName_description,
4646
};
4747

48-
private readonly Option<bool> _disableDiffToolOption = new("disable-diff-tool", new[] { "--disable-diff-tool" })
48+
private readonly CliOption<bool> _disableDiffToolOption = new("--disable-diff-tool")
4949
{
5050
Description = LocalizableStrings.command_verify_help_disableDiffTool_description,
5151
};
5252

53-
private readonly Option<bool> _disableDefaultExcludePatternsOption = new("disable-default-exclude-patterns", new[] { "--disable-default-exclude-patterns" })
53+
private readonly CliOption<bool> _disableDefaultExcludePatternsOption = new("--disable-default-exclude-patterns")
5454
{
5555
Description = LocalizableStrings.command_verify_help_disableDefaultExcludes_description,
5656
};
5757

58-
private readonly Option<IEnumerable<string>> _excludePatternOption = new("exclude-pattern", new[] { "--exclude-pattern" })
58+
private readonly CliOption<IEnumerable<string>> _excludePatternOption = new("--exclude-pattern")
5959
{
6060
Description = LocalizableStrings.command_verify_help_customExcludes_description,
6161
Arity = new ArgumentArity(0, 999)
6262
};
6363

64-
private readonly Option<IEnumerable<string>> _includePatternOption = new("include-pattern", new[] { "--include-pattern" })
64+
private readonly CliOption<IEnumerable<string>> _includePatternOption = new("--include-pattern")
6565
{
6666
Description = LocalizableStrings.command_verify_help_customIncludes_description,
6767
Arity = new ArgumentArity(0, 999)
6868
};
6969

70-
private readonly Option<bool> _verifyCommandOutputOption = new("verify-std", new[] { "--verify-std" })
70+
private readonly CliOption<bool> _verifyCommandOutputOption = new("--verify-std")
7171
{
7272
Description = LocalizableStrings.command_verify_help_verifyOutputs_description,
7373
};
7474

75-
private readonly Option<bool> _isCommandExpectedToFailOption = new("fail-expected", new[] { "--fail-expected" })
75+
private readonly CliOption<bool> _isCommandExpectedToFailOption = new("--fail-expected")
7676
{
7777
Description = LocalizableStrings.command_verify_help_expectFailure_description,
7878
};
7979

80-
private readonly Option<IEnumerable<UniqueForOption>> _uniqueForOption = new("unique-for", new[] { "--unique-for" })
80+
private readonly CliOption<IEnumerable<UniqueForOption>> _uniqueForOption = new("--unique-for")
8181
{
8282
Description = LocalizableStrings.command_verify_help_uniqueFor_description,
8383
Arity = new ArgumentArity(0, 999),
@@ -171,9 +171,9 @@ await engine.Execute(
171171
}
172172

173173
/// <summary>
174-
/// Case insensitive version for <see cref="Option{T}.AcceptOnlyFromAmong(string[])"/>.
174+
/// Case insensitive version for <see cref="CliOption{T}.AcceptOnlyFromAmong(string[])"/>.
175175
/// </summary>
176-
private static void FromAmongCaseInsensitive(Option<IEnumerable<UniqueForOption>> option, string[]? allowedValues = null, string? allowedHiddenValue = null)
176+
private static void FromAmongCaseInsensitive(CliOption<IEnumerable<UniqueForOption>> option, string[]? allowedValues = null, string? allowedHiddenValue = null)
177177
{
178178
allowedValues ??= Array.Empty<string>();
179179
option.Validators.Add(optionResult => ValidateAllowedValues(optionResult, allowedValues, allowedHiddenValue));

tools/Microsoft.TemplateEngine.Authoring.CLI/Commands/localize/LocalizeCommand.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55

66
namespace Microsoft.TemplateEngine.Authoring.CLI.Commands
77
{
8-
internal class LocalizeCommand : Command
8+
internal class LocalizeCommand : CliCommand
99
{
1010
internal LocalizeCommand()
1111
: base("localize")
1212
{
13-
this.Subcommands.Add(new ExportCommand());
13+
Subcommands.Add(new ExportCommand());
1414
}
1515
}
1616
}

tools/Microsoft.TemplateEngine.Authoring.CLI/Commands/localize/export/ExportCommand.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,25 +11,25 @@ internal sealed class ExportCommand : ExecutableCommand<ExportCommandArgs>
1111
{
1212
private const string CommandName = "export";
1313

14-
private readonly Argument<IEnumerable<string>> _templatePathArgument = new("template-path")
14+
private readonly CliArgument<IEnumerable<string>> _templatePathArgument = new("template-path")
1515
{
1616
Arity = ArgumentArity.OneOrMore,
1717
Description = LocalizableStrings.command_export_help_templatePath_description,
1818
};
1919

20-
private readonly Option<IEnumerable<string>> _languageOption = new("language", new[] { "--language", "-l" })
20+
private readonly CliOption<IEnumerable<string>> _languageOption = new("--language", "-l")
2121
{
2222
Description = LocalizableStrings.command_export_help_language_description,
2323
Arity = ArgumentArity.OneOrMore,
2424
AllowMultipleArgumentsPerToken = true,
2525
};
2626

27-
private readonly Option<bool> _recursiveOption = new("recursive", new[] { "--recursive", "-r" })
27+
private readonly CliOption<bool> _recursiveOption = new("recursive", new[] { "--recursive", "-r" })
2828
{
2929
Description = LocalizableStrings.command_export_help_recursive_description,
3030
};
3131

32-
private readonly Option<bool> _dryRunOption = new("dry-run", new[] { "--dry-run", "-d" })
32+
private readonly CliOption<bool> _dryRunOption = new("--dry-run", "-d")
3333
{
3434
Description = LocalizableStrings.command_export_help_dryrun_description,
3535
};

tools/Microsoft.TemplateEngine.Authoring.CLI/Commands/validate/ValidateCommand.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ internal class ValidateCommand : ExecutableCommand<ValidateCommandArgs>
1414
{
1515
private const string CommandName = "validate";
1616

17-
private readonly Argument<string> _templateLocationArg = new("template-location")
17+
private readonly CliArgument<string> _templateLocationArg = new("template-location")
1818
{
1919
Description = LocalizableStrings.command_validate_help_description,
2020
Arity = new ArgumentArity(1, 1)

tools/Microsoft.TemplateEngine.Authoring.CLI/Program.cs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,20 +11,20 @@ internal sealed class Program
1111
{
1212
internal static Task<int> Main(string[] args)
1313
{
14-
RootCommand rootCommand = new("dotnet-template-authoring");
14+
CliRootCommand rootCommand = new("dotnet-template-authoring");
1515
rootCommand.Subcommands.Add(new LocalizeCommand());
1616
rootCommand.Subcommands.Add(new VerifyCommand());
1717
rootCommand.Subcommands.Add(new ValidateCommand());
1818

1919
return GetCommandLineConfiguration(rootCommand).InvokeAsync(args);
2020
}
2121

22-
internal static CommandLineConfiguration GetCommandLineConfiguration(Command command)
22+
internal static CliConfiguration GetCommandLineConfiguration(CliCommand command)
2323
{
24-
CommandLineBuilder builder = new CommandLineBuilder(command)
25-
.UseDefaults()
26-
.EnablePosixBundling(false);
27-
return builder.Build();
24+
return new CliConfiguration(command)
25+
{
26+
EnablePosixBundling = false
27+
};
2828
}
2929
}
3030
}

tools/Microsoft.TemplateSearch.TemplateDiscovery/Program.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ internal class Program
99
{
1010
private static async Task<int> Main(string[] args)
1111
{
12-
Command rootCommand = new TemplateDiscoveryCommand();
13-
return await rootCommand.InvokeAsync(args).ConfigureAwait(false);
12+
CliCommand rootCommand = new TemplateDiscoveryCommand();
13+
return await rootCommand.Parse(args).InvokeAsync().ConfigureAwait(false);
1414
}
1515
}
1616
}

0 commit comments

Comments
 (0)