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++)
{
if (args[i].StartsWith("-"))
{
if (args[i] == "-v" || args[i] == "--verbose")
{
Verbose = true;
}
else if (args[i] == "-m" || args[i] == "--mafiles-path")
{
i++;
if (i < args.Length)
SteamGuardPath = args[i];
else
{
Console.WriteLine($"Expected path after {args[i-1]}");
return;
}
}
else if (args[i] == "--help" || args[i] == "-h")
{ {
ShowHelp(); ShowHelp();
return; return;
} }
Verbose = args.Contains("-v") || args.Contains("--verbose");
// Actions
if (args.Contains("--generate-code"))
{
action = "generate-code";
} }
else if (args.Contains("--encrypt")) else // Parse as action or username
{ {
action = "encrypt"; if (string.IsNullOrEmpty(action))
}
else if (args.Contains("--decrypt"))
{ {
action = "decrypt"; if (args[i] == "add")
}
else if (args.Contains("--setup"))
{ {
action = "setup"; action = "setup";
} }
// Misc else if (args[i] == "encrypt")
if (args.Contains("--user") || args.Contains("-u"))
{ {
int u = Array.IndexOf(args, "--user"); action = "encrypt";
if (u == -1)
{
u = Array.IndexOf(args, "-u");
} }
try else if (args[i] == "decrypt")
{ {
user = args[u + 1]; action = "decrypt";
} }
catch (IndexOutOfRangeException) else if (args[i] == "remove")
{ {
Console.WriteLine("error: Account name must be supplied after --user or -u."); action = "remove";
return; }
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];
} }
if (Verbose) Console.WriteLine("Generating Steam Gaurd code for account \"{0}\"", user);
} }
// 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)
{ {