in openapi-diff/src/core/OpenApiDiff/HelpGenerator.cs [68:129]
public static string Generate(string template, Settings settings)
{
if (String.IsNullOrEmpty(template))
{
throw new ArgumentNullException("template");
}
// Reflect over properties in Settings to get documentation content
var parameters = new List<Tuple<string, SettingsInfoAttribute>>();
foreach (PropertyInfo property in typeof(Settings).GetProperties())
{
var doc = property.GetCustomAttributes<SettingsInfoAttribute>().FirstOrDefault();
if (doc != null)
{
parameters.Add(new Tuple<string, SettingsInfoAttribute>(property.Name, doc));
}
}
// Generate usage syntax
var syntaxSection = new StringBuilder("dotnet OpenApiDiff.dll ");
foreach (var parameter in parameters.OrderBy(t => t.Item1).OrderByDescending(t => t.Item2.IsRequired))
{
if (parameter.Item2.IsRequired)
{
syntaxSection.AppendFormat("-{0} <value> ", parameter.Item1);
}
else
{
syntaxSection.AppendFormat("[-{0} <value>] ", parameter.Item1);
}
}
// Generate parameters section
var parametersSection = new StringBuilder();
const string parametersPattern = @"\$parameters-start\$(.+)\$parameters-end\$";
var parameterTemplate = Regex.Match(template, parametersPattern, RegexOptions.Singleline).Groups[1].Value.Trim();
GetParametersInfo(typeof(Settings).GetProperties().OrderBy(p => p.Name), parametersSection, parameterTemplate,
"$parameter$", "$parameter-desc$");
// Generate examples section.
var examplesSection = new StringBuilder();
const string examplesPattern = @"\$examples-start\$(.+)\$examples-end\$";
var exampleTemplate = Regex.Match(template, examplesPattern, RegexOptions.Singleline).Groups[1].Value.Trim() + Environment.NewLine;
foreach (HelpExample example in Examples)
{
examplesSection.AppendLine(" " + exampleTemplate.
Replace("$example$", example.Example).
Replace("$example-desc$", example.Description));
}
// Process template replacing all major sections.
template = template.
Replace("$version$", "0.1.0").
Replace("$syntax$", syntaxSection.ToString());
template = Regex.Replace(template, parametersPattern, parametersSection.ToString(), RegexOptions.Singleline);
template = Regex.Replace(template, examplesPattern, examplesSection.ToString(), RegexOptions.Singleline);
return template;
}