private string?GetValueForOption(string parameterName, OptionResult optionResult) { //if default value is used, no need to return it - it will be populated in template engine edge instead. if (optionResult.IsImplicit) { return(null); } var optionValue = optionResult.GetValueOrDefault(); if (optionValue == null) { return(null); } if (!Template.CliParameters.TryGetValue(parameterName, out CliTemplateParameter? parameter)) { throw new InvalidOperationException($"Parameter {parameterName} is not defined for {Template.Identity}."); } if (parameter.Type == ParameterType.Hex && optionResult.Option.ValueType == typeof(long)) { var intValue = (long)optionValue; return($"0x{intValue.ToString("X")}"); } return(optionValue.ToString()); }
private static string OutputValidiator(OptionResult optionResult) { ArgumentNullException.ThrowIfNull(optionResult); var outPath = optionResult?.GetValueOrDefault <string>(); if (outPath is null) { return(default);
public static string Validator <T>(OptionResult result, Func <T, bool> validator) { var value = result.GetValueOrDefault <T>(); if (!validator(value)) { return($"Option {result.Token?.Value} cannot be set to {value}"); } return(string.Empty); }
static string ValidateTelemetry(CommandResult result) { const string nameMessage = "--telemetry-name must be 3 - 24 characters [a-z][0-9]"; const string keyMessage = "--telemetry-key must be 36 characters 8[hex]-4[hex]-4[hex]-12[hex]"; const string bothMessage = "--telemetry-name and --telemetry-key must both be specified or omitted"; OptionResult name = result.Children.FirstOrDefault(c => c.Symbol.Name == "telemetry-name") as OptionResult; OptionResult key = result.Children.FirstOrDefault(c => c.Symbol.Name == "telemetry-key") as OptionResult; // neither name or key is set if (name == null && key == null) { return(string.Empty); } // both name and key must be set if (name == null || key == null) { return(bothMessage); } // get the values string n = name.GetValueOrDefault <string>(); string k = key.GetValueOrDefault <string>(); // both are null is OK if (n == null && k == null) { return(string.Empty); } // both must be set if (n == null || k == null) { return(bothMessage); } // name must be 3-24 characters in length if (n.Length < 3 || n.Length > 24) { return(nameMessage); } // key must be 36 characters if (k.Length != 36) { return(keyMessage); } return(string.Empty); }
// validate --duration and --random based on --run-loop private static string ValidateRunLoopDependencies(CommandResult result) { OptionResult runLoopRes = result.Children.FirstOrDefault(c => c.Symbol.Name == "run-loop") as OptionResult; OptionResult durationRes = result.Children.FirstOrDefault(c => c.Symbol.Name == "duration") as OptionResult; OptionResult randomRes = result.Children.FirstOrDefault(c => c.Symbol.Name == "random") as OptionResult; bool runLoop = runLoopRes.GetValueOrDefault <bool>(); int? duration = durationRes.GetValueOrDefault <int?>(); bool random = randomRes.GetValueOrDefault <bool>(); if (duration != null && duration > 0 && !runLoop) { return("--run-loop must be true to use --duration"); } if (random && !runLoop) { return("--run-loop must be true to use --random"); } return(string.Empty); }
public void The_default_value_of_an_option_with_no_arguments_is_true() { var command = new OptionResult(new Option("-x"), new Token("-x", TokenType.Option)); command.GetValueOrDefault().Should().Be(null); }
private static string?DirectoryValidator(OptionResult r) { var dir = r.GetValueOrDefault <DirectoryInfo>(); return(dir.Exists ? null : $"Directory {dir} doesn't exist!"); }