Helper also know type string; move to .net 10

This commit is contained in:
BlubbFish 2026-08-04 12:19:10 +02:00
parent cc579102cb
commit 7f3f3a7b89
3 changed files with 195 additions and 193 deletions

View File

@ -1,199 +1,199 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
namespace BlubbFish.Utils namespace BlubbFish.Utils
{ {
public class CmdArgs public class CmdArgs
{ {
public enum ArgLength public enum ArgLength
{ {
Single, Single,
Touple Touple
} }
#region Classes #region Classes
public struct VaildArguments public struct VaildArguments
{ {
public VaildArguments(ArgLength length, Boolean required, String @default = "", String description = "") public VaildArguments(ArgLength length, Boolean required, String @default = "", String description = "")
{ {
this.Required = required; this.Required = required;
this.Length = length; this.Length = length;
this.Description = description; this.Description = description;
this.@Default = @default; this.@Default = @default;
} }
public VaildArguments(ArgLength length, String @default = "", String description = "") public VaildArguments(ArgLength length, String @default = "", String description = "")
{ {
this.Required = false; this.Required = false;
this.Length = length; this.Length = length;
this.Description = description; this.Description = description;
this.@Default = @default; this.@Default = @default;
} }
public ArgLength Length { get; private set; } public ArgLength Length { get; private set; }
public Boolean Required { get; private set; } public Boolean Required { get; private set; }
public String Description { get; private set; } public String Description { get; private set; }
public String Default { get; private set; } public String Default { get; private set; }
} }
private struct ArgTouple private struct ArgTouple
{ {
public ArgTouple(String type, String data) public ArgTouple(String type, String data)
{ {
this.Type = type; this.Type = type;
this.Data = data; this.Data = data;
} }
public ArgTouple(String type) public ArgTouple(String type)
{ {
this.Type = type; this.Type = type;
this.Data = null; this.Data = null;
} }
public String Type { get; private set; } public String Type { get; private set; }
public String Data { get; private set; } public String Data { get; private set; }
internal void SetData(String data) internal void SetData(String data)
{ {
if (data != "") { if (data != "") {
this.Data = data; this.Data = data;
} }
} }
} }
#endregion #endregion
private String[] args; private String[] args;
private List<ArgTouple> argList; private List<ArgTouple> argList;
private Dictionary<String, VaildArguments> argsPosible = new Dictionary<String, VaildArguments>(); private Dictionary<String, VaildArguments> argsPosible = new Dictionary<String, VaildArguments>();
private static CmdArgs instances = null; private static CmdArgs instances = null;
private Boolean isSetArguments = false; private Boolean isSetArguments = false;
private CmdArgs() private CmdArgs()
{ {
} }
/// <summary> /// <summary>
/// Gibt eine Instanz der Klasse zurück /// Gibt eine Instanz der Klasse zurück
/// </summary> /// </summary>
/// <returns>Klasse</returns> /// <returns>Klasse</returns>
public static CmdArgs Instance public static CmdArgs Instance
{ {
get { get {
if (instances == null) { if (instances == null) {
instances = new CmdArgs(); instances = new CmdArgs();
} }
return instances; return instances;
} }
} }
/// <summary> /// <summary>
/// Übernimmt die Argumente für die Klasse /// Übernimmt die Argumente für die Klasse
/// </summary> /// </summary>
/// <param name="arguments">Mögliche Komandozeilenargumente</param> /// <param name="arguments">Mögliche Komandozeilenargumente</param>
/// <param name="args">Tatsächliche Komandozeilenargumente</param> /// <param name="args">Tatsächliche Komandozeilenargumente</param>
public void SetArguments(Dictionary<String, VaildArguments> arguments, String[] args) public void SetArguments(Dictionary<String, VaildArguments> arguments, String[] args)
{ {
this.args = args; this.args = args;
if (!this.isSetArguments) { if (!this.isSetArguments) {
this.isSetArguments = true; this.isSetArguments = true;
this.argsPosible = arguments; this.argsPosible = arguments;
this.Init(); this.Init();
} }
} }
private void Init() private void Init()
{ {
this.argList = new List<ArgTouple>(); this.argList = new List<ArgTouple>();
for (Int32 i = 0; i < this.args.Length; i++) { for (Int32 i = 0; i < this.args.Length; i++) {
if (this.argsPosible.Keys.Contains(this.args[i])) { if (this.argsPosible.Keys.Contains(this.args[i])) {
ArgTouple arg = new ArgTouple(this.args[i]); ArgTouple arg = new ArgTouple(this.args[i]);
if (this.argsPosible[this.args[i]].Length == ArgLength.Touple) { if (this.argsPosible[this.args[i]].Length == ArgLength.Touple) {
if (this.args.Length > i + 1) { if (this.args.Length > i + 1) {
arg.SetData(this.args[++i]); arg.SetData(this.args[++i]);
} else { } else {
Console.WriteLine(this.GetUsageList("")); Console.WriteLine(this.GetUsageList(""));
throw new ArgumentException("Argument: "+this.args[i]+" missing second argument."); throw new ArgumentException("Argument: "+this.args[i]+" missing second argument.");
} }
} }
this.argList.Add(arg); this.argList.Add(arg);
} }
} }
foreach(KeyValuePair<String, VaildArguments> item in this.argsPosible) { foreach(KeyValuePair<String, VaildArguments> item in this.argsPosible) {
if(!this.HasArgumentType(item.Key) && item.Value.Length == ArgLength.Touple && item.Value.Default != "") { if(!this.HasArgumentType(item.Key) && item.Value.Length == ArgLength.Touple && item.Value.Default != "") {
this.argList.Add(new ArgTouple(item.Key, item.Value.Default)); this.argList.Add(new ArgTouple(item.Key, item.Value.Default));
} }
} }
} }
/// <summary> /// <summary>
/// Menge der angegebenen Komandozeilen-Argumente /// Menge der angegebenen Komandozeilen-Argumente
/// </summary> /// </summary>
/// <returns>Menge</returns> /// <returns>Menge</returns>
public Int32 GetArgsLength() => this.argList.Count; public Int32 GetArgsLength() => this.argList.Count;
/// <summary> /// <summary>
/// Gibt zurück ob ein Argument angegeben wurde /// Gibt zurück ob ein Argument angegeben wurde
/// </summary> /// </summary>
/// <param name="name">Name des Arguments</param> /// <param name="name">Name des Arguments</param>
/// <returns>true wenn angegeben</returns> /// <returns>true wenn angegeben</returns>
public Boolean HasArgumentType(String name) public Boolean HasArgumentType(String name)
{ {
foreach (ArgTouple t in this.argList) { foreach (ArgTouple t in this.argList) {
if (t.Type == name) { if (t.Type == name) {
return true; return true;
} }
} }
return false; return false;
} }
/// <summary> /// <summary>
/// Gibt den Inhalt des angegeben Arguments zurück, nur bei zweiteiligen Argumenten möglich /// Gibt den Inhalt des angegeben Arguments zurück, nur bei zweiteiligen Argumenten möglich
/// </summary> /// </summary>
/// <param name="name">Name des Arguments</param> /// <param name="name">Name des Arguments</param>
/// <returns>Inhalt des Arguments oder ArgumentNullException</returns> /// <returns>Inhalt des Arguments oder ArgumentNullException</returns>
public String GetArgumentData(String name) public String GetArgumentData(String name)
{ {
foreach (ArgTouple t in this.argList) { foreach (ArgTouple t in this.argList) {
if (t.Type == name && t.Data != null) { if (t.Type == name && t.Data != null) {
return t.Data; return t.Data;
} }
} }
throw new ArgumentNullException(); throw new ArgumentNullException();
} }
public Boolean HasAllRequiredArguments() public Boolean HasAllRequiredArguments()
{ {
foreach (KeyValuePair<String, VaildArguments> item in this.argsPosible) { foreach (KeyValuePair<String, VaildArguments> item in this.argsPosible) {
if (item.Value.Required && !this.HasArgumentType(item.Key)) { if (item.Value.Required && !this.HasArgumentType(item.Key)) {
return false; return false;
} }
} }
return true; return true;
} }
public String GetUsageList(String name) public String GetUsageList(String name)
{ {
String ret = "Usage: " + name + " Parameter\nParameter:\n"; String ret = "Usage: " + name + " Parameter\nParameter:\n";
String req = ""; String req = "";
String opt = ""; String opt = "";
foreach (KeyValuePair<String, VaildArguments> item in this.argsPosible) { foreach (KeyValuePair<String, VaildArguments> item in this.argsPosible) {
if (item.Value.Required) { if (item.Value.Required) {
req += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? (item.Value.Default != "" ? " " + item.Value.Default + "\n" : " [data]\n") : "\n"); req += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? (item.Value.Default != "" ? " " + item.Value.Default + "\n" : " [data]\n") : "\n");
if(item.Value.Description != "") { if(item.Value.Description != "") {
req += "\t" + item.Value.Description + "\n"; req += "\t" + item.Value.Description + "\n";
} }
} }
} }
if (req != "") { if (req != "") {
ret += "Benötigte Parameter:\n" + req; ret += "Benötigte Parameter:\n" + req;
} }
foreach (KeyValuePair<String, VaildArguments> item in this.argsPosible) { foreach (KeyValuePair<String, VaildArguments> item in this.argsPosible) {
if (!item.Value.Required) { if (!item.Value.Required) {
opt += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? (item.Value.Default != "" ? " " + item.Value.Default + "\n" : " [data]\n") : "\n"); opt += item.Key + " " + ((item.Value.Length == ArgLength.Touple) ? (item.Value.Default != "" ? " " + item.Value.Default + "\n" : " [data]\n") : "\n");
if (item.Value.Description != "") { if (item.Value.Description != "") {
opt += "\t" + item.Value.Description + "\n"; opt += "\t" + item.Value.Description + "\n";
} }
} }
} }
if (opt != "") { if (opt != "") {
ret += "Optionale Parameter:\n" + opt; ret += "Optionale Parameter:\n" + opt;
} }
return ret; return ret;
} }
} }
} }

View File

@ -23,7 +23,9 @@ namespace BlubbFish.Utils {
public static void SetProperty(this Object o, String name, String value) { public static void SetProperty(this Object o, String name, String value) {
PropertyInfo prop = o.GetType().GetProperty(name); PropertyInfo prop = o.GetType().GetProperty(name);
if (prop.CanWrite) { 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); prop.SetValue(o, vb);
} else if (prop.PropertyType == typeof(Byte) && Byte.TryParse(value, out Byte v8)) { } else if (prop.PropertyType == typeof(Byte) && Byte.TryParse(value, out Byte v8)) {
prop.SetValue(o, v8); prop.SetValue(o, v8);

View File

@ -1,7 +1,7 @@
<Project Sdk="Microsoft.NET.Sdk"> <Project Sdk="Microsoft.NET.Sdk">
<PropertyGroup> <PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework> <TargetFramework>net10.0</TargetFramework>
<AssemblyName>Utils</AssemblyName> <AssemblyName>Utils</AssemblyName>
<RootNamespace>BlubbFish.Utils</RootNamespace> <RootNamespace>BlubbFish.Utils</RootNamespace>
<Description>Provides useful classes for other projects</Description> <Description>Provides useful classes for other projects</Description>