Skip to content

Commit d216b53

Browse files
authored
Update System.CommandLine to latest version (#1265)
Update `System.CommandLine` to the latest version. There have been some changes to the API since the last version around binding a command to a handler, and the removal of the model binding. We now explicitly specify the options when setting the handlers. This means we should be able to avoid the use of reflection inside of the package, making it trimable in the future!
2 parents c8bec66 + 3119578 commit d216b53

File tree

14 files changed

+171
-272
lines changed

14 files changed

+171
-272
lines changed

src/shared/Atlassian.Bitbucket/UI/Commands/CredentialsCommand.cs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,23 +15,19 @@ public abstract class CredentialsCommand : HelperCommand
1515
protected CredentialsCommand(ICommandContext context)
1616
: base(context, "prompt", "Show authentication prompt.")
1717
{
18-
AddOption(
19-
new Option<string>("--url", "Bitbucket Server or Data Center URL")
20-
);
18+
var url = new Option<Uri>("--url", "Bitbucket Server or Data Center URL");
19+
AddOption(url);
2120

22-
AddOption(
23-
new Option<string>("--username", "Username or email.")
24-
);
21+
var userName = new Option<string>("--username", "Username or email.");
22+
AddOption(userName);
2523

26-
AddOption(
27-
new Option("--show-oauth", "Show OAuth option.")
28-
);
24+
var oauth = new Option<bool>("--show-oauth", "Show OAuth option.");
25+
AddOption(oauth);
2926

30-
AddOption(
31-
new Option("--show-basic", "Show username/password option.")
32-
);
27+
var basic = new Option<bool>("--show-basic", "Show username/password option.");
28+
AddOption(basic);
3329

34-
Handler = CommandHandler.Create<Uri, string, bool, bool>(ExecuteAsync);
30+
this.SetHandler(ExecuteAsync, url, userName, oauth, basic);
3531
}
3632

3733
private async Task<int> ExecuteAsync(Uri url, string userName, bool showOAuth, bool showBasic)

src/shared/Core/Commands/ConfigurationCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.CommandLine;
2-
using System.CommandLine.Invocation;
32
using System.Threading.Tasks;
43

54
namespace GitCredentialManager.Commands
@@ -15,9 +14,10 @@ protected ConfigurationCommandBase(ICommandContext context, string name, string
1514
Context = context;
1615
ConfigurationService = configurationService;
1716

18-
AddOption(new Option<bool>("--system", "Modify the system-wide Git configuration instead of the current user"));
17+
var system = new Option<bool>("--system", "Modify the system-wide Git configuration instead of the current user");
18+
AddOption(system);
1919

20-
Handler = CommandHandler.Create<bool>(ExecuteAsync);
20+
this.SetHandler(ExecuteAsync, system);
2121
}
2222

2323
protected ICommandContext Context { get; }

src/shared/Core/Commands/DiagnoseCommand.cs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
using System;
22
using System.Collections.Generic;
33
using System.CommandLine;
4-
using System.CommandLine.Invocation;
54
using System.IO;
6-
using System.Reflection;
75
using System.Text;
86
using System.Threading.Tasks;
97
using GitCredentialManager.Diagnostics;
@@ -34,11 +32,10 @@ public DiagnoseCommand(ICommandContext context)
3432
new MicrosoftAuthenticationDiagnostic(context)
3533
};
3634

37-
AddOption(
38-
new Option<string>(new []{"--output", "-o"}, "Output directory for diagnostic logs.")
39-
);
35+
var output = new Option<string>(new[] { "--output", "-o" }, "Output directory for diagnostic logs.");
36+
AddOption(output);
4037

41-
Handler = CommandHandler.Create<string>(ExecuteAsync);
38+
this.SetHandler(ExecuteAsync, output);
4239
}
4340

4441
public void AddDiagnostic(IDiagnostic diagnostic)

