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 all 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
72 changes: 64 additions & 8 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.Diagnostics.CodeAnalysis;
using System.IO;
using System.Linq;
Expand All @@ -26,9 +28,22 @@ internal class Program
internal const int UnableToLocateDotNetCliExitCode = 4;

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

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

internal static RootCommand CreateCommandLineOptions()
{
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 @@ -65,13 +80,51 @@ private static async Task<int> Main(string[] args)
};

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

// Parse the incoming args and invoke the handler
return await rootCommand.InvokeAsync(args);
static string? ValidateProjectArgumentAndWorkspace(CommandResult symbolResult)
{
try
{
var project = symbolResult.GetArgumentValueOrDefault<string>("project");
var workspace = symbolResult.ValueForOption<string>("workspace");
if (!string.IsNullOrEmpty(project) && !string.IsNullOrEmpty(workspace))
{
return Resources.Cannot_specify_both_project_argument_and_workspace_option;
}
}
catch (InvalidOperationException) // Parsing of arguments failed. This will be reported later.
{
}

return null;
}

static string? ValidateWorkspaceAndFolder(CommandResult symbolResult)
{
try
{
var project = symbolResult.GetArgumentValueOrDefault<string>("project");
var workspace = symbolResult.ValueForOption<string>("workspace");
var folder = symbolResult.ValueForOption<string>("folder");
project ??= workspace;
if (!string.IsNullOrEmpty(project) && !string.IsNullOrEmpty(folder))
{
return Resources.Cannot_specify_both_folder_and_workspace_options;
}
}
catch (InvalidOperationException)// Parsing of arguments failed. This will be reported later.
{
}

return null;
}
}

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 @@ -98,13 +151,13 @@ public static async Task<int> Run(string? folder, string? workspace, string? ver
string workspaceDirectory;
string workspacePath;
WorkspaceType workspaceType;

if (!string.IsNullOrEmpty(folder) && !string.IsNullOrEmpty(workspace))
if (!string.IsNullOrEmpty(workspace) && string.IsNullOrEmpty(project))
{
logger.LogWarning(Resources.Cannot_specify_both_folder_and_workspace_options);
return 1;
logger.LogWarning(Resources.Workspace_option_is_deprecated_Use_the_project_argument_instead);
}
else if (!string.IsNullOrEmpty(workspace))

workspace ??= project;
if (!string.IsNullOrEmpty(workspace))
{
var (isSolution, workspaceFilePath) = MSBuildWorkspaceFinder.FindWorkspace(currentDirectory, workspace);

Expand All @@ -118,7 +171,10 @@ public static async Task<int> Run(string? folder, string? workspace, string? ver
// a global.json if present.
var directoryName = Path.GetDirectoryName(workspacePath);
if (directoryName is null)
{
throw new Exception($"Unable to find folder at '{workspacePath}'");
}

workspaceDirectory = directoryName;
}
else
Expand Down
60 changes: 33 additions & 27 deletions src/Resources.resx
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="utf-8"?>
<root>
<!--
Microsoft ResX Schema

<!--
Microsoft ResX Schema
Version 2.0

The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
The primary goals of this format is to allow a simple XML format
that is mostly human readable. The generation and parsing of the
various data types are done through the TypeConverter classes
associated with the data types.

Example:

... ado.net/XML headers & schema ...
<resheader name="resmimetype">text/microsoft-resx</resheader>
<resheader name="version">2.0</resheader>
Expand All @@ -26,36 +26,36 @@
<value>[base64 mime encoded string representing a byte array form of the .NET Framework object]</value>
<comment>This is a comment</comment>
</data>

There are any number of "resheader" rows that contain simple
There are any number of "resheader" rows that contain simple
name/value pairs.

Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
Each data row contains a name, and value. The row also contains a
type or mimetype. Type corresponds to a .NET class that support
text/value conversion through the TypeConverter architecture.
Classes that don't support this are serialized and stored with the
mimetype set.

The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
The mimetype is used for serialized objects, and tells the
ResXResourceReader how to depersist the object. This is currently not
extensible. For a given mimetype the value must be set accordingly:

Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
Note - application/x-microsoft.net.object.binary.base64 is the format
that the ResXResourceWriter will generate, however the reader can
read any of the formats listed below.

mimetype: application/x-microsoft.net.object.binary.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.soap.base64
value : The object must be serialized with
value : The object must be serialized with
: System.Runtime.Serialization.Formatters.Soap.SoapFormatter
: and then encoded with base64 encoding.

mimetype: application/x-microsoft.net.object.bytearray.base64
value : The object must be serialized into a byte array
value : The object must be serialized into a byte array
: using a System.ComponentModel.TypeConverter
: and then encoded with base64 encoding.
-->
Expand Down Expand Up @@ -234,4 +234,10 @@
<data name="The_dotnet_CLI_version_is_0" xml:space="preserve">
<value>The dotnet CLI version is '{0}'.</value>
</data>
<data name="Cannot_specify_both_project_argument_and_workspace_option" xml:space="preserve">
<value>Cannot specify both project argument and workspace option.</value>
</data>
<data name="Workspace_option_is_deprecated_Use_the_project_argument_instead" xml:space="preserve">
<value>"workspace" option is deprecated. Use the project argument instead.</value>
</data>
</root>
10 changes: 10 additions & 0 deletions src/xlf/Resources.cs.xlf
Expand Up @@ -27,6 +27,11 @@
<target state="new">Cannot specify both folder and workspace options.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_both_project_argument_and_workspace_option">
<source>Cannot specify both project argument and workspace option.</source>
<target state="new">Cannot specify both project argument and workspace option.</target>
<note />
</trans-unit>
<trans-unit id="Complete_in_0_ms">
<source>Complete in {0}ms.</source>
<target state="new">Complete in {0}ms.</target>
Expand Down Expand Up @@ -192,6 +197,11 @@
<target state="new">Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.</target>
<note />
</trans-unit>
<trans-unit id="Workspace_option_is_deprecated_Use_the_project_argument_instead">
<source>"workspace" option is deprecated. Use the project argument instead.</source>
<target state="new">"workspace" option is deprecated. Use the project argument instead.</target>
<note />
</trans-unit>
<trans-unit id="Writing_formatting_report_to_0">
<source>Writing formatting report to: '{0}'.</source>
<target state="new">Writing formatting report to: '{0}'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.de.xlf
Expand Up @@ -27,6 +27,11 @@
<target state="new">Cannot specify both folder and workspace options.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_both_project_argument_and_workspace_option">
<source>Cannot specify both project argument and workspace option.</source>
<target state="new">Cannot specify both project argument and workspace option.</target>
<note />
</trans-unit>
<trans-unit id="Complete_in_0_ms">
<source>Complete in {0}ms.</source>
<target state="new">Complete in {0}ms.</target>
Expand Down Expand Up @@ -192,6 +197,11 @@
<target state="new">Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.</target>
<note />
</trans-unit>
<trans-unit id="Workspace_option_is_deprecated_Use_the_project_argument_instead">
<source>"workspace" option is deprecated. Use the project argument instead.</source>
<target state="new">"workspace" option is deprecated. Use the project argument instead.</target>
<note />
</trans-unit>
<trans-unit id="Writing_formatting_report_to_0">
<source>Writing formatting report to: '{0}'.</source>
<target state="new">Writing formatting report to: '{0}'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.es.xlf
Expand Up @@ -27,6 +27,11 @@
<target state="new">Cannot specify both folder and workspace options.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_both_project_argument_and_workspace_option">
<source>Cannot specify both project argument and workspace option.</source>
<target state="new">Cannot specify both project argument and workspace option.</target>
<note />
</trans-unit>
<trans-unit id="Complete_in_0_ms">
<source>Complete in {0}ms.</source>
<target state="new">Complete in {0}ms.</target>
Expand Down Expand Up @@ -192,6 +197,11 @@
<target state="new">Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.</target>
<note />
</trans-unit>
<trans-unit id="Workspace_option_is_deprecated_Use_the_project_argument_instead">
<source>"workspace" option is deprecated. Use the project argument instead.</source>
<target state="new">"workspace" option is deprecated. Use the project argument instead.</target>
<note />
</trans-unit>
<trans-unit id="Writing_formatting_report_to_0">
<source>Writing formatting report to: '{0}'.</source>
<target state="new">Writing formatting report to: '{0}'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.fr.xlf
Expand Up @@ -27,6 +27,11 @@
<target state="new">Cannot specify both folder and workspace options.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_both_project_argument_and_workspace_option">
<source>Cannot specify both project argument and workspace option.</source>
<target state="new">Cannot specify both project argument and workspace option.</target>
<note />
</trans-unit>
<trans-unit id="Complete_in_0_ms">
<source>Complete in {0}ms.</source>
<target state="new">Complete in {0}ms.</target>
Expand Down Expand Up @@ -192,6 +197,11 @@
<target state="new">Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.</target>
<note />
</trans-unit>
<trans-unit id="Workspace_option_is_deprecated_Use_the_project_argument_instead">
<source>"workspace" option is deprecated. Use the project argument instead.</source>
<target state="new">"workspace" option is deprecated. Use the project argument instead.</target>
<note />
</trans-unit>
<trans-unit id="Writing_formatting_report_to_0">
<source>Writing formatting report to: '{0}'.</source>
<target state="new">Writing formatting report to: '{0}'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.it.xlf
Expand Up @@ -27,6 +27,11 @@
<target state="new">Cannot specify both folder and workspace options.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_both_project_argument_and_workspace_option">
<source>Cannot specify both project argument and workspace option.</source>
<target state="new">Cannot specify both project argument and workspace option.</target>
<note />
</trans-unit>
<trans-unit id="Complete_in_0_ms">
<source>Complete in {0}ms.</source>
<target state="new">Complete in {0}ms.</target>
Expand Down Expand Up @@ -192,6 +197,11 @@
<target state="new">Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.</target>
<note />
</trans-unit>
<trans-unit id="Workspace_option_is_deprecated_Use_the_project_argument_instead">
<source>"workspace" option is deprecated. Use the project argument instead.</source>
<target state="new">"workspace" option is deprecated. Use the project argument instead.</target>
<note />
</trans-unit>
<trans-unit id="Writing_formatting_report_to_0">
<source>Writing formatting report to: '{0}'.</source>
<target state="new">Writing formatting report to: '{0}'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.ja.xlf
Expand Up @@ -27,6 +27,11 @@
<target state="new">Cannot specify both folder and workspace options.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_both_project_argument_and_workspace_option">
<source>Cannot specify both project argument and workspace option.</source>
<target state="new">Cannot specify both project argument and workspace option.</target>
<note />
</trans-unit>
<trans-unit id="Complete_in_0_ms">
<source>Complete in {0}ms.</source>
<target state="new">Complete in {0}ms.</target>
Expand Down Expand Up @@ -192,6 +197,11 @@
<target state="new">Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.</target>
<note />
</trans-unit>
<trans-unit id="Workspace_option_is_deprecated_Use_the_project_argument_instead">
<source>"workspace" option is deprecated. Use the project argument instead.</source>
<target state="new">"workspace" option is deprecated. Use the project argument instead.</target>
<note />
</trans-unit>
<trans-unit id="Writing_formatting_report_to_0">
<source>Writing formatting report to: '{0}'.</source>
<target state="new">Writing formatting report to: '{0}'.</target>
Expand Down
10 changes: 10 additions & 0 deletions src/xlf/Resources.ko.xlf
Expand Up @@ -27,6 +27,11 @@
<target state="new">Cannot specify both folder and workspace options.</target>
<note />
</trans-unit>
<trans-unit id="Cannot_specify_both_project_argument_and_workspace_option">
<source>Cannot specify both project argument and workspace option.</source>
<target state="new">Cannot specify both project argument and workspace option.</target>
<note />
</trans-unit>
<trans-unit id="Complete_in_0_ms">
<source>Complete in {0}ms.</source>
<target state="new">Complete in {0}ms.</target>
Expand Down Expand Up @@ -192,6 +197,11 @@
<target state="new">Warnings were encountered while loading the workspace. Set the verbosity option to the 'diagnostic' level to log warnings.</target>
<note />
</trans-unit>
<trans-unit id="Workspace_option_is_deprecated_Use_the_project_argument_instead">
<source>"workspace" option is deprecated. Use the project argument instead.</source>
<target state="new">"workspace" option is deprecated. Use the project argument instead.</target>
<note />
</trans-unit>
<trans-unit id="Writing_formatting_report_to_0">
<source>Writing formatting report to: '{0}'.</source>
<target state="new">Writing formatting report to: '{0}'.</target>
Expand Down