Browse Source

Switch CommandLineArguments to McMaster

pull/3205/head
Christoph Wille 1 year ago
parent
commit
e39403289d
  1. 1
      Directory.Packages.props
  2. 64
      ILSpy.Tests/CommandLineArgumentsTests.cs
  3. 82
      ILSpy/CommandLineArguments.cs
  4. 1
      ILSpy/ILSpy.csproj

1
Directory.Packages.props

@ -14,6 +14,7 @@ @@ -14,6 +14,7 @@
<PackageVersion Include="Iced" Version="1.21.0" />
<PackageVersion Include="JunitXml.TestLogger" Version="3.1.12" />
<PackageVersion Include="K4os.Compression.LZ4" Version="1.3.8" />
<PackageVersion Include="McMaster.Extensions.CommandLineUtils" Version="4.1.1" />
<PackageVersion Include="McMaster.Extensions.Hosting.CommandLine" Version="4.1.1" />
<PackageVersion Include="Microsoft.CodeAnalysis.CSharp" Version="4.9.2" />
<PackageVersion Include="Microsoft.CodeAnalysis.VisualBasic" Version="4.9.2" />

64
ILSpy.Tests/CommandLineArgumentsTests.cs

@ -24,25 +24,75 @@ namespace ICSharpCode.ILSpy.Tests @@ -24,25 +24,75 @@ namespace ICSharpCode.ILSpy.Tests
[Test]
public void VerifySeparateOption()
{
var cmdLineArgs = new CommandLineArguments(new string[] { "/separate" });
var cmdLineArgs = new CommandLineArguments(new string[] { "--instancing", "Multi" });
cmdLineArgs.SingleInstance.Should().BeFalse();
}
[Test]
public void VerifySingleInstanceOption()
{
var cmdLineArgs = new CommandLineArguments(new string[] { "/singleInstance" });
var cmdLineArgs = new CommandLineArguments(new string[] { "--instancing", "Single" });
cmdLineArgs.SingleInstance.Should().BeTrue();
}
[Test]
public void VerifySeparateSingleInstanceOptionOrdering()
public void VerifyNavigateToOption()
{
var cmdLineArgsCase1 = new CommandLineArguments(new string[] { "/singleInstance", "/separate" });
cmdLineArgsCase1.SingleInstance.Should().BeFalse();
const string navigateTo = "MyNamespace.MyClass";
var cmdLineArgs = new CommandLineArguments(new string[] { "--navigateto", navigateTo });
cmdLineArgs.NavigateTo.Should().BeEquivalentTo(navigateTo);
}
[Test]
public void VerifySearchOption()
{
const string searchWord = "TestContainers";
var cmdLineArgs = new CommandLineArguments(new string[] { "--search", searchWord });
cmdLineArgs.Search.Should().BeEquivalentTo(searchWord);
}
[Test]
public void VerifyLanguageOption()
{
const string language = "csharp";
var cmdLineArgs = new CommandLineArguments(new string[] { "--language", language });
cmdLineArgs.Language.Should().BeEquivalentTo(language);
}
var cmdLineArgsCase2 = new CommandLineArguments(new string[] { "/separate", "/singleInstance" });
cmdLineArgsCase2.SingleInstance.Should().BeTrue();
[Test]
public void VerifyConfigOption()
{
const string configFile = "myilspyoptions.xml";
var cmdLineArgs = new CommandLineArguments(new string[] { "--config", configFile });
cmdLineArgs.ConfigFile.Should().BeEquivalentTo(configFile);
}
[Test]
public void VerifyNoActivateOption()
{
var cmdLineArgs = new CommandLineArguments(new string[] { "--noactivate" });
cmdLineArgs.NoActivate.Should().BeTrue();
}
[Test]
public void MultipleAssembliesAsArguments()
{
var cmdLineArgs = new CommandLineArguments(new string[] { "assembly1", "assembly2", "assembly3" });
cmdLineArgs.AssembliesToLoad.Should().HaveCount(3);
}
[Test]
public void PassAtFileArgumentsSpaceSeparated()
{
string filepath = System.IO.Path.GetTempFileName();
System.IO.File.WriteAllText(filepath, "assembly1 assembly2 assembly3 --instancing multi --noactivate");
var cmdLineArgs = new CommandLineArguments(new string[] { $"@{filepath}" });
cmdLineArgs.SingleInstance.Should().BeFalse();
cmdLineArgs.NoActivate.Should().BeTrue();
cmdLineArgs.AssembliesToLoad.Should().HaveCount(3);
}
}
}

