public static ILookup<String, String> Parse(String[] args, params CommandlineOption[] options) { return options .SelectMany(o => o.ShortNames .Select(c => Tuple.Create("-" + c, o)) .Concat(o.LongNames.Select(s => Tuple.Create("--" + s, o))) ) .ToDictionary() .Let(map => args .TakeWhile(s => s != "--") .SelectMany((a, i) => a.Length > 1 && a[0] == '-' && a[1] != '-' ? a.Substring(1, a.Length - 2) .Select(c => MakeTuple(map, "-" + c, o => null)) .EndWith(MakeTuple(map, "-" + a.Last(), o => o.AllowValue ? args[i + 1] : null)) : new[] { a.StartsWith("--") ? a.Split(new [] { '=', }, 2).Let(_ => MakeTuple(map, _[0], o => _.Length == 2 ? _[1] : null) ) : MakeTuple(_parameter, a), } ) ) .ToArray() .Let(_ => _.Where((t, i) => t.Item1 != _parameter || i == 0 || !_[i - 1].Item1.AllowValue)) .Concat(args .SkipWhile(s => s != "--") .Skip(1) .Select(a => MakeTuple(_parameter, a)) ) .ToLookup(t => t.Item1.Null(o => o.Id), t => t.Item2); }
private static void Main(String[] args) { _parameters = ConfigurationManager.AppSettings.AllKeys .ToDictionary(k => k, k => ConfigurationManager.AppSettings[k]); foreach (Match match in args .TakeWhile(s => s != "--") .Select(s => Regex.Match(s, "(-(?<key>[a-zA-Z0-9_]*)(=(?<value>(\"[^\"]*\")|('[^']*')|(.*)))?)*")) .Where(m => m.Success) ) { _parameters[match.Groups["key"].Value] = match.Groups["value"].Success ? match.Groups["value"].Value : "true"; } if (AppDomain.CurrentDomain.IsDefaultAppDomain()) { Environment.CurrentDirectory = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location); AppDomain domain = AppDomain.CreateDomain( "MetaTweetMint.exe:run", AppDomain.CurrentDomain.Evidence, new AppDomainSetup() { ApplicationBase = Path.GetFullPath(_parameters["init_base"]), PrivateBinPath = _parameters["init_probe"], PrivateBinPathProbe = "true", ApplicationName = "MetaTweetMint", LoaderOptimization = LoaderOptimization.MultiDomainHost, } ); domain.ExecuteAssembly(Assembly.GetExecutingAssembly().Location, args); return; } String cultureString; Thread.CurrentThread.CurrentCulture = _parameters.ContainsKey("culture") ? String.IsNullOrEmpty(cultureString = _parameters["culture"]) ? Thread.CurrentThread.CurrentCulture : cultureString == "invaliant" ? CultureInfo.InvariantCulture : CultureInfo.GetCultureInfo(cultureString) : CultureInfo.InvariantCulture; Thread.CurrentThread.CurrentUICulture = Thread.CurrentThread.CurrentCulture; Environment.CurrentDirectory = AppDomain.CurrentDomain.BaseDirectory; Application.ThreadException += (sender, e) => new ThreadExceptionDialog(e.Exception) .ShowDialog(); using (ClientCore client = new ClientCore()) { client.Initialize(_parameters); client.Run(); } }
private static void Main(String[] args) { if (args.TakeWhile(s => s == "-").Any(s => s == "-d" || s == "-debug")) { Debugger.Launch(); } if (Environment.UserInteractive) { RunServerInConsole(args.SkipWhile(s => s == "-")); } else { ServiceBase.Run(new ServerHost()); } }
public async Task<PingResult> PingOne(String address) { String hostname = String.Concat(address.TakeWhile(c => c != ':')); Ping pinger = new Ping(); try { PingReply reply = await pinger.SendPingAsync(hostname, 1000); return new PingResult(address, reply); } catch (PingException) { return null; } }
public IEnumerable<int> Find(string query) { query = new String(query.Reverse().ToArray()); IEnumerable<int> indecies = new int[] { }; query.TakeWhile((c, i) => { if (i == 0) { indecies = Find(c); return true; } else { indecies = Find(indecies, c); return true; } }).ToArray(); return indecies.Select(i => suffix[i]).ToArray(); }
protected override void OnStart(String[] args) { Environment.CurrentDirectory = new FileInfo(Assembly.GetExecutingAssembly().Location).Directory.FullName; foreach (Match match in args .TakeWhile(s => s != "--") .Select(s => Regex.Match(s, "(-(?<key>[a-zA-Z0-9_]*)(=(?<value>(\"[^\"]*\")|('[^']*')|(.*)))?)*")) .Where(m => m.Success) ) { this.Launcher.Arguments[match.Groups["key"].Value] = match.Groups["value"].Success ? match.Groups["value"].Value : "true"; } if (this.Launcher.Arguments.ContainsKey("host_debug") && this.Launcher.Arguments["host_debug"] == "true") { Debugger.Launch(); } this.Launcher.Arguments[".pid"] = Process.GetCurrentProcess().Id.ToString(); this.Launcher.Arguments[".svc_id"] = this.ServiceName; this.Launcher.StartServer(); }
public static long ToBytes(String myHumanSize) { myHumanSize = myHumanSize.Trim(); var numberString = new string(myHumanSize.TakeWhile(_ => Char.IsDigit(_) || Char.IsPunctuation(_)).ToArray()); var unit = myHumanSize.Remove(0, numberString.Length).Trim(); var number = double.Parse(numberString, CultureInfo.GetCultureInfo("en-us")); switch (unit) { case "": case "B": return (long)number; case "KB": case "kB": return (long)(number * 1000); case "KiB": return (long)(number * 1024); case "MB": return (long)(number * 1000000); case "MiB": return (long)(number * 1048576); case "GB": return (long)(number * 1000000000); case "GiB": return (long)(number * 1073741824); case "TB": return (long)(number * 1000000000000); case "TiB": return (long)(number * 1099511627776); case "PB": return (long)(number * 1000000000000000); case "PiB": return (long)(number * 1125899906842624); default: throw new ArgumentException("Can not read value for max memory."); } }
// Gets called when someone does Trace.TraceXxx() or TraceSource.TraceEvent(). public override void TraceEvent(TraceEventCache eventCache, String source, TraceEventType eventType, Int32 id, String message) { // Extract #EventId:123 prefix. if ((message != null) && (message.Length > 0) && (message[0] == '#')) { if (message.StartsWith("#EventId:")) { message = message.Substring(9).TrimStart(); String idAsString = new String(message.TakeWhile(c => Char.IsDigit(c)).ToArray()); if (idAsString != String.Empty) { id = Int32.Parse(idAsString); message = message.Substring(idAsString.Length).TrimStart(); } } } try { // Note: By default, the id is 0. TraceSimpleEvent(eventCache.DateTime.ToLocalTime(), Thread.CurrentThread.ManagedThreadId, eventType, message, id, eventCache, source); } catch (Exception ex) { if (ex is NotImplementedException) { string dateTimeString = DateTime.Now.ToString("yyyy/MM/dd HH:mm:ss"); int managedThreadId = Thread.CurrentThread.ManagedThreadId; Debug.WriteLine($"[{eventType}] {dateTimeString} #{managedThreadId,-2} {message}"); } else if (ex is ThreadAbortException) { // Thread is being externally aborted (e.g. because appdomain is being unloaded). Don't fastfail - as that will kill the entire process. throw; } else { // Tracing needs to be reliable otherwise it will be a pain to track down application errors, so make tracing errors fatal. Environment.FailFast("Exception in trace listener.", ex); } } }