src/shared/Core/Commands/GitCommandBase.cs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.CommandLine;
4-
using System.CommandLine.Invocation;
54
using System.Threading.Tasks;
65

76
namespace GitCredentialManager.Commands
@@ -23,7 +22,7 @@ protected GitCommandBase(ICommandContext context, string name, string descriptio
2322
Context = context;
2423
_hostProviderRegistry = hostProviderRegistry;
2524

26-
Handler = CommandHandler.Create(ExecuteAsync);
25+
this.SetHandler(ExecuteAsync);
2726
}
2827

2928
protected ICommandContext Context { get; }

src/shared/Core/Core.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@
2424
<ItemGroup>
2525
<PackageReference Include="Microsoft.Identity.Client" Version="4.52.0" />
2626
<PackageReference Include="Microsoft.Identity.Client.Extensions.Msal" Version="2.28.0" />
27-
<PackageReference Include="System.CommandLine" Version="2.0.0-beta1.21216.1" />
27+
<PackageReference Include="System.CommandLine" Version="2.0.0-beta4.22272.1" />
2828
<PackageReference Include="Avalonia" Version="11.0.0-preview6" />
2929
<PackageReference Include="Avalonia.Skia" Version="11.0.0-preview6" />
3030
<PackageReference Include="Avalonia.Themes.Fluent" Version="11.0.0-preview6" />

src/shared/Core/UI/Commands/CredentialsCommand.cs

Lines changed: 17 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
using System;
21
using System.Collections.Generic;
32
using System.CommandLine;
4-
using System.CommandLine.Invocation;
53
using System.Threading;
64
using System.Threading.Tasks;
75
using GitCredentialManager.UI.ViewModels;
@@ -13,52 +11,40 @@ public abstract class CredentialsCommand : HelperCommand
1311
protected CredentialsCommand(ICommandContext context)
1412
: base(context, "basic", "Show basic authentication prompt.")
1513
{
16-
AddOption(
17-
new Option<string>("--title", "Window title (optional).")
18-
);
14+
var title = new Option<string>("--title", "Window title (optional).");
15+
AddOption(title);
1916

20-
AddOption(
21-
new Option<string>("--resource", "Resource name or URL (optional).")
22-
);
17+
var resource = new Option<string>("--resource", "Resource name or URL (optional).");
18+
AddOption(resource);
2319

24-
AddOption(
25-
new Option<string>("--username", "User name (optional).")
26-
);
20+
var userName = new Option<string>("--username", "User name (optional).");
21+
AddOption(userName);
2722

28-
AddOption(
29-
new Option("--no-logo", "Hide the Git Credential Manager logo and logotype.")
30-
);
23+
var noLogo = new Option<bool>("--no-logo", "Hide the Git Credential Manager logo and logotype.");
24+
AddOption(noLogo);
3125

32-
Handler = CommandHandler.Create<CommandOptions>(ExecuteAsync);
33-
}
34-
35-
private class CommandOptions
36-
{
37-
public string Title { get; set; }
38-
public string Resource { get; set; }
39-
public string UserName { get; set; }
40-
public bool NoLogo { get; set; }
26+
this.SetHandler(ExecuteAsync, title, resource, userName, noLogo);
4127
}
4228

