Skip to content

Instantly share code, notes, and snippets.

@ChadRoberts21
Last active July 26, 2018 09:40
Show Gist options
  • Save ChadRoberts21/85b8eabfd5fe8a0dfc61ada0054eb386 to your computer and use it in GitHub Desktop.
Save ChadRoberts21/85b8eabfd5fe8a0dfc61ada0054eb386 to your computer and use it in GitHub Desktop.
CMD Utils
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;
namespace <Project>.CMD
{
public static class Utils
{
private static string flagRegex = "(-[a-zA-z])";
public static Dictionary<string, string> MakeArgsDict(string[] args)
{
// looks for flags (a "-" followed by a letter) and puts any string after the flag and the flag it self in a dict, is flags are singular then the value is set to null
Dictionary<string, string> argDict = new Dictionary<string, string>();
Regex regex = new Regex(flagRegex);
for (int i = 0; i < args.Length; i++)
{
Match match = regex.Match(args[i]);
if (match.Success)
{
if ((i + 1 < args.Length) && (!regex.Match(args[i + 1]).Success))
{
argDict.Add(args[i], args[i + 1]);
i++;
}
else
{
argDict.Add(args[i], null);
}
}
}
return argDict;
}
public static bool YesOrNoQuestion(string question)
{
bool stop = false;
while (!stop)
{
Console.WriteLine(question + " [yes/no]");
string answer = Console.ReadLine();
switch (answer.ToLower())
{
case "yes":
case "ye":
case "y":
case "yeah":
case "yea":
{
stop = true;
return true;
}
case "no":
case "n":
case "nope":
case "no way":
{
stop = true;
return false;
}
default:
{
Console.WriteLine("Not a recognized answer. Please try again.");
break;
}
}
}
//encase we magically fall out of the loop
return false;
}
public static string AskUserName()
{
Console.Write("User Name: ");
Console.WriteLine("\n");
return Console.ReadLine();
}
public static string AskPassword()
{
System.Console.Write("Password: ");
string password = null;
while (true)
{
ConsoleKeyInfo key = System.Console.ReadKey(true);
if (key.Key == ConsoleKey.Enter)
break;
password += key.KeyChar;
Console.Write("*");
}
Console.WriteLine("\n");
return password;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment