cscz/cscz/Program.cs

81 lines
2.2 KiB
C#

using System;
using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
namespace cscz
{
class Program
{
public static void Main(string[] args)
{
Dictionary<string, string> parsedArgs = new Dictionary<string, string>();
ParseOptions(args, new List<string>(), new List<string>(){ "help", "private" });
if(args.Length >= 1 && args[0] == "--help")
{
Assembly asm = Assembly.GetExecutingAssembly();
/*** Debug - Lists the names of all embedded resources ***
foreach(string str in asm.GetManifestResourceNames())
Console.WriteLine(str);*/
StreamReader helpTextReader = new StreamReader(asm.GetManifestResourceStream(@"cscz.Help.md"));
string helpText = helpTextReader.ReadToEnd();
helpTextReader.Dispose();
Console.WriteLine(helpText);
return;
}
ClassGenerator cg = new ClassGenerator();
string source = Console.In.ReadToEnd();
cg.ParseString(source);
cg.CreateProperties = !parsedArgs.ContainsKey("private");
Console.WriteLine(cg);
}
/// <summary>
/// Parses command line options out into a dictionary.
/// </summary>
/// <param name="args">The arguments to parse.</param>
/// <param name="extraArgs">A list in which to dump any extra arguments found that aren't attached to a flag.</param>
/// <param name="noValueArgs">A list of flags which do not take a value.</param>
/// <returns>The parsed arguments.</returns>
static Dictionary<string, string> ParseOptions(string[] args, List<string> extraArgs, List<string> noValueArgs)
{
Dictionary<string, string> result = new Dictionary<string, string>();
for(int i = 0; i < args.Length; i++)
{
if (args[i].StartsWith("-"))
{
string optionKey = args[i].Trim(new char[] { '-', ' ' });
if (i < args.Length - 1 && !args[i + 1].StartsWith("-"))
{
if(!noValueArgs.Contains(optionKey))
{
result[optionKey] = args[i + 1].Trim();
i++;
}
else
{
result[optionKey] = "true";
}
}
else
{
result[optionKey] = "true";
}
}
else
{
extraArgs.Add(args[i]);
}
}
return result;
}
}
}