Browse Source

CommandLineArguments ctor to static Create method

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

24
ILSpy.Tests/CommandLineArgumentsTests.cs

@ -14,7 +14,7 @@ namespace ICSharpCode.ILSpy.Tests @@ -14,7 +14,7 @@ namespace ICSharpCode.ILSpy.Tests
[Test]
public void VerifyEmptyArgumentsArray()
{
var cmdLineArgs = new CommandLineArguments(new string[] { });
var cmdLineArgs = CommandLineArguments.Create(new string[] { });
cmdLineArgs.AssembliesToLoad.Should().BeEmpty();
cmdLineArgs.SingleInstance.Should().BeNull();
@ -28,14 +28,14 @@ namespace ICSharpCode.ILSpy.Tests @@ -28,14 +28,14 @@ namespace ICSharpCode.ILSpy.Tests
[Test]
public void VerifyHelpOption()
{
var cmdLineArgs = new CommandLineArguments(new string[] { "--help" });
var cmdLineArgs = CommandLineArguments.Create(new string[] { "--help" });
cmdLineArgs.ArgumentsParser.IsShowingInformation.Should().BeTrue();
}
[Test]
public void VerifyForceNewInstanceOption()
{
var cmdLineArgs = new CommandLineArguments(new string[] { "--newinstance" });
var cmdLineArgs = CommandLineArguments.Create(new string[] { "--newinstance" });
cmdLineArgs.SingleInstance.Should().BeFalse();
}
@ -43,21 +43,21 @@ namespace ICSharpCode.ILSpy.Tests @@ -43,21 +43,21 @@ namespace ICSharpCode.ILSpy.Tests
public void VerifyNavigateToOption()
{
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);
}
[Test]
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");
}
[Test]
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);
}
@ -66,7 +66,7 @@ namespace ICSharpCode.ILSpy.Tests @@ -66,7 +66,7 @@ namespace ICSharpCode.ILSpy.Tests
public void VerifySearchOption()
{
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);
}
@ -74,7 +74,7 @@ namespace ICSharpCode.ILSpy.Tests @@ -74,7 +74,7 @@ namespace ICSharpCode.ILSpy.Tests
public void VerifyLanguageOption()
{
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);
}
@ -82,21 +82,21 @@ namespace ICSharpCode.ILSpy.Tests @@ -82,21 +82,21 @@ namespace ICSharpCode.ILSpy.Tests
public void VerifyConfigOption()
{
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);
}
[Test]
public void VerifyNoActivateOption()
{
var cmdLineArgs = new CommandLineArguments(new string[] { "--noactivate" });
var cmdLineArgs = CommandLineArguments.Create(new string[] { "--noactivate" });
cmdLineArgs.NoActivate.Should().BeTrue();
}
[Test]
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);
}
@ -107,7 +107,7 @@ namespace ICSharpCode.ILSpy.Tests @@ -107,7 +107,7 @@ namespace ICSharpCode.ILSpy.Tests
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
{

2
ILSpy/App.xaml.cs

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

115
ILSpy/AppEnv/CommandLineArguments.cs

@ -18,6 +18,7 @@ @@ -18,6 +18,7 @@
using McMaster.Extensions.CommandLineUtils;
using System;
using System.Collections.Generic;
using System.Linq;
@ -36,7 +37,12 @@ namespace ICSharpCode.ILSpy.AppEnv @@ -36,7 +37,12 @@ namespace ICSharpCode.ILSpy.AppEnv
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() {
// https://natemcmaster.github.io/CommandLineUtils/docs/response-file-parsing.html?tabs=using-attributes
@ -47,58 +53,67 @@ namespace ICSharpCode.ILSpy.AppEnv @@ -47,58 +53,67 @@ namespace ICSharpCode.ILSpy.AppEnv
};
app.HelpOption();
ArgumentsParser = app;
var oForceNewInstance = app.Option("--newinstance",
"Start a new instance of ILSpy even if the user configuration is set to single-instance",
CommandOptionType.NoValue);
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'",
CommandOptionType.SingleValue);
oNavigateTo.DefaultValue = null;
var oSearch = app.Option<string>("-s|--search <SEARCHTERM>",
"Search for t:TypeName, m:Member or c:Constant; use exact match (=term), 'should not contain' (-term) or 'must contain' (+term); use /reg(ular)?Ex(pressions)?/ or both - t:/Type(Name)?/...",
CommandOptionType.SingleValue);
oSearch.DefaultValue = null;
var oLanguage = app.Option<string>("-l|--language <LANGUAGEIDENTIFIER>",
"Selects the specified language.\r\nExample: 'ILSpy --language:C#' or 'ILSpy --language:IL'",
CommandOptionType.SingleValue);
oLanguage.DefaultValue = null;
var oConfig = app.Option<string>("-c|--config <CONFIGFILENAME>",
"Provide a specific configuration file.\r\nExample: 'ILSpy --config:myconfig.xml'",
CommandOptionType.SingleValue);
oConfig.DefaultValue = null;
var instance = new CommandLineArguments(app);
var oNoActivate = app.Option("--noactivate",
"Do not activate the existing ILSpy instance. This option has no effect if a new ILSpy instance is being started.",
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 (oForceNewInstance.HasValue())
SingleInstance = false;
NavigateTo = oNavigateTo.ParsedValue;
Search = oSearch.ParsedValue;
Language = oLanguage.ParsedValue;
ConfigFile = oConfig.ParsedValue;
if (oNoActivate.HasValue())
NoActivate = true;
foreach (var assembly in files.Values)
try
{
var oForceNewInstance = app.Option("--newinstance",
"Start a new instance of ILSpy even if the user configuration is set to single-instance",
CommandOptionType.NoValue);
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'",
CommandOptionType.SingleValue);
oNavigateTo.DefaultValue = null;
var oSearch = app.Option<string>("-s|--search <SEARCHTERM>",
"Search for t:TypeName, m:Member or c:Constant; use exact match (=term), 'should not contain' (-term) or 'must contain' (+term); use /reg(ular)?Ex(pressions)?/ or both - t:/Type(Name)?/...",
CommandOptionType.SingleValue);
oSearch.DefaultValue = null;
var oLanguage = app.Option<string>("-l|--language <LANGUAGEIDENTIFIER>",
"Selects the specified language.\r\nExample: 'ILSpy --language:C#' or 'ILSpy --language IL'",
CommandOptionType.SingleValue);
oLanguage.DefaultValue = null;
var oConfig = app.Option<string>("-c|--config <CONFIGFILENAME>",
"Provide a specific configuration file.\r\nExample: 'ILSpy --config myconfig.xml'",
CommandOptionType.SingleValue);
oConfig.DefaultValue = null;
var oNoActivate = app.Option("--noactivate",
"Do not activate the existing ILSpy instance. This option has no effect if a new ILSpy instance is being started.",
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 (oForceNewInstance.HasValue())
instance.SingleInstance = false;
instance.NavigateTo = oNavigateTo.ParsedValue;
instance.Search = oSearch.ParsedValue;
instance.Language = oLanguage.ParsedValue;
instance.ConfigFile = oConfig.ParsedValue;
if (oNoActivate.HasValue())
instance.NoActivate = true;
foreach (var assembly in files.Values)
{
if (!string.IsNullOrWhiteSpace(assembly))
instance.AssembliesToLoad.Add(assembly);
}
}
catch (Exception ex)
{
if (!string.IsNullOrWhiteSpace(assembly))
AssembliesToLoad.Add(assembly);
// 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 @@ -654,7 +654,7 @@ namespace ICSharpCode.ILSpy
internal async Task HandleSingleInstanceCommandLineArguments(string[] args)
{
var cmdArgs = new CommandLineArguments(args);
var cmdArgs = CommandLineArguments.Create(args);
await Dispatcher.InvokeAsync(() => {
if (HandleCommandLineArguments(cmdArgs))

Loading…
Cancel
Save