static ParsedArgs ParseArgs(string[] args) { var parsedArgs = new ParsedArgs(); // The first argument is always the 'verb' unless it starts with a hyphen. var first = args.FirstOrDefault(); int skip = 0; if (first != null && !first.StartsWith("-")) { parsedArgs.Verb = first; skip = 1; } string currentSwitch = ""; foreach (var arg in args.Skip(skip)) { if (arg.StartsWith("-")) { // arguments to a switch can be space or colon seperated. Colon args are found first. var splits = arg.Split(':'); currentSwitch = splits.First().Substring(1).ToLower(); var switchArgs = splits.Skip(1).ToList(); parsedArgs.Add(currentSwitch, switchArgs); } else { List <string> switchArgs; if (!parsedArgs.TryGetValue(currentSwitch, out switchArgs)) { switchArgs = new List <string>(); parsedArgs.Add(currentSwitch, switchArgs); } switchArgs.Add(arg); } } return(parsedArgs); }
public MethodInvoker GetMethodInvoker(ParsedArgs args) { var argNames = args.Keys; if (args.Verb != Name || // Parameters.Count() != args.Count || Parameters.All(p => p.SupportsArgument(argNames)) == false) { return(null); } var parameterValues = new List <object>(); foreach (var para in Parameters.OrderBy(p => p.Position)) { object invokeParam; var alias = para.GetSupportedAlias(argNames); // if null method argument was an optional argument, so pass default value. if (alias == null) { invokeParam = para.DefaultValue; } else { // check if parameter is an array if (para.IsArray) { invokeParam = para.GetParameterArray(args[alias].ToArray()); } else { invokeParam = para.GetParameter(args[alias].FirstOrDefault()); } } parameterValues.Add(invokeParam); } var invoker = new MethodInvoker(this, parameterValues.ToArray()); return(invoker); }