Skip to content

Commit a5fe497

Browse files
committed
Update System.CommandLine usage to match latest version's API surface.
1 parent 3d7e841 commit a5fe497

File tree

5 files changed

+70
-127
lines changed

5 files changed

+70
-127
lines changed

Directory.Packages.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,7 @@
101101
<PackageVersion Include="StyleCop.Analyzers" Version="$(StyleCopAnalyzersPackageVersion)" />
102102
<PackageVersion Include="System.CodeDom" Version="$(SystemCodeDomPackageVersion)" />
103103
<PackageVersion Include="System.CommandLine" Version="$(SystemCommandLineVersion)" />
104+
<PackageVersion Include="System.CommandLine.NamingConventionBinder" Version="$(SystemCommandLineNamingConventionBinderVersion)" />
104105
<PackageVersion Include="System.Composition.AttributedModel" Version="$(SystemCompositionAttributedModelPackageVersion)" />
105106
<PackageVersion Include="System.Composition.Convention" Version="$(SystemCompositionConventionPackageVersion)" />
106107
<PackageVersion Include="System.Composition.Hosting" Version="$(SystemCompositionHostingPackageVersion)" />

eng/Versions.props

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@
6363
<StyleCopAnalyzersPackageVersion>1.2.0-beta.435</StyleCopAnalyzersPackageVersion>
6464
<WebDeploymentPackageVersion>4.0.5</WebDeploymentPackageVersion>
6565
<SystemCommandLineVersion>2.0.0-beta5.25223.119</SystemCommandLineVersion>
66+
<SystemCommandLineNamingConventionBinderVersion>2.0.0-beta5.25223.119</SystemCommandLineNamingConventionBinderVersion>
6667
<MicrosoftCodeAnalysisAnalyzerTestingVersion>1.1.2-beta1.22216.1</MicrosoftCodeAnalysisAnalyzerTestingVersion>
6768
<MicrosoftVisualBasicVersion>10.3.0</MicrosoftVisualBasicVersion>
6869
<MicrosoftVisualStudioSetupConfigurationInteropVersion>3.2.2146</MicrosoftVisualStudioSetupConfigurationInteropVersion>

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff.Tool/GenAPIDiffConfigurationBinder.cs

Lines changed: 0 additions & 71 deletions
This file was deleted.

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff.Tool/Microsoft.DotNet.ApiDiff.Tool.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818

1919
<ItemGroup>
2020
<PackageReference Include="System.CommandLine" />
21+
<PackageReference Include="System.CommandLine.NamingConventionBinder" />
2122
</ItemGroup>
2223

2324
</Project>

src/Compatibility/ApiDiff/Microsoft.DotNet.ApiDiff.Tool/Program.cs

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

44
using System.CommandLine;
5+
using System.ComponentModel;
56
using System.Diagnostics;
67
using Microsoft.DotNet.ApiSymbolExtensions.Logging;
78

