public Command ParseArguments(Command c, ArrayList args)
{
PropertyInfo [] pi = c.GetType ().GetProperties ();
int num_args = args != null ? args.Count : 0;
for (int i = 0; i < num_args; i++){
string arg = (string) args [i];
if (!arg.StartsWith ("-")){
if (c.Args == null)
c.Args = new ArrayList ();
c.Args.Add (arg);
for (int j = i+1; j < args.Count; j++)
c.Args.Add ((string) args [j]);
break;
}
arg = arg.Substring (1);
for (int j = 0; j < pi.Length; j++){
if (!pi [j].CanWrite)
continue;
object[] attrs = pi [j].GetCustomAttributes (
typeof (PropertyAttribute), false);
if ((attrs != null) && (attrs.Length > 0)) {
PropertyAttribute attr = (PropertyAttribute) attrs [0];
if ((arg != attr.Name) && (arg != attr.ShortName))
continue;
} else if (pi [j].Name.ToLower () != arg)
continue;
if (pi [j].PropertyType == typeof (bool)){
pi [j].SetValue (c, true, null);
goto next;
}
if (pi [j].PropertyType == typeof (string)){
i++;
if (i >= args.Count){
Error ("Missing argument to flag: " + arg);
return null;
}
pi [j].SetValue (c, args [i], null);
goto next;
}
if (pi [j].PropertyType == typeof (int)){
i++;
if (i >= args.Count){
Error ("Missing integer to flag: " + arg);
return null;
}
try {
pi [j].SetValue (c, Int32.Parse ((string) args [i]), null);
} catch {
Error ("Expected number, got: `{0}'", args [i]);
return null;
}
goto next;
}
}
if (c.Args == null)
c.Args = new ArrayList ();
c.Args.Add (args [i]);
next:
;
}
return c;
}