Files
CopyDb/CommandArgs.cs
2024-02-23 00:41:24 -05:00

74 lines
2.0 KiB
C#

using System;
using System.Collections.Generic;
using System.Text;
namespace CopyDb
{
public class CommandArgs : Dictionary<String,NVP>
{
public CommandArgs(String[] args)
{
for(int index=0;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],sb.ToString().Trim()};
}
}
else nvp=command.Split('=');
if(2!=nvp.Length)continue;
if(ContainsKey(nvp[0]))continue;
Add(nvp[0].ToUpper(),new NVP(nvp[0],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;
}
}
}