private void PrintVerbHelpText(Type verbType, string?commandLineVerb) { var verbAttribute = verbType.GetCustomAttribute <VerbAttribute>(); if (verbAttribute != null) { var verb = NamingHelper.MakeKebabCase(verbAttribute.VerbName); Console.Write(_localizableStrings.UsageHeader); Console.WriteLine($"{verb} [options]"); Console.WriteLine(); var optionInfoList = verbType.GetProperties(BindingFlags.Instance | BindingFlags.Public) .Select(x => NameDescription.FromPropertyInfo(x, _localizableStrings, _resourceManager)) .OfType <NameDescription>() .ToList(); var columnLength = optionInfoList.Count == 0 ? 0 : optionInfoList.Max(x => x.Name.Length); if (optionInfoList.Count > 0) { Console.WriteLine(_localizableStrings.OptionsHeader); } foreach (var x in optionInfoList) { Console.Write(GetColumnString(x.Name, columnLength)); Console.WriteLine(x.Description); } } else if (!string.IsNullOrWhiteSpace(commandLineVerb)) { PrintUnknownVerbHelpText(commandLineVerb); } }
private void PrintHelpText(IReadOnlyList <Type> relatedTypes) { var selfAssembly = typeof(GnuFilter).Assembly; var verbInfoList = relatedTypes .Select(x => NameDescription.FromVerbType(x, _resourceManager)) .OfType <NameDescription>() .ToList(); var mergedOptionInfoList = relatedTypes .Where(x => !x.IsDefined(typeof(VerbAttribute))) .SelectMany(x => NameDescription.EnumerateFromVerbType(x, _localizableStrings, _resourceManager)).ToList(); var maxVerbTextLength = verbInfoList.Count == 0 ? 0 : verbInfoList.Max(x => x.Name.Length); var maxOptionTextLength = mergedOptionInfoList.Count == 0 ? 0 : mergedOptionInfoList.Max(x => x.Name.Length); var columnLength = Math.Max(maxOptionTextLength, maxVerbTextLength); columnLength = Math.Max(12, columnLength); Console.Write(_localizableStrings.UsageHeader); if (verbInfoList.Count > 0) { Console.WriteLine("[options] [command] [command-options] [arguments]"); } else { Console.WriteLine("[options] [arguments]"); } Console.WriteLine(); if (mergedOptionInfoList.Count > 0) { Console.WriteLine(_localizableStrings.OptionsHeader); } foreach (var x in mergedOptionInfoList) { Console.Write(GetColumnString(x.Name, columnLength)); Console.WriteLine(x.Description); } Console.WriteLine(); if (verbInfoList.Count > 0) { Console.WriteLine(_localizableStrings.CommandHeader); } foreach (var x in verbInfoList) { Console.Write(GetColumnString(x.Name, columnLength)); Console.WriteLine(x.Description); } }