Added help argument to CLI

This commit is contained in:
erri120 2020-01-06 15:54:46 +01:00
parent b8a6db5211
commit 92c841f755
No known key found for this signature in database
GPG Key ID: A8C0A18D8D4D3135
2 changed files with 30 additions and 3 deletions

View File

@ -6,14 +6,17 @@ namespace Wabbajack.Common
{ {
public static class CLIArguments public static class CLIArguments
{ {
[CLIOptions("nosettings")] [CLIOptions("nosettings", HelpText = "Don't load the saved Settings")]
public static bool NoSettings { get; set; } public static bool NoSettings { get; set; }
[CLIOptions("apikey")] [CLIOptions("apikey", HelpText = "Manually input an Nexus api key")]
public static string ApiKey { get; set; } public static string ApiKey { get; set; }
[CLIOptions("install", ShortOption = 'i')] [CLIOptions("install", ShortOption = 'i', HelpText = "Install a ModList via CLI")]
public static string InstallPath { get; set; } public static string InstallPath { get; set; }
[CLIOptions("help", ShortOption = 'h', HelpText = "Display this message")]
public static bool Help { get; set; }
} }
public static class CLI public static class CLI
@ -40,6 +43,26 @@ namespace Wabbajack.Common
}); });
} }
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);
});
}
private static void FillVariable(dynamic option, ref PropertyInfo p, ref string[] args, bool single) private static void FillVariable(dynamic option, ref PropertyInfo p, ref string[] args, bool single)
{ {
var s = single ? $"-{option}" : $"--{option}"; var s = single ? $"-{option}" : $"--{option}";
@ -70,6 +93,8 @@ namespace Wabbajack.Common
public string Option; public string Option;
// -shortOption, short name of the option. Eg: -o // -shortOption, short name of the option. Eg: -o
public char ShortOption; public char ShortOption;
// text to be displayed when --help is called
public string HelpText;
public CLIOptions(string option) public CLIOptions(string option)
{ {

View File

@ -15,6 +15,8 @@ namespace Wabbajack
public App() public App()
{ {
CLI.ParseOptions(Environment.GetCommandLineArgs()); CLI.ParseOptions(Environment.GetCommandLineArgs());
if(CLIArguments.Help)
CLI.DisplayHelpText();
} }
} }
} }