public virtual bool ShouldAppendFlag(Argument argument, Type type, object value) { if (PreAppendChecker != null && !PreAppendChecker(argument, type, value)) { return(false); } if (AppendCheckers.TryGetValue(type, out Func <Argument, Type, object, bool> append)) { return(append(argument, type, value)); } return(true); }
public YoutubeDLApi(string executable, YoutubeDLArguments arguments = null) : base(executable, arguments) { ValueTranslators.Add(typeof(DateTime), (a, t, v) => { return(((DateTime)v).ToString("yyyyMMdd")); }); ValueTranslators.Add(typeof(ByteSize), (a, t, v) => { return(((ByteSize)v).Bytes.ToString("#.#")); }); AppendCheckers.Add(typeof(Dictionary <string, string>), (a, t, v) => { return(((Dictionary <string, string>)v).Count > 0); }); }
protected void SetDefaults(bool clear = true) { if (clear) { ValueTranslators.Clear(); Builders.Clear(); AppendCheckers.Clear(); PreAppendChecker = null; } // Translates string properties to string... duh! ValueTranslators.Add(typeof(string), (a, t, v) => v.ToString()); // Translates enum properties to string by using the name representing the enum value. ValueTranslators.Add(typeof(Enum), (a, t, v) => { string name = Enum.GetName(t, v); return(a.EnumCase == EnumCasePolicy.LOWERCASE ? name.ToLower() : a.EnumCase == EnumCasePolicy.UPPERCASE ? name.ToUpper() : name); }); // Only append flag if value isn't null. This is checked before any other registered append checkers. PreAppendChecker = (a, t, v) => v != null; // Only append boolean flags if the value is true. AppendCheckers.Add(typeof(bool), (a, t, v) => (bool)v); // Add bool flags if value isnt false. // Custom builder for string -> string dictionaries repeatedly adds flag with key-value pair seperated by colon. Builders.Add(typeof(Dictionary <string, string>), (a, t, v) => { string builtFlag = string.Empty; int count = 0; Dictionary <string, string> dic = (Dictionary <string, string>)v; foreach (KeyValuePair <string, string> entry in dic) { string value = string.Format("{0}:{1}", entry.Key, entry.Value); value = QuoteFlagValue(a.QuotePolicy, value); builtFlag += string.Format("{0}{1} ", (dic.Count > 1 && count > 0) ? (a.Flag + " ") : string.Empty, value); count++; } if (dic.Count > 0) { builtFlag = builtFlag.Remove(builtFlag.Length - 1, 1); } return(a.Template.Replace("{flag}", a.Flag).Replace("{value}", builtFlag)); }); }