public static PingTask FromCommandLines(string[] args) { PingTask task = new PingTask(); task.Args = PingArgs.FromCommandLines(args); return(task); }
public static PingArgs FromCommandLines(string[] args) { List <string> rawArgs = new List <string>(); Dictionary <string, string> options = new Dictionary <string, string>(); int i = 0; while (i < args.Length) { string name = args[i]; string value = i + 1 < args.Length ? args[i + 1] : null; if (name.StartsWith("-")) { string key = name.Substring(1); if (value.StartsWith("-")) { string key2 = value.Substring(1); options[key] = null; } else { options[key] = value; } } else { rawArgs.Add(args[i]); } i++; } int? count = GetInt(options, "c"); int? timeout = GetInt(options, "t"); int? interval = GetInt(options, "i"); string p = options.ContainsKey("p") ? options["p"] : null; var result = new PingArgs(); result.Count = count ?? result.Count; result.Timeout = timeout ?? result.Timeout; result.Interval = interval ?? result.Interval; result.PingType = p == "u" ? PingTypes.Udp : result.PingType; var line = rawArgs.FirstOrDefault(); if (rawArgs.Any()) { var items = line.Split(new string[] { ":" }, StringSplitOptions.RemoveEmptyEntries); IPAddress ad; if (!string.IsNullOrWhiteSpace(items[0]) && IPAddress.TryParse(items[0], out ad)) { result.Address = ad; } if (items.Length > 1) { int port = 0; if (int.TryParse(items[1], out port)) { result.Port = port; } } } return(result); }