Upgrade argument parsing. The new argument parser is not fully functional yet though.

This commit is contained in:
Starbeamrainbowlabs 2016-05-08 12:51:57 +01:00
parent 151e45c3de
commit 82c47179a5

View file

@ -3,6 +3,7 @@ using System.Text;
using System.Diagnostics;
using System.Reflection;
using System.IO;
using System.Collections.Generic;
namespace cscz
{
@ -10,6 +11,9 @@ namespace cscz
{
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();
@ -24,16 +28,53 @@ namespace cscz
}
ClassGenerator cg = new ClassGenerator();
/*
cg.Signatures.Add("float radius");
cg.Signatures.Add("double weight");
Console.WriteLine(cg);
*/
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;
}
}
}