@@ -17,124 +18,134 @@ public static async Task Main(string[] args)
1718
{
1819
RootCommand rootCommand = new("genapidiff");
1920

20-
Option<string> optionBeforeAssembliesFolderPath = new(["--before", "-b"])
21+
Option<string> optionBeforeAssembliesFolderPath = new(name: "", aliases: ["--before", "-b"])
2122
{
2223
Description = "The path to the folder containing the old (before) assemblies to be included in the diff.",
2324
Arity = ArgumentArity.ExactlyOne,
24-
IsRequired = true
25+
Required = true
2526
};
2627

27-
Option<string> optionBeforeRefAssembliesFolderPath = new(["--refbefore", "-rb"])
28+
Option<string> optionBeforeRefAssembliesFolderPath = new(name: "", aliases: ["--refbefore", "-rb"])
2829
{
2930
Description = "The path to the folder containing the references required by old (before) assemblies, not to be included in the diff.",
3031
Arity = ArgumentArity.ExactlyOne,
31-
IsRequired = false
32+
Required = false
3233
};
3334

34-
Option<string> optionAfterAssembliesFolderPath = new(["--after", "-a"])
35+
Option<string> optionAfterAssembliesFolderPath = new(name: "", aliases: ["--after", "-a"])
3536
{
3637
Description = "The path to the folder containing the new (after) assemblies to be included in the diff.",
3738
Arity = ArgumentArity.ExactlyOne,
38-
IsRequired = true
39+
Required = true
3940
};
4041

41-
Option<string> optionAfterRefAssembliesFolderPath = new(["--refafter", "-ra"])
42+
Option<string> optionAfterRefAssembliesFolderPath = new(name: "", aliases: ["--refafter", "-ra"])
4243
{
4344
Description = "The path to the folder containing references required by the new (after) reference assemblies, not to be included in the diff.",
4445
Arity = ArgumentArity.ExactlyOne,
45-
IsRequired = false
46+
Required = false
4647
};
4748

48-
Option<string> optionOutputFolderPath = new(["--output", "-o"])
49+
Option<string> optionOutputFolderPath = new(name: "", aliases: ["--output", "-o"])
4950
{
5051
Description = "The path to the output folder.",
5152
Arity = ArgumentArity.ExactlyOne,
52-
IsRequired = true
53+
Required = true
5354
};
5455

55-
Option<string> optionBeforeFriendlyName = new(["--beforeFriendlyName", "-bfn"])
56+
Option<string> optionBeforeFriendlyName = new(name: "", aliases: ["--beforeFriendlyName", "-bfn"])
5657
{
5758
Description = "The friendly name to describe the 'before' assembly.",
5859
Arity = ArgumentArity.ExactlyOne,
59-
IsRequired = true
60+
Required = true
6061
};
6162

62-
Option<string> optionAfterFriendlyName = new(["--afterFriendlyName", "-afn"])
63+
Option<string> optionAfterFriendlyName = new(name: "", aliases: ["--afterFriendlyName", "-afn"])
6364
{
6465
Description = "The friendly name to describe the 'after' assembly.",
6566
Arity = ArgumentArity.ExactlyOne,
66-
IsRequired = true
67+
Required = true
6768
};
6869

69-
Option<string> optionTableOfContentsTitle = new(["--tableOfContentsTitle", "-tc"], () => "api_diff")
70+
Option<string> optionTableOfContentsTitle = new(name: "", aliases: ["--tableOfContentsTitle", "-tc"])
7071
{
7172
Description = $"The optional title of the markdown table of contents file that is placed in the output folder.",
7273
Arity = ArgumentArity.ZeroOrMore,
73-
IsRequired = true
74+
Required = true,
75+
DefaultValueFactory = _ => "api_diff"
7476
};
7577

76-
Option<FileInfo[]?> optionFilesWithAssembliesToExclude = new(["--assembliesToExclude", "-eas"], () => null)
78+
Option<FileInfo[]?> optionFilesWithAssembliesToExclude = new(name: "", aliases: ["--assembliesToExclude", "-eas"])
7779
{
7880
Description = "An optional array of filepaths, each containing a list of assemblies that should be excluded from the diff. Each file should contain one assembly name per line, with no extensions.",
7981
Arity = ArgumentArity.ZeroOrMore,
80-
IsRequired = false,
82+
Required = false,
83+
DefaultValueFactory = _ => null
8184
};
8285

83-
Option<FileInfo[]?> optionFilesWithAttributesToExclude = new(["--attributesToExclude", "-eattrs"], () => null)
86+
Option<FileInfo[]?> optionFilesWithAttributesToExclude = new(name: "", aliases: ["--attributesToExclude", "-eattrs"])
8487
{
8588
Description = "An optional array of filepaths, each containing a list of attributes to exclude from the diff. Each file should contain one API full name per line.",
8689
Arity = ArgumentArity.ZeroOrMore,
87-
IsRequired = false
90+
Required = false,
91+
DefaultValueFactory = _ => null
8892
};
8993

90-
Option<FileInfo[]?> optionFilesWithApisToExclude = new(["--apisToExclude", "-eapis"], () => null)
94+
Option<FileInfo[]?> optionFilesWithApisToExclude = new(name: "", aliases: ["--apisToExclude", "-eapis"])
9195
{
9296
Description = "An optional array of filepaths, each containing a list of APIs to exclude from the diff. Each file should contain one API full name per line.",
9397
Arity = ArgumentArity.ZeroOrMore,
94-
IsRequired = false
98+
Required = false,
99+
DefaultValueFactory = _ => null
95100
};
96101

97-
Option<bool> optionAddPartialModifier = new(["--addPartialModifier", "-apm"], () => false)
102+
Option<bool> optionAddPartialModifier = new(name: "", aliases: ["--addPartialModifier", "-apm"])
98103
{
99-
Description = "Add the 'partial' modifier to types."
104+
Description = "Add the 'partial' modifier to types.",
105+
DefaultValueFactory = _ => false
100106
};
101107

102-
Option<bool> optionAttachDebugger = new(["--attachDebugger", "-d"], () => false)
108+
Option<bool> optionAttachDebugger = new(name: "", aliases: ["--attachDebugger", "-d"])
103109
{
104-
Description = "Stops the tool at startup, prints the process ID and waits for a debugger to attach."
110+
Description = "Stops the tool at startup, prints the process ID and waits for a debugger to attach.",
111+
DefaultValueFactory = _ => false
105112
};
106113

107114
// Custom ordering for the help menu.
108-
rootCommand.Add(optionBeforeAssembliesFolderPath);
109-
rootCommand.Add(optionBeforeRefAssembliesFolderPath);
110-
rootCommand.Add(optionAfterAssembliesFolderPath);
111-
rootCommand.Add(optionAfterRefAssembliesFolderPath);
112-
rootCommand.Add(optionOutputFolderPath);
113-
rootCommand.Add(optionBeforeFriendlyName);
114-
rootCommand.Add(optionAfterFriendlyName);
115-
rootCommand.Add(optionTableOfContentsTitle);
116-
rootCommand.Add(optionFilesWithAssembliesToExclude);
117-
rootCommand.Add(optionFilesWithAttributesToExclude);
118-
rootCommand.Add(optionFilesWithApisToExclude);
119-
rootCommand.Add(optionAddPartialModifier);
120-
rootCommand.Add(optionAttachDebugger);
121-
122-
GenAPIDiffConfigurationBinder c = new(optionBeforeAssembliesFolderPath,
123-
optionBeforeRefAssembliesFolderPath,
124-
optionAfterAssembliesFolderPath,
125-
optionAfterRefAssembliesFolderPath,
126-
optionOutputFolderPath,
127-
optionBeforeFriendlyName,
128-
optionAfterFriendlyName,
129-
optionTableOfContentsTitle,
130-
optionFilesWithAssembliesToExclude,
131-
optionFilesWithAttributesToExclude,
132-
optionFilesWithApisToExclude,
133-
optionAddPartialModifier,
134-
optionAttachDebugger);
135-
136-
rootCommand.SetHandler(async (DiffConfiguration diffConfig) => await HandleCommandAsync(diffConfig).ConfigureAwait(false), c);
137-
await rootCommand.InvokeAsync(args);
115+
rootCommand.Options.Add(optionBeforeAssembliesFolderPath);
116+
rootCommand.Options.Add(optionBeforeRefAssembliesFolderPath);
117+
rootCommand.Options.Add(optionAfterAssembliesFolderPath);
118+
rootCommand.Options.Add(optionAfterRefAssembliesFolderPath);
119+
rootCommand.Options.Add(optionOutputFolderPath);
120+
rootCommand.Options.Add(optionBeforeFriendlyName);
121+
rootCommand.Options.Add(optionAfterFriendlyName);
122+
rootCommand.Options.Add(optionTableOfContentsTitle);
123+
rootCommand.Options.Add(optionFilesWithAssembliesToExclude);
124+
rootCommand.Options.Add(optionFilesWithAttributesToExclude);
125+
rootCommand.Options.Add(optionFilesWithApisToExclude);
126+
rootCommand.Options.Add(optionAddPartialModifier);
127+
rootCommand.Options.Add(optionAttachDebugger);
128+
129+
rootCommand.SetAction(async (ParseResult result) =>
130+
{
131+
DiffConfiguration c = new(
132+
BeforeAssembliesFolderPath: result.GetValue(optionBeforeAssembliesFolderPath) ?? throw new NullReferenceException("Null before assemblies directory"),
133+
BeforeAssemblyReferencesFolderPath: result.GetValue(optionBeforeRefAssembliesFolderPath),
134+
AfterAssembliesFolderPath: result.GetValue(optionAfterAssembliesFolderPath) ?? throw new NullReferenceException("Null after assemblies directory"),
135+
AfterAssemblyReferencesFolderPath: result.GetValue(optionAfterRefAssembliesFolderPath),
136+
OutputFolderPath: result.GetValue(optionOutputFolderPath) ?? throw new NullReferenceException("Null output directory"),
137+
BeforeFriendlyName: result.GetValue(optionBeforeFriendlyName) ?? throw new NullReferenceException("Null before friendly name"),
138+
AfterFriendlyName: result.GetValue(optionAfterFriendlyName) ?? throw new NullReferenceException("Null after friendly name"),
139+
TableOfContentsTitle: result.GetValue(optionTableOfContentsTitle) ?? throw new NullReferenceException("Null table of contents title"),
140+
FilesWithAssembliesToExclude: result.GetValue(optionFilesWithAssembliesToExclude),
141+
FilesWithAttributesToExclude: result.GetValue(optionFilesWithAttributesToExclude),
142+
FilesWithApisToExclude: result.GetValue(optionFilesWithApisToExclude),
143+
AddPartialModifier: result.GetValue(optionAddPartialModifier),
144+
AttachDebugger: result.GetValue(optionAttachDebugger)
145+
);
146+
await HandleCommandAsync(c).ConfigureAwait(false);
147+
});
148+
await rootCommand.Parse(args).InvokeAsync();
138149
}
139150

140151
private static Task HandleCommandAsync(DiffConfiguration diffConfig)

0 commit comments

Comments
 (0)