82
ILSpy/CommandLineArguments.cs

@ -18,9 +18,20 @@ @@ -18,9 +18,20 @@
using System;
using System.Collections.Generic;
using System.Linq;
using McMaster.Extensions.CommandLineUtils;
using TomsToolbox.Essentials;
namespace ICSharpCode.ILSpy
{
internal enum InstancingMode
{
Single,
Multi
}
sealed class CommandLineArguments
{
// see /doc/Command Line.txt for details
@ -34,32 +45,59 @@ namespace ICSharpCode.ILSpy @@ -34,32 +45,59 @@ namespace ICSharpCode.ILSpy
public CommandLineArguments(IEnumerable<string> arguments)
{
foreach (string arg in arguments)
var app = new CommandLineApplication() {
ResponseFileHandling = ResponseFileHandling.ParseArgsAsSpaceSeparated,
};
var oInstancing = app.Option("-i|--instancing <single/multi>", "Single or multi instance", CommandOptionType.SingleValue);
var oNavigateTo = app.Option("-n|--navigateto <TYPENAME>", "Navigates to the member specified by the given ID string", CommandOptionType.SingleValue);
var oSearch = app.Option("-s|--search <STRING>", "Search for", CommandOptionType.SingleValue);
var oLanguage = app.Option("-l|--language <STRING>", "Selects the specified language", CommandOptionType.SingleValue);
var oConfig = app.Option("-c|--config <STRING>", "Search for", CommandOptionType.SingleValue);
var oNoActivate = app.Option("--noactivate", "Search for", CommandOptionType.NoValue);
// https://natemcmaster.github.io/CommandLineUtils/docs/arguments.html#variable-numbers-of-arguments
// To enable this, MultipleValues must be set to true, and the argument must be the last one specified.
var files = app.Argument("Assemblies", "Assemblies to load", multipleValues: true);
app.Parse(arguments.ToArray());
if (oInstancing.Value != null)
{
if (arg.Length == 0)
continue;
if (arg[0] == '/')
if (Enum.TryParse<InstancingMode>(oInstancing.Value(), true, out var mode))
{
if (arg.Equals("/singleInstance", StringComparison.OrdinalIgnoreCase))
this.SingleInstance = true;
else if (arg.Equals("/separate", StringComparison.OrdinalIgnoreCase))
this.SingleInstance = false;
else if (arg.StartsWith("/navigateTo:", StringComparison.OrdinalIgnoreCase))
this.NavigateTo = arg.Substring("/navigateTo:".Length);
else if (arg.StartsWith("/search:", StringComparison.OrdinalIgnoreCase))
this.Search = arg.Substring("/search:".Length);
else if (arg.StartsWith("/language:", StringComparison.OrdinalIgnoreCase))
this.Language = arg.Substring("/language:".Length);
else if (arg.Equals("/noActivate", StringComparison.OrdinalIgnoreCase))
this.NoActivate = true;
else if (arg.StartsWith("/config:", StringComparison.OrdinalIgnoreCase))
this.ConfigFile = arg.Substring("/config:".Length);
}
else
{
this.AssembliesToLoad.Add(arg);
switch (mode)
{
case InstancingMode.Single:
SingleInstance = true;
break;
case InstancingMode.Multi:
SingleInstance = false;
break;
}
}
}
if (oNavigateTo.Value != null)
NavigateTo = oNavigateTo.Value();
if (oSearch.Value != null)
Search = oSearch.Value();
if (oLanguage.Value != null)
Language = oLanguage.Value();
if (oConfig.Value != null)
ConfigFile = oConfig.Value();
if (oNoActivate.HasValue())
NoActivate = true;
foreach (var assembly in files.Values)
{
if (!string.IsNullOrWhiteSpace(assembly))
AssembliesToLoad.Add(assembly);
}
}
}
}

1
ILSpy/ILSpy.csproj

@ -45,6 +45,7 @@ @@ -45,6 +45,7 @@
<ItemGroup>
<PackageReference Include="AvalonEdit" />
<PackageReference Include="Dirkster.AvalonDock.Themes.VS2013" />
<PackageReference Include="McMaster.Extensions.CommandLineUtils" />
<PackageReference Include="Microsoft.VisualStudio.Composition" />
<PackageReference Include="DataGridExtensions" />
<PackageReference Include="Microsoft.Xaml.Behaviors.Wpf" />

Loading…
Cancel
Save