// ******************************************************************************** /// <summary> /// Display the help text on the console. /// </summary> /// <param name="console">Console used for writing.</param> /// <param name="data">The help data for rendering.</param> /// <created>GJK,23/10/2020</created> /// <changed>GJK,23/10/2020</changed> // ******************************************************************************** public static void Show(this IStdOut console, HelpData data) { if (data == null) { throw new ArgumentNullException(nameof(data), "Expect helpdata to display."); } int longestKeyLenght = 5; foreach (var item in data.Parameters) { if (item.Key.Length > longestKeyLenght) { longestKeyLenght = item.Key.Length + 2; } } console?.WriteLine("Help:"); if (!string.IsNullOrEmpty(data.Description)) { console?.WriteLine(data.Description); } console?.WriteLine("Key{0}Description", GetSpaces(longestKeyLenght - 3)); foreach (var item in data.Parameters) { console.WriteLine("{0}{1}{2}", item.Key, GetSpaces(longestKeyLenght - item.Key.Length), item.Description); } console?.WriteLine("Arguments are parsed as"); console?.WriteLine("Key:Value"); }
private void WriteLine([CanBeNull] string line) { if (line != null) { _stdOut.WriteLine(_tabsString + line + "\x001B[0;37;40m"); } }
public void WriteLine(params Text[] text) { var sb = new StringBuilder(); var lastColor = Color.Default; foreach (var item in text) { if (item.Color != lastColor) { lastColor = item.Color; sb.Append($"\x001B[{_colorTheme.GetAnsiColor(lastColor)}m"); } sb.Append(item.Value); } sb.Append($"\x001B[{_colorTheme.GetAnsiColor(Color.Default)}m"); _stdOut.WriteLine(sb.ToString()); }
/// <summary>Registers an intent to use the <c>Description</c> attribute on test methods.</summary> /// <remarks>This causes MsTest descriptions to be written to the test log (.trx-file).</remarks> public static ContextBuilder RegisterDescription(this ContextBuilder theContextBuilder, string testName, Assembly assembly = null, Type extraType = null, IStdOut stdOut = null) { assembly ??= Assembly.GetCallingAssembly(); stdOut ??= DefaultStdOut; Type[] types = BothTypes(extraType, typeof(TestDescriptionAttribute)); MethodInfo[] allMethodsForTest = GetMethodsForTest(testName, assembly); foreach (Type type in types) { foreach (MethodInfo methodInfo in allMethodsForTest) { foreach (IAttributeValue attribute in methodInfo.GetCustomAttributes(type, false)) { stdOut.WriteLine($@"{TestDescriptionAttribute.Prefix}{attribute.Value}{TestDescriptionAttribute.Postfix}"); } } } return(theContextBuilder); }
// ******************************************************************************** /// <summary> /// Parse and handle the arguments. /// </summary> /// <param name="configAction">The command line parameter configuration.</param> /// <param name="stdOut">Output channel (default null)</param> /// <param name="args">arguments</param> /// <returns>A list with unrecognized command line arguments.</returns> /// <created>GJK,23/10/2020</created> /// <changed>GJK,23/10/2020</changed> // ******************************************************************************** public static List <string> ParseArgs(Action <CmdArgConfigurator> configAction, IStdOut stdOut = null, params string[] args) { if (configAction == null) { throw new ArgumentNullException(nameof(configAction), "Expected a method for configurating the command arguments."); } var config = new CmdArgConfiguration(); var configurator = new CmdArgConfigurator(config); try { configAction(configurator); } catch (Exception e) { throw new ArgumentException("Exception thrown in config action passed to CmdArgParser.Parse method. See inner exception for more details", e); } var extraArgs = new List <string>(); if (args.Length > 0) { foreach (var argument in args) { var argumentLC = argument.ToLower(); var argParsed = false; foreach (var parameter in config.Parameters) { foreach (var key in parameter.Keys) { if (argumentLC.StartsWith(key)) { var rightSide = argument.Substring(key.Length); if (string.IsNullOrEmpty(rightSide)) { parameter.Handler(string.Empty); argParsed = true; } else if (rightSide.StartsWith(":")) { var value = rightSide.Substring(1); parameter.Handler(value); argParsed = true; } } } } if (!argParsed) { extraArgs.Add(argument); } } } if (extraArgs.Count > 0 && config.ShowHelpOnExtraArguments) { stdOut?.WriteLine("Unrecognized arguments: "); foreach (var extraArg in extraArgs) { stdOut?.WriteLine("Key: {0}", extraArg); } } return(extraArgs); }