private void AddOption(string requiredWord, int maxLength, BaseOptionAttribute option, int widthOfHelpText) { OptionsHelp.Append(" "); var optionName = new StringBuilder(maxLength); if (option.HasShortName) { if (AddDashesToOption) { optionName.Append('-'); } optionName.AppendFormat("{0}", option.ShortName); if (option.HasLongName) { optionName.Append(", "); } } if (option.HasLongName) { if (AddDashesToOption) { optionName.Append("--"); } optionName.AppendFormat("{0}", option.LongName); } OptionsHelp.Append(optionName.Length < maxLength ? optionName.ToString().PadRight(maxLength) : optionName.ToString()); OptionsHelp.Append(" "); if (option.Required) { option.HelpText = string.Format(CultureInfo.InvariantCulture, "{0} ", requiredWord) + option.HelpText; } var e = new FormatOptionHelpTextEventArgs(option); OnFormatOptionHelpText(e); option.HelpText = e.Option.HelpText; if (!string.IsNullOrWhiteSpace(option.HelpText)) { do { var wordBuffer = 0; var words = option.HelpText.Split(new[] { ' ' }); for (var i = 0; i < words.Length; i++) { if (words[i].Length < (widthOfHelpText - wordBuffer)) { OptionsHelp.Append(words[i]); wordBuffer += words[i].Length; if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1) { OptionsHelp.Append(" "); wordBuffer++; } } else if (words[i].Length >= widthOfHelpText && wordBuffer == 0) { OptionsHelp.Append(words[i].Substring( 0, widthOfHelpText )); wordBuffer = widthOfHelpText; break; } else { break; } } option.HelpText = option.HelpText.Substring(Math.Min( wordBuffer, option.HelpText.Length )) .Trim(); if (option.HelpText.Length <= 0) continue; OptionsHelp.Append(Environment.NewLine); OptionsHelp.Append(new string(' ', maxLength + 6)); } while (option.HelpText.Length > widthOfHelpText); } OptionsHelp.Append(option.HelpText); OptionsHelp.Append(Environment.NewLine); if (AdditionalNewLineAfterOption) OptionsHelp.Append(Environment.NewLine); }
/// <summary> /// Initializes a new instance of the <see cref="FormatOptionHelpTextEventArgs"/> class. /// </summary> /// <param name="option">Option to format.</param> public FormatOptionHelpTextEventArgs(BaseOptionAttribute option) { Option = option; }