Skip to content

Commit d3b416a

Browse files
committed
Ensure help parsing is shared by all commands
Fixes #67
1 parent 964f92f commit d3b416a

File tree

2 files changed

+15
-1
lines changed

2 files changed

+15
-1
lines changed

src/VisualStudio.Tests/CommandFactoryTests.cs

+8
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,14 @@ public async Task when_creating_command_with_help_argument_then_throws_show_usag
2929
await Assert.ThrowsAsync<ShowUsageException>(async () => await commandFactory.CreateCommandAsync("test", ImmutableArray.Create("/h")));
3030
}
3131

32+
[Fact]
33+
public async Task when_creating_builtin_command_with_help_argument_then_throws_show_usage()
34+
{
35+
var commandFactory = new CommandFactory();
36+
37+
await Assert.ThrowsAsync<ShowUsageException>(async () => await commandFactory.CreateCommandAsync("modify", ImmutableArray.Create("-?")));
38+
}
39+
3240
[Theory]
3341
[InlineData(Commands.Install, typeof(InstallCommand))]
3442
[InlineData(Commands.Run, typeof(RunCommand))]

src/VisualStudio/CommandFactory.cs

+7-1
Original file line numberDiff line numberDiff line change
@@ -115,8 +115,14 @@ public Task<Command> CreateCommandAsync(string command, ImmutableArray<string> a
115115
// Create the descriptor
116116
var commandDescriptor = factory.CreateDescriptor();
117117

118+
// Parse out help arg up-front to avoid unnecessary extra parsing for args.
119+
var help = new HelpOption();
120+
var finalArgs = help.Parse(args);
121+
if (help.Value)
122+
throw new ShowUsageException(commandDescriptor);
123+
118124
// Parse the arguments
119-
commandDescriptor.Parse(args);
125+
commandDescriptor.Parse(finalArgs);
120126

121127
// And create the command
122128
return Task.FromResult(factory.CreateCommand(commandDescriptor));

0 commit comments

Comments
 (0)