Skip to content

Commit a1497ff

Browse files
committed
push version and info command activation into the Option definitions
1 parent 1414d33 commit a1497ff

File tree

1 file changed

+38
-21
lines changed

1 file changed

+38
-21
lines changed

src/Cli/dotnet/Commands/Workload/WorkloadCommandParser.cs

Lines changed: 38 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -21,23 +21,25 @@
2121
using Microsoft.TemplateEngine.Cli.Commands;
2222
using IReporter = Microsoft.DotNet.Cli.Utils.IReporter;
2323
using Command = System.CommandLine.Command;
24+
using System.CommandLine.Invocation;
2425

2526
namespace Microsoft.DotNet.Cli.Commands.Workload;
2627

2728
internal static class WorkloadCommandParser
2829
{
2930
public static readonly string DocsLink = "https://aka.ms/dotnet-workload";
30-
3131
public static readonly Option<bool> InfoOption = new("--info")
3232
{
3333
Description = CliCommandStrings.WorkloadInfoDescription,
34-
Arity = ArgumentArity.Zero
34+
Arity = ArgumentArity.Zero,
35+
Action = new ShowWorkloadsInfoAction()
3536
};
3637

3738
public static readonly Option<bool> VersionOption = new("--version")
3839
{
3940
Description = CliCommandStrings.WorkloadVersionDescription,
40-
Arity = ArgumentArity.Zero
41+
Arity = ArgumentArity.Zero,
42+
Action = new ShowWorkloadsVersionOption()
4143
};
4244

4345
public static Command GetCommand()
@@ -78,11 +80,11 @@ void WriteUpdateModeAndAnyError(string indent = "")
7880
reporter.WriteLine(indent + CliCommandStrings.ShouldInstallAWorkloadSet);
7981
}
8082
}
81-
83+
8284
if (showVersion)
8385
{
8486
reporter.WriteLine($" Workload version: {GetWorkloadsVersion()}");
85-
87+
8688
WriteUpdateModeAndAnyError(indent: " ");
8789
reporter.WriteLine();
8890
}
@@ -134,22 +136,7 @@ void WriteUpdateModeAndAnyError(string indent = "")
134136
}
135137
}
136138

137-
private static int ProcessArgs(ParseResult parseResult)
138-
{
139-
if (parseResult.HasOption(InfoOption) && parseResult.RootSubCommandResult() == "workload")
140-
{
141-
ShowWorkloadsInfo(parseResult);
142-
Reporter.Output.WriteLine(string.Empty);
143-
return 0;
144-
}
145-
else if (parseResult.HasOption(VersionOption) && parseResult.RootSubCommandResult() == "workload")
146-
{
147-
Reporter.Output.WriteLine(GetWorkloadsVersion());
148-
Reporter.Output.WriteLine(string.Empty);
149-
return 0;
150-
}
151-
return parseResult.HandleMissingCommand();
152-
}
139+
private static int ProcessArgs(ParseResult parseResult) => parseResult.HandleMissingCommand();
153140

154141
private static Command ConstructCommand()
155142
{
@@ -182,4 +169,34 @@ private static Command ConstructCommand()
182169

183170
return command;
184171
}
172+
173+
private class ShowWorkloadsInfoAction : SynchronousCommandLineAction
174+
{
175+
public ShowWorkloadsInfoAction()
176+
{
177+
Terminating = true;
178+
}
179+
180+
public override int Invoke(ParseResult parseResult)
181+
{
182+
ShowWorkloadsInfo(parseResult);
183+
Reporter.Output.WriteLine(string.Empty);
184+
return 0;
185+
}
186+
}
187+
188+
private class ShowWorkloadsVersionOption : SynchronousCommandLineAction
189+
{
190+
public ShowWorkloadsVersionOption()
191+
{
192+
Terminating = true;
193+
}
194+
195+
public override int Invoke(ParseResult parseResult)
196+
{
197+
Reporter.Output.WriteLine(GetWorkloadsVersion());
198+
Reporter.Output.WriteLine(string.Empty);
199+
return 0;
200+
}
201+
}
185202
}

0 commit comments

Comments
 (0)