2020-01-05 12:52:02 +00:00
|
|
|
|
using System;
|
|
|
|
|
using System.Linq;
|
|
|
|
|
using System.Reflection;
|
|
|
|
|
|
|
|
|
|
namespace Wabbajack.Common
|
|
|
|
|
{
|
|
|
|
|
public static class CLIArguments
|
|
|
|
|
{
|
2020-01-06 14:54:46 +00:00
|
|
|
|
[CLIOptions("nosettings", HelpText = "Don't load the saved Settings")]
|
2020-01-05 12:52:02 +00:00
|
|
|
|
public static bool NoSettings { get; set; }
|
|
|
|
|
|
2020-01-06 14:54:46 +00:00
|
|
|
|
[CLIOptions("apikey", HelpText = "Manually input an Nexus api key")]
|
2020-01-05 12:52:02 +00:00
|
|
|
|
public static string ApiKey { get; set; }
|
2020-01-05 13:09:02 +00:00
|
|
|
|
|
2020-01-06 14:54:46 +00:00
|
|
|
|
[CLIOptions("install", ShortOption = 'i', HelpText = "Install a ModList via CLI")]
|
2020-01-05 13:09:02 +00:00
|
|
|
|
public static string InstallPath { get; set; }
|
2020-01-06 14:54:46 +00:00
|
|
|
|
|
|
|
|
|
[CLIOptions("help", ShortOption = 'h', HelpText = "Display this message")]
|
|
|
|
|
public static bool Help { get; set; }
|
2020-01-05 12:52:02 +00:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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)
|
|
|
|
|
{
|
2020-01-05 13:09:02 +00:00
|
|
|
|
if (args.Length == 1) return;
|
2020-01-05 12:52:02 +00:00
|
|
|
|
// 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];
|
2020-01-05 13:09:02 +00:00
|
|
|
|
if (cur?.Option == null) return;
|
2020-01-05 12:52:02 +00:00
|
|
|
|
|
2020-01-05 13:09:02 +00:00
|
|
|
|
FillVariable(cur.Option, ref p, ref args, false);
|
|
|
|
|
FillVariable(cur.ShortOption, ref p, ref args, true);
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-01-05 12:52:02 +00:00
|
|
|
|
|
2020-01-06 14:54:46 +00:00
|
|
|
|
public static void DisplayHelpText()
|
|
|
|
|
{
|
|
|
|
|
Console.WriteLine("Wabbajack CLI Help Text");
|
|
|
|
|
Console.WriteLine("{0,-20} | {1,-15} | {2,-30}", "Option", "Short Option", "Help Text");
|
|
|
|
|
|
|
|
|
|
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;
|
|
|
|
|
|
|
|
|
|
var shortText = cur.ShortOption != 0 ? $"-{cur.ShortOption}" : "";
|
|
|
|
|
var helpText = string.IsNullOrWhiteSpace(cur.HelpText) ? "" : cur.HelpText;
|
|
|
|
|
Console.WriteLine("{0,-20} | {1,-15} | {2,-30}", $"--{cur.Option}", shortText, helpText);
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-05 13:09:02 +00:00
|
|
|
|
private static void FillVariable(dynamic option, ref PropertyInfo p, ref string[] args, bool single)
|
|
|
|
|
{
|
|
|
|
|
var s = single ? $"-{option}" : $"--{option}";
|
2020-01-05 12:52:02 +00:00
|
|
|
|
|
2020-01-05 13:09:02 +00:00
|
|
|
|
if (!args.Any(a => a.Contains(s))) return;
|
2020-01-05 12:52:02 +00:00
|
|
|
|
|
2020-01-05 13:09:02 +00:00
|
|
|
|
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);
|
2020-01-05 12:52:02 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
[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;
|
2020-01-06 14:54:46 +00:00
|
|
|
|
// text to be displayed when --help is called
|
|
|
|
|
public string HelpText;
|
2020-01-05 12:52:02 +00:00
|
|
|
|
|
|
|
|
|
public CLIOptions(string option)
|
|
|
|
|
{
|
|
|
|
|
Option = option;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|