internal CommandConfigurator(string name, string description, CliConfig cliConfig) { Name = name; Description = description; _parents = new List <string>(); _cliConfig = cliConfig; }
public async void Load_ConfigFile(bool configFileExists) { if (configFileExists) { await File.WriteAllTextAsync(CliConfigFile, JsonConvert.SerializeObject(new { CliConfig = _configs })); } else { if (File.Exists(CliConfigFile)) { File.Delete(CliConfigFile); } } var config = new CliConfig(_logger.Object); await config.Load(); if (configFileExists) { Assert.Equal(_configs["ApiUrl"], config.ApiUrl); } else { Assert.Equal("https://localhost", config.ApiUrl); } }
private CommandConfigurator(string name, List <string> parents, string description, CliConfig cliConfig) { Name = name; Description = description; _parents = parents; _cliConfig = cliConfig; }
public PrintHelpCommandExecutionContext(CliConfig cliConfig, CommandConfig commandConfig) : base((commandArgs, output) => { output.Write(cliConfig.HelpConfig.Printer.PrintHelp(cliConfig, commandConfig)); return(0); }, null) { }
public PrintVersionCommandExecutionContext(CliConfig cliConfig, CommandConfig commandConfig) : base((commandArgs, output) => { output.WriteLine(cliConfig.Name + " Version: " + Assembly.GetEntryAssembly()?.GetName().Version); return(0); }, null) { }
public PrintHelpCommandExecutionContext(string additionalPrefixMessage, CliConfig cliConfig, CommandConfig commandConfig) : base((commandArgs, output) => { output.WriteLine(additionalPrefixMessage); output.WriteLine(); output.Write(cliConfig.HelpConfig.Printer.PrintHelp(cliConfig, commandConfig)); return(ErrorExitCode); }, null) { }
public async void InitConfigFile(bool reset) { await File.WriteAllTextAsync(CliConfigFile, JsonConvert.SerializeObject(new { CliConfig = _configs })); await CliConfig.InitConfigFile(reset, _logger.Object); var expected = reset ? "https://localhost" : "https://localhost/test"; var obj = JObject.Parse(await File.ReadAllTextAsync(CliConfigFile)); var loadedConfigs = obj["CliConfig"].ToObject <Dictionary <string, string> >(); Assert.Equal(expected, loadedConfigs["ApiUrl"]); }
public void SetValue(string configName, string configValue) { if (File.Exists(CliConfigFile)) { File.Delete(CliConfigFile); } var config = new CliConfig(_logger.Object); config.SetValue(configName, configValue); Assert.Equal(configValue, config.Configs[configName]); }
public void TestPropertyValues() { if (File.Exists(CliConfigFile)) { File.Delete(CliConfigFile); } var config = new CliConfig(_logger.Object); Assert.Equal("https://localhost", config.ApiUrl); Assert.Equal(TimeSpan.Parse("00:01:00"), config.ApiRequestTimeout); Assert.Equal("Polyrific/Catapult", config.AppDataFolderPath); }
public CommandExecutionContextProvider( CliConfig cliConfig, CommandConfig rootCommandConfig, string[] args) { _cliConfig = cliConfig; _rootCommandConfig = rootCommandConfig; _args = args; _valueConverter = new ValueConverter(cliConfig.ArgumentConverters); _argumentValueReader = new ArgumentValueReader(_valueConverter); _configReader = new ConfigReader(_argumentValueReader, cliConfig.Config, cliConfig.GenericConfig); }
public async void Save_ToConfigFile() { if (File.Exists(CliConfigFile)) { File.Delete(CliConfigFile); } var config = new CliConfig(_logger.Object); config.Configs["ApiUrl"] = "https://localhost/test"; await config.Save(); var obj = JObject.Parse(await File.ReadAllTextAsync(CliConfigFile)); var loadedConfigs = obj["CliConfig"].ToObject <Dictionary <string, string> >(); Assert.Equal("https://localhost/test", loadedConfigs["ApiUrl"]); }
public void GetValue_WithoutDefault(string configName) { if (File.Exists(CliConfigFile)) { File.Delete(CliConfigFile); } var config = new CliConfig(_logger.Object); if (configName == "ApiUrl") { var value = config.GetValue(configName); Assert.Equal("https://localhost", value); } else { var exception = Record.Exception(() => config.GetValue(configName)); Assert.IsType <InvalidConfigNameException>(exception); } }
public void RemoveValue(string configName) { if (File.Exists(CliConfigFile)) { File.Delete(CliConfigFile); } var config = new CliConfig(_logger.Object); config.RemoveValue(configName); if (configName == "ApiUrl") { Assert.Equal("https://localhost", config.ApiUrl); } else { Assert.False(config.Configs.ContainsKey(configName)); } }
public void GetValue_WithDefault(string configName) { if (File.Exists(CliConfigFile)) { File.Delete(CliConfigFile); } var config = new CliConfig(_logger.Object); var value = config.GetValueOrDefault(configName, "DefaultValue"); if (configName == "ApiUrl") { Assert.Equal("https://localhost", value); } else { Assert.Equal("DefaultValue", value); } }
public HelpPrinterInt(CliConfig cliConfig, CommandConfig commandConfig) { _cliConfig = cliConfig; _commandConfig = commandConfig; }
//c'tors: //- //public methods: public void MainLoop(string[] args) { Console.SetBufferSize(2 * Console.LargestWindowWidth, 10 * Console.LargestWindowHeight); //not really sure about this line (we may still need double buffering and/or custom-flush) do { if (conf == null && sim == null) { conf = CreateCliConfig(); sim = CreateCellSimulation(); FillWithRandoms(rand); conf.App = this; SetWindowSize(); Rerender(); } if (conf == null) { conf = CreateCliConfig(); conf.App = this; SetWindowSize(); Rerender(); } if (sim == null) { sim = CreateCellSimulation(); FillWithRandoms(rand); SetWindowSize(); Rerender(); } //Console.WriteLine(); Console.ForegroundColor = conf.PromptColor; Console.Write(conf.PromptText); Console.ForegroundColor = conf.UserColor; Console.CursorVisible = true; command = Console.ReadLine(); Console.CursorVisible = false; if (command.Equals("default")) { conf = null; sim = null; } else if (command.Equals("conf")) { conf.SuppressRebuildReformat = true; //we may have several changes, will react later ItemsChanger <CliConfig> changer = new ItemsChanger <CliConfig>(conf, conf); changer.MainLoop(); SetWindowSize(); confStr1 = null; //AlifeText may have changed confStr2 = null; //HalfAlifeText may have changed confStr3 = null; //DeadText may have changed conf.SuppressRebuildReformat = false; //now let the console app react to all changes feedback = "Left configuration management."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("settings")) { sim.Settings.SuppressUpdates = true; //we may have several changes, will react later ItemsChanger <SimulationSettings> changer = new ItemsChanger <SimulationSettings>(sim.Settings, conf); changer.MainLoop(); SetWindowSize(); sim.Settings.SuppressUpdates = false; //now let the sim react to all our changes feedback = "Left settings management."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("sim")) { //TODO //display a list with all our simulation types //let user start a new simulation of any of those types } else if (command.StartsWith("save conf")) { //TODO } else if (command.StartsWith("save sim")) { //TODO } else if (command.StartsWith("load conf")) { //TODO } else if (command.StartsWith("load sim")) { //TODO } else if (command.StartsWith("random")) { string[] split = command.Split(sep); Random r; bool success; int seed; if (split.Length < 2) { r = rand; success = true; } else { success = int.TryParse(split[1], out seed); r = new Random(seed); } if (success) { FillWithRandoms(r); feedback = "Filled cells with random numbers."; feedbackType = FeedbackType.OKAY; } else { feedback = "Could not parse seed value: " + split[1]; feedbackType = FeedbackType.ERROR; } } else if (command.Equals("run")) { Rerender(); feedback = "Started automatic run of simulations."; feedbackType = FeedbackType.OKAY; AutoSimLoop(); feedback = "Completed automatic run of simulations."; feedbackType = FeedbackType.OKAY; } else if (command.StartsWith("size")) { string[] split = command.Split(sep); if (split.Length > 2) { int newX, newY = -1; bool success = int.TryParse(split[2], out newX); if (split.Length > 3) { success = success && int.TryParse(split[3], out newY); } if (success && newX > 0 && split.Length < 4 || newY > 0) { sim.Settings.SuppressUpdates = true; sim.Settings.SizeX = newX; sim.Settings.SizeY = split.Length > 3 ? newY : newX; sim.Settings.SuppressUpdates = false; feedback = "Set x and y to desired values."; feedbackType = FeedbackType.OKAY; } else { feedback = "Could not set to new size."; feedbackType = FeedbackType.ERROR; } } } else if (command.StartsWith("param")) { string[] split = command.Split(sep); if (split.Length > 3) { int n = -1; decimal v = -1m; bool success = int.TryParse(split[2], out n); success = success && decimal.TryParse(split[3], out v); if (success) { SimulationParameter param = null; if (n == 1) { param = sim.Param1; } if (n == 2) { param = sim.Param2; } //TODO: param = sim.GetParam(n); //TODO: int maxN = sim.GetNumParams(); if (param != null) { if (v >= param.Min && v <= param.Max) { param.Current = v; feedback = $"Set param {n} to value {v}."; feedbackType = FeedbackType.OKAY; } else { feedback = $"Value {v} not in allowed interval [{param.Min}..{param.Max}]!"; feedbackType = FeedbackType.ERROR; } } else { feedback = $"Unknown param [{n}] (try a different index!)."; feedbackType = FeedbackType.ERROR; } } else { feedback = "Could not set param (incorrect number?)."; feedbackType = FeedbackType.ERROR; } } } else if (command.StartsWith("set")) { int x, y = -1; decimal val = 1m; string[] split = command.Split(sep); if (split.Length > 2) { bool success = int.TryParse(split[1], out x); success = success && int.TryParse(split[2], out y); if (split.Length > 3) { success = decimal.TryParse(split[3], out val); } if (success && BoundsCheck(x, y)) { sim.SetCellValue(x, y, val); feedback = "Set value for cell."; feedbackType = FeedbackType.OKAY; } else { feedback = "Could not set to new value."; feedbackType = FeedbackType.ERROR; } } } else if (command.Equals("fill")) { FillWith(1m); feedback = "Set every cell to 1 (100%)."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("clear")) { FillWith(0m); feedback = "Set every cell to 0 (0%)."; feedbackType = FeedbackType.OKAY; } else if (command.StartsWith("clear")) { int x, y = -1; string[] split = command.Split(sep); if (split.Length > 2) { bool success = int.TryParse(split[1], out x); success = success && int.TryParse(split[2], out y); if (success && BoundsCheck(x, y)) { sim.SetCellValue(x, y, 0m); feedback = "Set cell value to 0 (0%)."; feedbackType = FeedbackType.OKAY; } else { feedback = "Could not set cell value to 0 (0%)."; feedbackType = FeedbackType.ERROR; } } } else if (command.Equals("next")) { sim.CalculateNextGen(); feedback = "Calculated next generation."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("current?")) { feedback = "Current generation is " + sim.CurrentGen + "."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("oldest?")) { feedback = "Oldest generation is " + sim.OldestGen + "."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("num?")) { feedback = "Number of generations: " + sim.NumGens + "."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("oldest")) { sim.GoToOldestGen(); feedback = "Went to oldest generation."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("back")) { sim.GoBackOneGen(); feedback = "Went back one generation."; feedbackType = FeedbackType.OKAY; } else if (command.Equals("zero")) { sim.RelabelCurrentAsZero(); feedback = "Relabled current generation as zero, deleted others."; feedbackType = FeedbackType.OKAY; } else if (command.StartsWith("go to")) { string[] split = command.Split(sep); if (split.Length > 2) { int n; bool success = int.TryParse(split[2], out n); if (success && n >= 0) { bool okay = sim.GoToGen(n, this); if (okay) { feedback = "Went to generation " + n + "."; feedbackType = FeedbackType.OKAY; } else { feedback = "Tried to go to generation " + n + " but failed."; feedbackType = FeedbackType.ERROR; } } else { feedback = "Could not go to that generation."; feedbackType = FeedbackType.ERROR; } } } else if (command.Equals("info")) { ShowSimInfo(); } else if (command.Equals("help") || command.Equals("?")) { ShowHelp(); } else if (command.Equals("max")) { feedback = "max size: " + GetMaxSimSize(); feedbackType = FeedbackType.OKAY; } else { feedback = "Unknown command: \"" + command + "\""; feedbackType = FeedbackType.ERROR; } Rerender(); }while (!command.Equals("exit")); }
/// <inheritdoc /> public string PrintHelp(CliConfig cliConfig, CommandConfig commandConfig) { return(new HelpPrinterInt(cliConfig, commandConfig).Print()); }