Files
ARM64/IPMonitor/CommandArgs.cs
2025-04-27 00:33:08 -04:00

86 lines
2.3 KiB
C#

using System.Text;
using MarketData.Utils;
namespace IPMonitor
{
public class CommandArgs : Dictionary<String,NVP>
{
private String[] arguments = default;
public String[] GetArguments()
{
return arguments;
}
// While the commands are converted to uppercase care must be taken to preserve the case of the arguments.
public CommandArgs(String[] args)
{
arguments = args;
for(int index=1;index<args.Length;index++)
{
String command=args[index];
String[] nvp=null;
if(command.StartsWith("/"))
{
command=command.Substring(1);
nvp=command.Split(':');
if(nvp.Length>2)
{
StringBuilder sb=new StringBuilder();
for(int subIndex=1;subIndex<nvp.Length;subIndex++)
{
sb.Append(nvp[subIndex]);
if(subIndex<nvp.Length-1)sb.Append(":");
}
nvp=new String[]{nvp[0].ToUpper(),sb.ToString()};
}
}
else nvp=command.Split('=');
if(2!=nvp.Length)continue;
if(ContainsKey(nvp[0].ToUpper()))continue;
Add(nvp[0].ToUpper(),new NVP(nvp[0].ToUpper(),nvp[1]));
}
}
public bool Contains(String name)
{
return ContainsKey(name);
}
public bool Has(String commandList)
{
commandList=commandList.ToUpper();
String[] commands=commandList.Split(',');
if(0==commands.Length)return false;
foreach(String command in commands)
{
if(!ContainsKey(command))return false;
}
return true;
}
public T Get<T>(String name)
{
T result=default(T);
try {result = (T)Convert.ChangeType(this[name].Value, typeof(T));}
catch {result = default(T);}
return result;
}
public T Coalesce<T>(String name,T coalesce)
{
T result=default(T);
try
{
if(!Contains(name))result=coalesce;
else result = (T)Convert.ChangeType(this[name].Value, typeof(T));
}
catch {result = default(T);}
return result;
}
public T Coalesce<T>(String name)
{
T result=default(T);
try {result = (T)Convert.ChangeType(this[name].Value, typeof(T));}
catch {result = default(T);}
return result;
}
}
}