Skip to content

Commit

Permalink
Ensure help parsing is shared by all commands
Browse files Browse the repository at this point in the history
Fixes #67
  • Loading branch information
kzu committed Jan 4, 2021
1 parent 964f92f commit d3b416a
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
8 changes: 8 additions & 0 deletions src/VisualStudio.Tests/CommandFactoryTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ public async Task when_creating_command_with_help_argument_then_throws_show_usag
await Assert.ThrowsAsync<ShowUsageException>(async () => await commandFactory.CreateCommandAsync("test", ImmutableArray.Create("/h")));
}

[Fact]
public async Task when_creating_builtin_command_with_help_argument_then_throws_show_usage()
{
var commandFactory = new CommandFactory();

await Assert.ThrowsAsync<ShowUsageException>(async () => await commandFactory.CreateCommandAsync("modify", ImmutableArray.Create("-?")));
}

[Theory]
[InlineData(Commands.Install, typeof(InstallCommand))]
[InlineData(Commands.Run, typeof(RunCommand))]
Expand Down
8 changes: 7 additions & 1 deletion src/VisualStudio/CommandFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,14 @@ public Task<Command> CreateCommandAsync(string command, ImmutableArray<string> a
// Create the descriptor
var commandDescriptor = factory.CreateDescriptor();

// Parse out help arg up-front to avoid unnecessary extra parsing for args.
var help = new HelpOption();
var finalArgs = help.Parse(args);
if (help.Value)
throw new ShowUsageException(commandDescriptor);

// Parse the arguments
commandDescriptor.Parse(args);
commandDescriptor.Parse(finalArgs);

// And create the command
return Task.FromResult(factory.CreateCommand(commandDescriptor));
Expand Down

0 comments on commit d3b416a

Please sign in to comment.