private static bool ParseArguments(IEnumerable <string> args, out bool isVerbError) { bool noVerb = false; using (Parser parser = new Parser(cfg => { cfg.CaseInsensitiveEnumValues = true; cfg.HelpWriter = Console.Out; })) { bool success = true; parser.ParseArguments <VisualizerOptions, CheckUpdatesOption>(args) .WithNotParsed(e => { noVerb = e.Any(err => err.Tag == ErrorType.NoVerbSelectedError || err.Tag == ErrorType.BadVerbSelectedError); success = false; }) .WithParsed <CheckUpdatesOption>(o => checkUpdates = true) .WithParsed <VisualizerOptions>(o => { options = o; if (!File.Exists(o.FileName)) { Console.WriteLine("File does not exist"); success = false; } }); isVerbError = noVerb; return(success); } }
private static bool ParseArguments(IEnumerable <string> args) { bool ProcessAppOptions(AppOptions o) { if (!string.IsNullOrWhiteSpace(o.ProxyAddress)) { int protocolLength = o.ProxyAddress.IndexOf("://"); if (!o.ProxyAddress.StartsWith("http://", StringComparison.InvariantCultureIgnoreCase)) { if (protocolLength > -1) { o.ProxyAddress = "http" + o.ProxyAddress.Substring(protocolLength); } else { o.ProxyAddress = "http://" + o.ProxyAddress; } } if (!Uri.IsWellFormedUriString(o.ProxyAddress, UriKind.Absolute)) { Console.WriteLine("Invalid proxy address"); return(false); } proxySettings = new ProxySettings { Address = o.ProxyAddress, Username = o.ProxyUsername, Password = o.ProxyPassword }; } if (o.UseMirror) { UrlManager.MirrorMode = o.UseMirror; } if (o.ServerUrl != null) { UrlManager.BaseUrl = o.ServerUrl; } return(true); } using (Parser parser = new Parser(cfg => { cfg.CaseInsensitiveEnumValues = true; cfg.HelpWriter = Console.Out; })) { bool success = true; parser.ParseArguments <VisualizerOptions, CheckUpdatesOption>(args) .WithNotParsed(e => success = false) .WithParsed <CheckUpdatesOption>(o => checkUpdates = true) .WithParsed <VisualizerOptions>(o => { if (!ProcessAppOptions(o)) { success = false; return; } options = o; if (!File.Exists(o.FileName)) { Console.WriteLine("File does not exist"); success = false; } }); return(success); } }