Merge pull request from erri120/cli

Cli
This commit is contained in:
Timothy Baldridge 2020-01-05 06:40:11 -08:00 committed by GitHub
commit a380f0d59e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
6 changed files with 91 additions and 9 deletions
Wabbajack.Common
Wabbajack.Lib/Downloaders
Wabbajack

79
Wabbajack.Common/CLI.cs Normal file
View File

@ -0,0 +1,79 @@
using System;
using System.Linq;
using System.Reflection;
namespace Wabbajack.Common
{
public static class CLIArguments
{
[CLIOptions("nosettings")]
public static bool NoSettings { get; set; }
[CLIOptions("apikey")]
public static string ApiKey { get; set; }
[CLIOptions("install", ShortOption = 'i')]
public static string InstallPath { get; set; }
}
public static class CLI
{
/// <summary>
/// Parses the argument and sets the properties of <see cref="CLIArguments"/>
/// </summary>
/// <param name="args"><see cref="Environment.GetCommandLineArgs"/></param>
public static void ParseOptions(string[] args)
{
if (args.Length == 1) return;
// get all properties of the class Options
typeof(CLIArguments).GetProperties().Do(p =>
{
var optionAttr = (CLIOptions[])p.GetCustomAttributes(typeof(CLIOptions));
if (optionAttr.Length != 1)
return;
var cur = optionAttr[0];
if (cur?.Option == null) return;
FillVariable(cur.Option, ref p, ref args, false);
FillVariable(cur.ShortOption, ref p, ref args, true);
});
}
private static void FillVariable(dynamic option, ref PropertyInfo p, ref string[] args, bool single)
{
var s = single ? $"-{option}" : $"--{option}";
if (!args.Any(a => a.Contains(s))) return;
if (p.PropertyType == typeof(bool))
{
p.SetValue(p, true);
return;
}
var filtered = args.Where(a => a.Contains(s)).ToList();
if (filtered.Count != 1) return;
var arg = filtered[0];
arg = arg.Replace($"{s}=", "");
if(p.PropertyType == typeof(string))
p.SetValue(p, arg);
}
}
[AttributeUsage(AttributeTargets.Property)]
public class CLIOptions : Attribute
{
// --option, long name of the option. Eg: --output
public string Option;
// -shortOption, short name of the option. Eg: -o
public char ShortOption;
public CLIOptions(string option)
{
Option = option;
}
}
}

View File

@ -19,7 +19,7 @@ namespace Wabbajack.Common
{ {
{"", "Wabbajack"}, {"", "Wabbajack"},
{"FriendlyTypeName", "Wabbajack"}, {"FriendlyTypeName", "Wabbajack"},
{"shell\\open\\command", "\"{appPath}\" -i \"%1\""}, {"shell\\open\\command", "\"{appPath}\" -i=\"%1\""},
}; };
private static readonly Dictionary<string, string> ExtList = new Dictionary<string, string> private static readonly Dictionary<string, string> ExtList = new Dictionary<string, string>
@ -34,7 +34,7 @@ namespace Wabbajack.Common
var tempKey = progIDKey?.OpenSubKey("shell\\open\\command"); var tempKey = progIDKey?.OpenSubKey("shell\\open\\command");
if (progIDKey == null || tempKey == null) return true; if (progIDKey == null || tempKey == null) return true;
var value = tempKey.GetValue(""); var value = tempKey.GetValue("");
return value == null || value.ToString().Equals($"\"{appPath}\" -i \"%1\""); return value == null || value.ToString().Equals($"\"{appPath}\" -i=\"%1\"");
} }
public static bool IsAssociated() public static bool IsAssociated()

View File

@ -35,6 +35,11 @@ namespace Wabbajack.Lib.Downloaders
public NexusDownloader() public NexusDownloader()
{ {
if (CLIArguments.ApiKey != null)
{
CLIArguments.ApiKey.ToEcryptedJson("nexusapikey");
}
TriggerLogin = ReactiveCommand.CreateFromTask( TriggerLogin = ReactiveCommand.CreateFromTask(
execute: () => Utils.CatchAndLog(NexusApiClient.RequestAndCacheAPIKey), execute: () => Utils.CatchAndLog(NexusApiClient.RequestAndCacheAPIKey),
canExecute: IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler)); canExecute: IsLoggedIn.Select(b => !b).ObserveOn(RxApp.MainThreadScheduler));

View File

@ -14,7 +14,7 @@ namespace Wabbajack
{ {
public App() public App()
{ {
// Do initialization in MainWindow ctor CLI.ParseOptions(Environment.GetCommandLineArgs());
} }
} }
} }

View File

@ -129,16 +129,16 @@ namespace Wabbajack
.Select(active => !SettingsPane.IsValueCreated || !object.ReferenceEquals(active, SettingsPane.Value)), .Select(active => !SettingsPane.IsValueCreated || !object.ReferenceEquals(active, SettingsPane.Value)),
execute: () => NavigateTo(SettingsPane.Value)); execute: () => NavigateTo(SettingsPane.Value));
} }
private static bool IsStartingFromModlist(out string modlistPath) private static bool IsStartingFromModlist(out string modlistPath)
{ {
string[] args = Environment.GetCommandLineArgs(); if (CLIArguments.InstallPath == null)
if (args.Length != 3 || !args[1].Contains("-i"))
{ {
modlistPath = default; modlistPath = default;
return false; return false;
} }
modlistPath = args[2]; modlistPath = CLIArguments.InstallPath;
return true; return true;
} }

View File

@ -48,9 +48,7 @@ namespace Wabbajack
}).FireAndForget(); }).FireAndForget();
// Load settings // Load settings
string[] args = Environment.GetCommandLineArgs(); if (CLIArguments.NoSettings || !MainSettings.TryLoadTypicalSettings(out var settings))
if ((args.Length > 1 && args[1] == "nosettings")
|| !MainSettings.TryLoadTypicalSettings(out var settings))
{ {
_settings = new MainSettings(); _settings = new MainSettings();
RunWhenLoaded(DefaultSettings); RunWhenLoaded(DefaultSettings);