43-
private async Task<int> ExecuteAsync(CommandOptions options)
29+
private async Task<int> ExecuteAsync(string title, string resource, string userName, bool noLogo)
4430
{
4531
var viewModel = new CredentialsViewModel();
4632

47-
if (!string.IsNullOrWhiteSpace(options.Title))
33+
if (!string.IsNullOrWhiteSpace(title))
4834
{
49-
viewModel.Title = options.Title;
35+
viewModel.Title = title;
5036
}
5137

52-
viewModel.Description = !string.IsNullOrWhiteSpace(options.Resource)
53-
? $"Enter your credentials for '{options.Resource}'"
38+
viewModel.Description = !string.IsNullOrWhiteSpace(resource)
39+
? $"Enter your credentials for '{resource}'"
5440
: "Enter your credentials";
5541

56-
if (!string.IsNullOrWhiteSpace(options.UserName))
42+
if (!string.IsNullOrWhiteSpace(userName))
5743
{
58-
viewModel.UserName = options.UserName;
44+
viewModel.UserName = userName;
5945
}
6046

61-
viewModel.ShowProductHeader = !options.NoLogo;
47+
viewModel.ShowProductHeader = !noLogo;
6248

6349
await ShowAsync(viewModel, CancellationToken.None);
6450

src/shared/Core/UI/Commands/DefaultAccountCommand.cs

Lines changed: 15 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
using System.Collections.Generic;
22
using System.CommandLine;
3-
using System.CommandLine.Invocation;
43
using System.Threading;
54
using System.Threading.Tasks;
65
using GitCredentialManager.UI.ViewModels;
@@ -12,40 +11,30 @@ public abstract class DefaultAccountCommand : HelperCommand
1211
protected DefaultAccountCommand(ICommandContext context)
1312
: base(context, "default-account", "Show prompt to confirm use of the default OS account.")
1413
{
15-
AddOption(
16-
new Option<string>("--title", "Window title (optional).")
17-
);
18-
19-
AddOption(
20-
new Option<string>("--username", "User name to display.")
21-
{
22-
IsRequired = true
23-
}
24-
);
14+
var title = new Option<string>("--title", "Window title (optional).");
15+
AddOption(title);
2516

26-
AddOption(
27-
new Option("--no-logo", "Hide the Git Credential Manager logo and logotype.")
28-
);
17+
var userName = new Option<string>("--username", "User name to display.")
18+
{
19+
IsRequired = true
20+
};
21+
AddOption(userName);
2922

30-
Handler = CommandHandler.Create(ExecuteAsync);
31-
}
23+
var noLogo = new Option<bool>("--no-logo", "Hide the Git Credential Manager logo and logotype.");
24+
AddOption(noLogo);
3225

33-
private class CommandOptions
34-
{
35-
public string Title { get; set; }
36-
public string UserName { get; set; }
37-
public bool NoLogo { get; set; }
26+
this.SetHandler(ExecuteAsync, title, userName, noLogo);
3827
}
3928

40-
private async Task<int> ExecuteAsync(CommandOptions options)
29+
private async Task<int> ExecuteAsync(string title, string userName, bool noLogo)
4130
{
4231
var viewModel = new DefaultAccountViewModel(Context.Environment)
4332
{
44-
Title = !string.IsNullOrWhiteSpace(options.Title)
45-
? options.Title
33+
Title = !string.IsNullOrWhiteSpace(title)
34+
? title
4635
: "Git Credential Manager",
47-
UserName = options.UserName,
48-
ShowProductHeader = !options.NoLogo
36+
UserName = userName,
37+
ShowProductHeader = !noLogo
4938
};
5039

5140
await ShowAsync(viewModel, CancellationToken.None);

src/shared/Core/UI/Commands/DeviceCodeCommand.cs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
using System;
21
using System.CommandLine;
3-
using System.CommandLine.Invocation;
42
using System.Threading;
53
using System.Threading.Tasks;
64
using GitCredentialManager.UI.ViewModels;
@@ -12,19 +10,16 @@ public abstract class DeviceCodeCommand : HelperCommand
1210
protected DeviceCodeCommand(ICommandContext context)
1311
: base(context, "device", "Show device code prompt.")
1412
{
15-
AddOption(
16-
new Option<string>("--code", "User code.")
17-
);
13+
var code = new Option<string>("--code", "User code.");
14+
AddOption(code);
1815

19-
AddOption(
20-
new Option<string>("--url", "Verification URL.")
21-
);
16+
var url =new Option<string>("--url", "Verification URL.");
17+
AddOption(url);
2218

23-
AddOption(
24-
new Option("--no-logo", "Hide the Git Credential Manager logo and logotype.")
25-
);
19+
var noLogo = new Option<bool>("--no-logo", "Hide the Git Credential Manager logo and logotype.");
20+
AddOption(noLogo);
2621

27-
Handler = CommandHandler.Create<string, string, bool>(ExecuteAsync);
22+
this.SetHandler(ExecuteAsync, code, url, noLogo);
2823
}
2924

3025
private async Task<int> ExecuteAsync(string code, string url, bool noLogo)

src/shared/Core/UI/Commands/OAuthCommand.cs

Lines changed: 19 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
using System;
22
using System.Collections.Generic;
33
using System.CommandLine;
4-
using System.CommandLine.Invocation;
54
using System.Threading;
65
using System.Threading.Tasks;
76
using GitCredentialManager.Authentication;
@@ -14,54 +13,40 @@ public abstract class OAuthCommand : HelperCommand
1413
protected OAuthCommand(ICommandContext context)
1514
: base(context, "oauth", "Show OAuth authentication prompt.")
1615
{
17-
AddOption(
18-
new Option<string>("--title", "Window title (optional).")
19-
);
16+
var title = new Option<string>("--title", "Window title (optional).");
17+
AddOption(title);
2018

21-
AddOption(
22-
new Option<string>("--resource", "Resource name or URL (optional).")
23-
);
19+
var resource = new Option<string>("--resource", "Resource name or URL (optional).");
20+
AddOption(resource);
2421

25-
AddOption(
26-
new Option("--browser", "Show browser authentication option.")
27-
);
22+
var browser = new Option<bool>("--browser", "Show browser authentication option.");
23+
AddOption(browser);
2824

29-
AddOption(
30-
new Option("--device-code", "Show device code authentication option.")
31-
);
25+
var deviceCode = new Option<bool>("--device-code", "Show device code authentication option.");
26+
AddOption(deviceCode);
3227

33-
AddOption(
34-
new Option("--no-logo", "Hide the Git Credential Manager logo and logotype.")
35-
);
28+
var noLogo = new Option<bool>("--no-logo", "Hide the Git Credential Manager logo and logotype.");
29+
AddOption(noLogo);
3630

37-
Handler = CommandHandler.Create<CommandOptions>(ExecuteAsync);
31+
this.SetHandler(ExecuteAsync, title, resource, browser, deviceCode, noLogo);
3832
}
3933

40-
private class CommandOptions
41-
{
42-
public string Title { get; set; }
43-
public string Resource { get; set; }
44-
public bool Browser { get; set; }
45-
public bool DeviceCode { get; set; }
46-
public bool NoLogo { get; set; }
47-
}
48-
49-
private async Task<int> ExecuteAsync(CommandOptions options)
34+
private async Task<int> ExecuteAsync(string title, string resource, bool browser, bool deviceCode, bool noLogo)
5035
{
5136
var viewModel = new OAuthViewModel();
5237

53-
if (!string.IsNullOrWhiteSpace(options.Title))
38+
if (!string.IsNullOrWhiteSpace(title))
5439
{
55-
viewModel.Title = options.Title;
40+
viewModel.Title = title;
5641
}
5742

58-
viewModel.Description = !string.IsNullOrWhiteSpace(options.Resource)
59-
? $"Sign in to '{options.Resource}'"
43+
viewModel.Description = !string.IsNullOrWhiteSpace(resource)
44+
? $"Sign in to '{resource}'"
6045
: "Select a sign-in option";
6146

62-
viewModel.ShowBrowserLogin = options.Browser;
63-
viewModel.ShowDeviceCodeLogin = options.DeviceCode;
64-
viewModel.ShowProductHeader = !options.NoLogo;
47+
viewModel.ShowBrowserLogin = browser;
48+
viewModel.ShowDeviceCodeLogin = deviceCode;
49+
viewModel.ShowProductHeader = !noLogo;
6550

6651
await ShowAsync(viewModel, CancellationToken.None);
6752

0 commit comments

Comments
 (0)