Files
marketdata/CommandArgs.cs
2024-02-22 14:52:53 -05:00

83 lines
2.2 KiB
C#

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MarketData.Utils;
//
namespace MarketData
{
public class CommandArgs : Dictionary<String,NVP>
{
// While the commands are converted to uppercase care must be taken to preserve the case of the arguments.
public CommandArgs(String[] 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;
}
}
}