protected OptionEnum AddOptionEnum(TOption type, string name, string description, List <string> values, string defaultValue, params string[] alias) { var opt = new OptionEnum(name, description, values, defaultValue, alias); Options.Add(type, opt); return(opt); }
public Program(string configFile) : this() { if (!string.IsNullOrWhiteSpace(configFile) && File.Exists(configFile)) { JsonSettings = JObject.Parse(File.ReadAllText(configFile)); //Se sono state definite delle settings di base le carico if (JsonSettings["settings"] != null) { var s = (JObject)JsonSettings["settings"]; Settings.ClearConsole = SetJsonValue(s, "clearConsole", Settings.ClearConsole); Settings.MenuType = SetJsonValue(s, "menuType", Settings.MenuType); Settings.MenuHeader = SetJsonValue(s, "menuHeader", Settings.MenuHeader); Settings.MenuFooter = SetJsonValue(s, "menuFooter", Settings.MenuFooter); Settings.Help = SetJsonValue(s, "help", Settings.Help); } if (JsonSettings["options"] != null) { var optList = (JArray)JsonSettings["options"]; foreach (JObject j in optList) { var jType = (OptionType)Enum.Parse(typeof(OptionType), j["type"].ToString()); Option opt; var enumValue = (TOption)Enum.Parse(typeof(TOption), j["name"].ToString()); switch (jType) { case OptionType.Flag: opt = new OptionFlag( j["name"].ToString(), j["description"] != null ? j["description"].ToString() : string.Empty, j["default"].ToObject <bool>(), j["alias"].ToList().ConvertAll(c => c.ToString()).ToArray() ); break; case OptionType.Date: opt = new OptionDate( j["name"].ToString(), j["description"] != null ? j["description"].ToString() : string.Empty, j["default"].ToObject <DateTime>(), j["alias"].ToList().ConvertAll(c => c.ToString()).ToArray() ); break; case OptionType.Number: opt = new OptionNumber( j["name"].ToString(), j["description"] != null ? j["description"].ToString() : string.Empty, j["default"].ToObject <decimal>(), j["alias"].ToList().ConvertAll(c => c.ToString()).ToArray() ); break; case OptionType.Enum: opt = new OptionEnum( j["name"].ToString(), j["description"] != null ? j["description"].ToString() : string.Empty, j["items"].ToList().ConvertAll(c => c.ToString()), j["default"].ToString(), j["alias"].ToList().ConvertAll(c => c.ToString()).ToArray() ); break; default: opt = new OptionString( j["name"].ToString(), j["description"] != null ? j["description"].ToString() : string.Empty, j["default"].ToString(), j["alias"].ToList().ConvertAll(c => c.ToString()).ToArray() ); break; } Options.Add(enumValue, opt); } } //Se sono state definite dei commands di base le carico if (JsonSettings["commands"] != null) { var cmdList = (JArray)JsonSettings["commands"]; foreach (JObject j in cmdList) { var enumValue = (TCommand)Enum.Parse(typeof(TCommand), j["name"].ToString()); var cmd = new Command <TOption>( j["name"].ToString(), j["description"] != null ? j["description"].ToString() : string.Empty, j["alias"].ToList().ConvertAll(c => c.ToString()).ToArray() ); if (j["menuId"] != null) { var id = int.Parse(j["menuId"].ToString()); if (Commands.Values.Count(c => c.MenuID == id) > 0) { throw new ArgumentException("MenuID of commands must be unique"); } cmd.MenuID = id; } else { cmd.MenuID = Commands.Values.Max(c => c.MenuID) + 1; } cmd.ShowInMenu = j["showInMenu"] != null ? j["showInMenu"].ToObject <bool>() : true; foreach (var item in j["validOptions"].ToList().ConvertAll(c => c.ToString()).ToArray()) { if (Options.Count(c => c.Value.Name == item) > 0) { cmd.ValidOptions.Add(Options.FirstOrDefault(c => c.Value.Name == item).Key); } } Commands.Add(enumValue, cmd); } } if (Settings.Help) { HelpCommand.MenuID = Commands.Values.Max(c => c.MenuID) + 1; } ExitCommand.MenuID = Commands.Values.Max(c => c.MenuID) + 1; } }