Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Command line argument for solution/project as positional argument #681

Merged
merged 15 commits into from May 22, 2020
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
23 changes: 18 additions & 5 deletions src/Program.cs
@@ -1,8 +1,10 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using System.IO;
using System.Linq;
using System.Threading;
Expand All @@ -24,9 +26,22 @@ internal class Program
internal const int UnableToLocateMSBuildExitCode = 3;

private static async Task<int> Main(string[] args)
{
var rootCommand = CreateCommandLineOptions();

// Parse the incoming args and invoke the handler
return await rootCommand.InvokeAsync(args);
}

public static RootCommand CreateCommandLineOptions()
MaStr11 marked this conversation as resolved.
Show resolved Hide resolved
{
var rootCommand = new RootCommand
{
new Argument<string>("project")
MaStr11 marked this conversation as resolved.
Show resolved Hide resolved
{
Arity = ArgumentArity.ZeroOrOne,
Description = Resources.The_solution_or_project_file_to_operate_on_If_a_file_is_not_specified_the_command_will_search_the_current_directory_for_one
MaStr11 marked this conversation as resolved.
Show resolved Hide resolved
},
new Option(new[] { "--folder", "-f" }, Resources.The_folder_to_operate_on_Cannot_be_used_with_the_workspace_option)
{
Argument = new Argument<string?>(() => null)
Expand Down Expand Up @@ -64,12 +79,10 @@ private static async Task<int> Main(string[] args)

rootCommand.Description = "dotnet-format";
rootCommand.Handler = CommandHandler.Create(typeof(Program).GetMethod(nameof(Run)));

// Parse the incoming args and invoke the handler
return await rootCommand.InvokeAsync(args);
return rootCommand;
}

public static async Task<int> Run(string? folder, string? workspace, string? verbosity, bool check, string[] include, string[] exclude, string? report, bool includeGenerated, IConsole console = null!)
public static async Task<int> Run(string? project, string? folder, string? workspace, string? verbosity, bool check, string[] include, string[] exclude, string? report, bool includeGenerated, IConsole console = null!)
{
// Setup logging.
var serviceCollection = new ServiceCollection();
Expand All @@ -96,7 +109,7 @@ public static async Task<int> Run(string? folder, string? workspace, string? ver
string workspaceDirectory;
string workspacePath;
WorkspaceType workspaceType;

workspace ??= project;
MaStr11 marked this conversation as resolved.
Show resolved Hide resolved
if (!string.IsNullOrEmpty(folder) && !string.IsNullOrEmpty(workspace))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This check is now done in the validator (ValidateWorkspaceAndFolder). Should I remove it here?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please do. =)

{
logger.LogWarning(Resources.Cannot_specify_both_folder_and_workspace_options);
Expand Down
106 changes: 105 additions & 1 deletion tests/ProgramTests.cs
@@ -1,12 +1,19 @@
// Copyright (c) Microsoft. All Rights Reserved. Licensed under the MIT license. See License.txt in the project root for license information.

using System.IO;
using System.Collections.Generic;
using System.CommandLine;
using System.CommandLine.Invocation;
using System.CommandLine.Parsing;
using Xunit;

namespace Microsoft.CodeAnalysis.Tools.Tests
{
public class ProgramTests
{
// Should be kept in sync with Program.Run
//
private delegate void TestCommandHandlerDelegate(string project, string folder, string workspace, string verbosity, bool check, string[] include, string[] exclude, string report, bool includeGenerated);

[Fact]
public void ExitCodeIsOneWithCheckAndAnyFilesFormatted()
{
Expand All @@ -33,5 +40,102 @@ public void ExitCodeIsSameWithoutCheck()

Assert.Equal(formatResult.ExitCode, exitCode);
}

[Fact]
public void CommandLine_OptionsAreParsedCorrectly()
{
// Arrange
var sut = Program.CreateCommandLineOptions();

// Act
var result = sut.Parse(new[] {
"--folder", "folder",
"--workspace", "workspace",
"--include", "include1", "include2",
"--exclude", "exclude1", "exclude2",
"--check",
"--report", "report",
"--verbosity", "verbosity",
"--include-generated"});

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.Equal(0, result.UnmatchedTokens.Count);
Assert.Equal(0, result.UnparsedTokens.Count);
Assert.Equal("folder", result.ValueForOption("folder"));
Assert.Equal("workspace", result.ValueForOption("workspace"));
Assert.Collection(result.ValueForOption<IEnumerable<string>>("include"), i0 => Assert.Equal("include1", i0), i1 => Assert.Equal("include2", i1));
Assert.Collection(result.ValueForOption<IEnumerable<string>>("exclude"), i0 => Assert.Equal("exclude1", i0), i1 => Assert.Equal("exclude2", i1));
Assert.True(result.ValueForOption<bool>("check"));
Assert.Equal("report", result.ValueForOption("report"));
Assert.Equal("verbosity", result.ValueForOption("verbosity"));
Assert.True(result.ValueForOption<bool>("include-generated"));
}

[Fact]
public void CommandLine_ProjectArgument_Simple()
{
// Arrange
var sut = Program.CreateCommandLineOptions();

// Act
var result = sut.Parse(new[] { "projectValue" });

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.Equal("projectValue", result.CommandResult.GetArgumentValueOrDefault("project"));
}

[Fact]
public void CommandLine_ProjectArgument_WithOption_AfterArgument()
{
// Arrange
var sut = Program.CreateCommandLineOptions();

// Act
var result = sut.Parse(new[] { "projectValue", "--verbosity", "verbosity" });

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.Equal("projectValue", result.CommandResult.GetArgumentValueOrDefault("project"));
Assert.Equal("verbosity", result.ValueForOption("verbosity"));
}

[Fact]
public void CommandLine_ProjectArgument_WithOption_BeforeArgument()
{
// Arrange
var sut = Program.CreateCommandLineOptions();

// Act
var result = sut.Parse(new[] { "--verbosity", "verbosity", "projectValue" });

// Assert
Assert.Equal(0, result.Errors.Count);
Assert.Equal("projectValue", result.CommandResult.GetArgumentValueOrDefault("project"));
Assert.Equal("verbosity", result.ValueForOption("verbosity"));
}

[Fact]
public void CommandLine_ProjectArgument_GetsPassedToHandler()
{
// Arrange
var sut = Program.CreateCommandLineOptions();
var handlerWasCalled = false;
sut.Handler = CommandHandler.Create(new TestCommandHandlerDelegate(TestCommandHandler));

void TestCommandHandler(string project, string folder, string workspace, string verbosity, bool check, string[] include, string[] exclude, string report, bool includeGenerated)
{
handlerWasCalled = true;
Assert.Equal("projectValue", project);
Assert.Equal("verbosity", verbosity);
};

// Act
var result = sut.Invoke(new[] { "--verbosity", "verbosity", "projectValue" });

// Assert
Assert.True(handlerWasCalled);
}
}
}