Files
Electron.NET/ElectronNET.CLI/SimpleCommandLineParser.cs

60 lines
1.8 KiB
C#
Raw Normal View History

using System;
using System.Collections.Generic;
using System.Linq;
2018-02-11 22:24:12 +01:00
namespace ElectronNET.CLI
{
public class SimpleCommandLineParser
{
public SimpleCommandLineParser()
{
Arguments = new Dictionary<string, string[]>();
}
public IDictionary<string, string[]> Arguments { get; private set; }
public void Parse(string[] args)
{
var currentName = "";
var values = new List<string>();
foreach (var arg in args)
{
if (arg.StartsWith("/"))
{
if (currentName != "")
2021-09-15 11:14:45 +02:00
{
2018-02-11 22:24:12 +01:00
Arguments[currentName] = values.ToArray();
2021-09-15 11:14:45 +02:00
}
2018-02-11 22:24:12 +01:00
values.Clear();
currentName = arg.Substring(1);
}
else if (currentName == "")
2021-09-15 11:14:45 +02:00
{
Arguments[arg] = Array.Empty<string>();
}
2018-02-11 22:24:12 +01:00
else
2021-09-15 11:14:45 +02:00
{
2018-02-11 22:24:12 +01:00
values.Add(arg);
2021-09-15 11:14:45 +02:00
}
2018-02-11 22:24:12 +01:00
}
if (currentName != "")
Arguments[currentName] = values.ToArray();
Console.ForegroundColor = ConsoleColor.Green;
Console.WriteLine($"Arguments: \n\t{string.Join("\n\t",Arguments.Keys.Select(i => $"{i} = {string.Join(" ", Arguments[i])}"))}");
Console.ResetColor();
2018-02-11 22:24:12 +01:00
}
public bool Contains(string name)
{
return Arguments.ContainsKey(name);
}
internal bool TryGet(string key, out string[] value)
{
value = null;
if (!Contains(key)) {
return false;
}
value = Arguments[key];
return true;
}
2018-02-11 22:24:12 +01:00
}
}