improved argument system for optimization and ease of use

This commit is contained in:
Carson McManus 2016-08-25 19:21:34 -04:00
parent e57ad4e2ce
commit 0eef63eca0

View file

@ -5,6 +5,7 @@ using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Runtime.InteropServices;
using System.Text; using System.Text;
using System.Threading.Tasks; using System.Threading.Tasks;
@ -23,55 +24,71 @@ public static class Program
[STAThread] [STAThread]
public static void Main(string[] args) public static void Main(string[] args)
{ {
string action = "generate-code"; string action = "";
string user = ""; string user = "";
// Parse cli arguments // Parse cli arguments
if (args.Contains("--help") || args.Contains("-h")) for (int i = 0; i < args.Length; i++)
{ {
ShowHelp(); if (args[i].StartsWith("-"))
return; {
} if (args[i] == "-v" || args[i] == "--verbose")
Verbose = args.Contains("-v") || args.Contains("--verbose"); {
// Actions Verbose = true;
if (args.Contains("--generate-code")) }
{ else if (args[i] == "-m" || args[i] == "--mafiles-path")
action = "generate-code"; {
} i++;
else if (args.Contains("--encrypt")) if (i < args.Length)
{ SteamGuardPath = args[i];
action = "encrypt"; else
} {
else if (args.Contains("--decrypt")) Console.WriteLine($"Expected path after {args[i-1]}");
{ return;
action = "decrypt"; }
} }
else if (args.Contains("--setup")) else if (args[i] == "--help" || args[i] == "-h")
{ {
action = "setup"; ShowHelp();
} return;
// Misc }
if (args.Contains("--user") || args.Contains("-u")) }
{ else // Parse as action or username
int u = Array.IndexOf(args, "--user"); {
if (u == -1) if (string.IsNullOrEmpty(action))
{ {
u = Array.IndexOf(args, "-u"); if (args[i] == "add")
} {
try action = "setup";
{ }
user = args[u + 1]; else if (args[i] == "encrypt")
} {
catch (IndexOutOfRangeException) action = "encrypt";
{ }
Console.WriteLine("error: Account name must be supplied after --user or -u."); else if (args[i] == "decrypt")
return; {
} action = "decrypt";
if (Verbose) Console.WriteLine("Generating Steam Gaurd code for account \"{0}\"", user); }
} else if (args[i] == "remove")
{
action = "remove";
}
else if (args[i] == "2fa" || args[i] == "code" || args[i] == "generate-code")
{
action = "generate-code";
}
continue;
}
// its a username
if (string.IsNullOrEmpty(user))
user = args[i];
}
}
// Do some configure if (string.IsNullOrEmpty(action))
action = "generate-code";
// Do some configuring
SteamGuardPath = SteamGuardPath.Replace("~", Environment.GetEnvironmentVariable("HOME")); SteamGuardPath = SteamGuardPath.Replace("~", Environment.GetEnvironmentVariable("HOME"));
if (!Directory.Exists(SteamGuardPath)) if (!Directory.Exists(SteamGuardPath))
{ {
@ -86,9 +103,12 @@ public static class Program
return; return;
} }
} }
if (Verbose) Console.WriteLine("maFiles path: {0}", SteamGuardPath);
if (Verbose) Console.WriteLine("Action: {0}", action); if (Verbose) Console.WriteLine($"Action: {action}");
if (Verbose) Console.WriteLine($"User: {user}");
if (Verbose) Console.WriteLine($"maFiles path: {SteamGuardPath}");
return;
// Perform desired action // Perform desired action
switch (action) switch (action)
{ {