Browse Source

CommandLineArguments ctor to static Create method

pull/3212/head
Christoph Wille 2 years ago
parent
commit
baea9c940a
  1. 24
      ILSpy.Tests/CommandLineArgumentsTests.cs
  2. 2
      ILSpy/App.xaml.cs
  3. 39
      ILSpy/AppEnv/CommandLineArguments.cs
  4. 2
      ILSpy/MainWindow.xaml.cs

24
ILSpy.Tests/CommandLineArgumentsTests.cs

@ -14,7 +14,7 @@ namespace ICSharpCode.ILSpy.Tests
[Test] [Test]
public void VerifyEmptyArgumentsArray() public void VerifyEmptyArgumentsArray()
{ {
var cmdLineArgs = new CommandLineArguments(new string[] { }); var cmdLineArgs = CommandLineArguments.Create(new string[] { });
cmdLineArgs.AssembliesToLoad.Should().BeEmpty(); cmdLineArgs.AssembliesToLoad.Should().BeEmpty();
cmdLineArgs.SingleInstance.Should().BeNull(); cmdLineArgs.SingleInstance.Should().BeNull();
@ -28,14 +28,14 @@ namespace ICSharpCode.ILSpy.Tests
[Test] [Test]
public void VerifyHelpOption() public void VerifyHelpOption()
{ {
var cmdLineArgs = new CommandLineArguments(new string[] { "--help" }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--help" });
cmdLineArgs.ArgumentsParser.IsShowingInformation.Should().BeTrue(); cmdLineArgs.ArgumentsParser.IsShowingInformation.Should().BeTrue();
} }
[Test] [Test]
public void VerifyForceNewInstanceOption() public void VerifyForceNewInstanceOption()
{ {
var cmdLineArgs = new CommandLineArguments(new string[] { "--newinstance" }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--newinstance" });
cmdLineArgs.SingleInstance.Should().BeFalse(); cmdLineArgs.SingleInstance.Should().BeFalse();
} }
@ -43,21 +43,21 @@ namespace ICSharpCode.ILSpy.Tests
public void VerifyNavigateToOption() public void VerifyNavigateToOption()
{ {
const string navigateTo = "MyNamespace.MyClass"; const string navigateTo = "MyNamespace.MyClass";
var cmdLineArgs = new CommandLineArguments(new string[] { "--navigateto", navigateTo }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--navigateto", navigateTo });
cmdLineArgs.NavigateTo.Should().BeEquivalentTo(navigateTo); cmdLineArgs.NavigateTo.Should().BeEquivalentTo(navigateTo);
} }
[Test] [Test]
public void VerifyNavigateToOption_NoneTest_Matching_VSAddin() public void VerifyNavigateToOption_NoneTest_Matching_VSAddin()
{ {
var cmdLineArgs = new CommandLineArguments(new string[] { "--navigateto:none" }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--navigateto:none" });
cmdLineArgs.NavigateTo.Should().BeEquivalentTo("none"); cmdLineArgs.NavigateTo.Should().BeEquivalentTo("none");
} }
[Test] [Test]
public void VerifyCaseSensitivityOfOptionsDoesntThrow() public void VerifyCaseSensitivityOfOptionsDoesntThrow()
{ {
var cmdLineArgs = new CommandLineArguments(new string[] { "--navigateTo:none" }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--navigateTo:none" });
cmdLineArgs.ArgumentsParser.RemainingArguments.Should().HaveCount(1); cmdLineArgs.ArgumentsParser.RemainingArguments.Should().HaveCount(1);
} }
@ -66,7 +66,7 @@ namespace ICSharpCode.ILSpy.Tests
public void VerifySearchOption() public void VerifySearchOption()
{ {
const string searchWord = "TestContainers"; const string searchWord = "TestContainers";
var cmdLineArgs = new CommandLineArguments(new string[] { "--search", searchWord }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--search", searchWord });
cmdLineArgs.Search.Should().BeEquivalentTo(searchWord); cmdLineArgs.Search.Should().BeEquivalentTo(searchWord);
} }
@ -74,7 +74,7 @@ namespace ICSharpCode.ILSpy.Tests
public void VerifyLanguageOption() public void VerifyLanguageOption()
{ {
const string language = "csharp"; const string language = "csharp";
var cmdLineArgs = new CommandLineArguments(new string[] { "--language", language }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--language", language });
cmdLineArgs.Language.Should().BeEquivalentTo(language); cmdLineArgs.Language.Should().BeEquivalentTo(language);
} }
@ -82,21 +82,21 @@ namespace ICSharpCode.ILSpy.Tests
public void VerifyConfigOption() public void VerifyConfigOption()
{ {
const string configFile = "myilspyoptions.xml"; const string configFile = "myilspyoptions.xml";
var cmdLineArgs = new CommandLineArguments(new string[] { "--config", configFile }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--config", configFile });
cmdLineArgs.ConfigFile.Should().BeEquivalentTo(configFile); cmdLineArgs.ConfigFile.Should().BeEquivalentTo(configFile);
} }
[Test] [Test]
public void VerifyNoActivateOption() public void VerifyNoActivateOption()
{ {
var cmdLineArgs = new CommandLineArguments(new string[] { "--noactivate" }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "--noactivate" });
cmdLineArgs.NoActivate.Should().BeTrue(); cmdLineArgs.NoActivate.Should().BeTrue();
} }
[Test] [Test]
public void MultipleAssembliesAsArguments() public void MultipleAssembliesAsArguments()
{ {
var cmdLineArgs = new CommandLineArguments(new string[] { "assembly1", "assembly2", "assembly3" }); var cmdLineArgs = CommandLineArguments.Create(new string[] { "assembly1", "assembly2", "assembly3" });
cmdLineArgs.AssembliesToLoad.Should().HaveCount(3); cmdLineArgs.AssembliesToLoad.Should().HaveCount(3);
} }
@ -107,7 +107,7 @@ namespace ICSharpCode.ILSpy.Tests
System.IO.File.WriteAllText(filepath, "assembly1\r\nassembly2\r\nassembly3\r\n--newinstance\r\n--noactivate"); System.IO.File.WriteAllText(filepath, "assembly1\r\nassembly2\r\nassembly3\r\n--newinstance\r\n--noactivate");
var cmdLineArgs = new CommandLineArguments(new string[] { $"@{filepath}" }); var cmdLineArgs = CommandLineArguments.Create(new string[] { $"@{filepath}" });
try try
{ {

2
ILSpy/App.xaml.cs

@ -65,7 +65,7 @@ namespace ICSharpCode.ILSpy
ILSpySettings.SettingsFilePathProvider = new ILSpySettingsFilePathProvider(); ILSpySettings.SettingsFilePathProvider = new ILSpySettingsFilePathProvider();
var cmdArgs = Environment.GetCommandLineArgs().Skip(1); var cmdArgs = Environment.GetCommandLineArgs().Skip(1);
App.CommandLineArguments = new CommandLineArguments(cmdArgs); App.CommandLineArguments = CommandLineArguments.Create(cmdArgs);
bool forceSingleInstance = (App.CommandLineArguments.SingleInstance ?? true) bool forceSingleInstance = (App.CommandLineArguments.SingleInstance ?? true)
&& !MiscSettingsPanel.CurrentMiscSettings.AllowMultipleInstances; && !MiscSettingsPanel.CurrentMiscSettings.AllowMultipleInstances;

39
ILSpy/AppEnv/CommandLineArguments.cs

@ -18,6 +18,7 @@
using McMaster.Extensions.CommandLineUtils; using McMaster.Extensions.CommandLineUtils;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
@ -36,7 +37,12 @@ namespace ICSharpCode.ILSpy.AppEnv
public CommandLineApplication ArgumentsParser { get; } public CommandLineApplication ArgumentsParser { get; }
public CommandLineArguments(IEnumerable<string> arguments) private CommandLineArguments(CommandLineApplication app)
{
ArgumentsParser = app;
}
public static CommandLineArguments Create(IEnumerable<string> arguments)
{ {
var app = new CommandLineApplication() { var app = new CommandLineApplication() {
// https://natemcmaster.github.io/CommandLineUtils/docs/response-file-parsing.html?tabs=using-attributes // https://natemcmaster.github.io/CommandLineUtils/docs/response-file-parsing.html?tabs=using-attributes
@ -47,14 +53,16 @@ namespace ICSharpCode.ILSpy.AppEnv
}; };
app.HelpOption(); app.HelpOption();
ArgumentsParser = app; var instance = new CommandLineArguments(app);
try
{
var oForceNewInstance = app.Option("--newinstance", var oForceNewInstance = app.Option("--newinstance",
"Start a new instance of ILSpy even if the user configuration is set to single-instance", "Start a new instance of ILSpy even if the user configuration is set to single-instance",
CommandOptionType.NoValue); CommandOptionType.NoValue);
var oNavigateTo = app.Option<string>("-n|--navigateto <TYPENAME>", var oNavigateTo = app.Option<string>("-n|--navigateto <TYPENAME>",
"Navigates to the member specified by the given ID string.\r\nThe member is searched for only in the assemblies specified on the command line.\r\nExample: 'ILSpy ILSpy.exe --navigateto:T:ICSharpCode.ILSpy.CommandLineArguments'", "Navigates to the member specified by the given ID string.\r\nThe member is searched for only in the assemblies specified on the command line.\r\nExample: 'ILSpy ILSpy.exe --navigateto T:ICSharpCode.ILSpy.CommandLineArguments'",
CommandOptionType.SingleValue); CommandOptionType.SingleValue);
oNavigateTo.DefaultValue = null; oNavigateTo.DefaultValue = null;
@ -64,12 +72,12 @@ namespace ICSharpCode.ILSpy.AppEnv
oSearch.DefaultValue = null; oSearch.DefaultValue = null;
var oLanguage = app.Option<string>("-l|--language <LANGUAGEIDENTIFIER>", var oLanguage = app.Option<string>("-l|--language <LANGUAGEIDENTIFIER>",
"Selects the specified language.\r\nExample: 'ILSpy --language:C#' or 'ILSpy --language:IL'", "Selects the specified language.\r\nExample: 'ILSpy --language:C#' or 'ILSpy --language IL'",
CommandOptionType.SingleValue); CommandOptionType.SingleValue);
oLanguage.DefaultValue = null; oLanguage.DefaultValue = null;
var oConfig = app.Option<string>("-c|--config <CONFIGFILENAME>", var oConfig = app.Option<string>("-c|--config <CONFIGFILENAME>",
"Provide a specific configuration file.\r\nExample: 'ILSpy --config:myconfig.xml'", "Provide a specific configuration file.\r\nExample: 'ILSpy --config myconfig.xml'",
CommandOptionType.SingleValue); CommandOptionType.SingleValue);
oConfig.DefaultValue = null; oConfig.DefaultValue = null;
@ -84,21 +92,28 @@ namespace ICSharpCode.ILSpy.AppEnv
app.Parse(arguments.ToArray()); app.Parse(arguments.ToArray());
if (oForceNewInstance.HasValue()) if (oForceNewInstance.HasValue())
SingleInstance = false; instance.SingleInstance = false;
NavigateTo = oNavigateTo.ParsedValue; instance.NavigateTo = oNavigateTo.ParsedValue;
Search = oSearch.ParsedValue; instance.Search = oSearch.ParsedValue;
Language = oLanguage.ParsedValue; instance.Language = oLanguage.ParsedValue;
ConfigFile = oConfig.ParsedValue; instance.ConfigFile = oConfig.ParsedValue;
if (oNoActivate.HasValue()) if (oNoActivate.HasValue())
NoActivate = true; instance.NoActivate = true;
foreach (var assembly in files.Values) foreach (var assembly in files.Values)
{ {
if (!string.IsNullOrWhiteSpace(assembly)) if (!string.IsNullOrWhiteSpace(assembly))
AssembliesToLoad.Add(assembly); instance.AssembliesToLoad.Add(assembly);
}
} }
catch (Exception ex)
{
// Intentionally ignore exceptions if any, this is only added to always have an exception-free startup
}
return instance;
} }
} }
} }

2
ILSpy/MainWindow.xaml.cs

@ -654,7 +654,7 @@ namespace ICSharpCode.ILSpy
internal async Task HandleSingleInstanceCommandLineArguments(string[] args) internal async Task HandleSingleInstanceCommandLineArguments(string[] args)
{ {
var cmdArgs = new CommandLineArguments(args); var cmdArgs = CommandLineArguments.Create(args);
await Dispatcher.InvokeAsync(() => { await Dispatcher.InvokeAsync(() => {
if (HandleCommandLineArguments(cmdArgs)) if (HandleCommandLineArguments(cmdArgs))

Loading…
Cancel
Save