diff --git a/Utils/CmdArgs.cs b/Utils/CmdArgs.cs index 63ade23..92a50bb 100644 --- a/Utils/CmdArgs.cs +++ b/Utils/CmdArgs.cs @@ -1,199 +1,199 @@ -using System; -using System.Collections.Generic; -using System.Linq; - -namespace BlubbFish.Utils -{ - public class CmdArgs - { - public enum ArgLength - { - Single, - Touple - } - #region Classes - public struct VaildArguments - { - public VaildArguments(ArgLength length, Boolean required, String @default = "", String description = "") - { - this.Required = required; - this.Length = length; - this.Description = description; - this.@Default = @default; - } - public VaildArguments(ArgLength length, String @default = "", String description = "") - { - this.Required = false; - this.Length = length; - this.Description = description; - this.@Default = @default; - } - - public ArgLength Length { get; private set; } - public Boolean Required { get; private set; } - public String Description { get; private set; } - public String Default { get; private set; } - } - private struct ArgTouple - { - public ArgTouple(String type, String data) - { - this.Type = type; - this.Data = data; - } - public ArgTouple(String type) - { - this.Type = type; - this.Data = null; - } - public String Type { get; private set; } - public String Data { get; private set; } - - internal void SetData(String data) - { - if (data != "") { - this.Data = data; - } - } - } - #endregion - private String[] args; - private List argList; - private Dictionary argsPosible = new Dictionary(); - private static CmdArgs instances = null; - private Boolean isSetArguments = false; - - private CmdArgs() - { - } - - /// - /// Gibt eine Instanz der Klasse zurück - /// - /// Klasse - public static CmdArgs Instance - { - get { - if (instances == null) { - instances = new CmdArgs(); - } - return instances; - } - } - - /// - /// Übernimmt die Argumente für die Klasse - /// - /// Mögliche Komandozeilenargumente - /// Tatsächliche Komandozeilenargumente - public void SetArguments(Dictionary arguments, String[] args) - { - this.args = args; - if (!this.isSetArguments) { - this.isSetArguments = true; - this.argsPosible = arguments; - this.Init(); - } - } - - private void Init() - { - this.argList = new List(); - for (Int32 i = 0; i < this.args.Length; i++) { - if (this.argsPosible.Keys.Contains(this.args[i])) { - ArgTouple arg = new ArgTouple(this.args[i]); - if (this.argsPosible[this.args[i]].Length == ArgLength.Touple) { - if (this.args.Length > i + 1) { - arg.SetData(this.args[++i]); - } else { - Console.WriteLine(this.GetUsageList("")); - throw new ArgumentException("Argument: "+this.args[i]+" missing second argument."); - } - } - this.argList.Add(arg); - } - } +using System; +using System.Collections.Generic; +using System.Linq; + +namespace BlubbFish.Utils +{ + public class CmdArgs + { + public enum ArgLength + { + Single, + Touple + } + #region Classes + public struct VaildArguments + { + public VaildArguments(ArgLength length, Boolean required, String @default = "", String description = "") + { + this.Required = required; + this.Length = length; + this.Description = description; + this.@Default = @default; + } + public VaildArguments(ArgLength length, String @default = "", String description = "") + { + this.Required = false; + this.Length = length; + this.Description = description; + this.@Default = @default; + } + + public ArgLength Length { get; private set; } + public Boolean Required { get; private set; } + public String Description { get; private set; } + public String Default { get; private set; } + } + private struct ArgTouple + { + public ArgTouple(String type, String data) + { + this.Type = type; + this.Data = data; + } + public ArgTouple(String type) + { + this.Type = type; + this.Data = null; + } + public String Type { get; private set; } + public String Data { get; private set; } + + internal void SetData(String data) + { + if (data != "") { + this.Data = data; + } + } + } + #endregion + private String[] args; + private List argList; + private Dictionary argsPosible = new Dictionary(); + private static CmdArgs instances = null; + private Boolean isSetArguments = false; + + private CmdArgs() + { + } + + /// + /// Gibt eine Instanz der Klasse zurück + /// + /// Klasse + public static CmdArgs Instance + { + get { + if (instances == null) { + instances = new CmdArgs(); + } + return instances; + } + } + + /// + /// Übernimmt die Argumente für die Klasse + /// + /// Mögliche Komandozeilenargumente + /// Tatsächliche Komandozeilenargumente + public void SetArguments(Dictionary arguments, String[] args) + { + this.args = args; + if (!this.isSetArguments) { + this.isSetArguments = true; + this.argsPosible = arguments; + this.Init(); + } + } + + private void Init() + { + this.argList = new List(); + for (Int32 i = 0; i < this.args.Length; i++) { + if (this.argsPosible.Keys.Contains(this.args[i])) { + ArgTouple arg = new ArgTouple(this.args[i]); + if (this.argsPosible[this.args[i]].Length == ArgLength.Touple) { + if (this.args.Length > i + 1) { + arg.SetData(this.args[++i]); + } else { + Console.WriteLine(this.GetUsageList("")); + throw new ArgumentException("Argument: "+this.args[i]+" missing second argument."); + } + } + this.argList.Add(arg); + } + } foreach(KeyValuePair item in this.argsPosible) { if(!this.HasArgumentType(item.Key) && item.Value.Length == ArgLength.Touple && item.Value.Default != "") { this.argList.Add(new ArgTouple(item.Key, item.Value.Default)); } - } - } - - /// - /// Menge der angegebenen Komandozeilen-Argumente - /// - /// Menge - public Int32 GetArgsLength() => this.argList.Count; - - /// - /// Gibt zurück ob ein Argument angegeben wurde - /// - /// Name des Arguments - /// true wenn angegeben - public Boolean HasArgumentType(String name) - { - foreach (ArgTouple t in this.argList) { - if (t.Type == name) { - return true; - } - } - return false; - } - - /// - /// Gibt den Inhalt des angegeben Arguments zurück, nur bei zweiteiligen Argumenten möglich - /// - /// Name des Arguments - /// Inhalt des Arguments oder ArgumentNullException - public String GetArgumentData(String name) - { - foreach (ArgTouple t in this.argList) { - if (t.Type == name && t.Data != null) { - return t.Data; - } - } - throw new ArgumentNullException(); - } - - public Boolean HasAllRequiredArguments() - { - foreach (KeyValuePair item in this.argsPosible) { - if (item.Value.Required && !this.HasArgumentType(item.Key)) { - return false; - } - } - return true; - } - - public String GetUsageList(String name) - { - String ret = "Usage: " + name + " Parameter\nParameter:\n"; - String req = ""; - String opt = ""; - foreach (KeyValuePair item in this.argsPosible) { - if (item.Value.Required) { - req += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? (item.Value.Default != "" ? " " + item.Value.Default + "\n" : " [data]\n") : "\n"); + } + } + + /// + /// Menge der angegebenen Komandozeilen-Argumente + /// + /// Menge + public Int32 GetArgsLength() => this.argList.Count; + + /// + /// Gibt zurück ob ein Argument angegeben wurde + /// + /// Name des Arguments + /// true wenn angegeben + public Boolean HasArgumentType(String name) + { + foreach (ArgTouple t in this.argList) { + if (t.Type == name) { + return true; + } + } + return false; + } + + /// + /// Gibt den Inhalt des angegeben Arguments zurück, nur bei zweiteiligen Argumenten möglich + /// + /// Name des Arguments + /// Inhalt des Arguments oder ArgumentNullException + public String GetArgumentData(String name) + { + foreach (ArgTouple t in this.argList) { + if (t.Type == name && t.Data != null) { + return t.Data; + } + } + throw new ArgumentNullException(); + } + + public Boolean HasAllRequiredArguments() + { + foreach (KeyValuePair item in this.argsPosible) { + if (item.Value.Required && !this.HasArgumentType(item.Key)) { + return false; + } + } + return true; + } + + public String GetUsageList(String name) + { + String ret = "Usage: " + name + " Parameter\nParameter:\n"; + String req = ""; + String opt = ""; + foreach (KeyValuePair item in this.argsPosible) { + if (item.Value.Required) { + req += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? (item.Value.Default != "" ? " " + item.Value.Default + "\n" : " [data]\n") : "\n"); if(item.Value.Description != "") { req += "\t" + item.Value.Description + "\n"; - } - } - } - if (req != "") { - ret += "Benötigte Parameter:\n" + req; - } - foreach (KeyValuePair item in this.argsPosible) { - if (!item.Value.Required) { - opt += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? (item.Value.Default != "" ? " " + item.Value.Default + "\n" : " [data]\n") : "\n"); + } + } + } + if (req != "") { + ret += "Benötigte Parameter:\n" + req; + } + foreach (KeyValuePair item in this.argsPosible) { + if (!item.Value.Required) { + opt += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? (item.Value.Default != "" ? " " + item.Value.Default + "\n" : " [data]\n") : "\n"); if (item.Value.Description != "") { opt += "\t" + item.Value.Description + "\n"; - } - } - } - if (opt != "") { - ret += "Optionale Parameter:\n" + opt; - } - return ret; - } - } -} + } + } + } + if (opt != "") { + ret += "Optionale Parameter:\n" + opt; + } + return ret; + } + } +} diff --git a/Utils/Helper.cs b/Utils/Helper.cs index 9f92a99..b833e57 100644 --- a/Utils/Helper.cs +++ b/Utils/Helper.cs @@ -23,7 +23,9 @@ namespace BlubbFish.Utils { public static void SetProperty(this Object o, String name, String value) { PropertyInfo prop = o.GetType().GetProperty(name); if (prop.CanWrite) { - if (prop.PropertyType == typeof(Boolean) && Boolean.TryParse(value, out Boolean vb)) { + if(prop.PropertyType == typeof(String)) { + prop.SetValue(o, value); + } else if (prop.PropertyType == typeof(Boolean) && Boolean.TryParse(value, out Boolean vb)) { prop.SetValue(o, vb); } else if (prop.PropertyType == typeof(Byte) && Byte.TryParse(value, out Byte v8)) { prop.SetValue(o, v8); diff --git a/Utils/Utils.csproj b/Utils/Utils.csproj index fb43f88..4361b26 100644 --- a/Utils/Utils.csproj +++ b/Utils/Utils.csproj @@ -1,7 +1,7 @@ - netcoreapp3.1 + net10.0 Utils BlubbFish.Utils Provides useful classes for other projects