/// <summary> /// The OnFormatOptionHelpText method also allows derived classes to handle the event without attaching a delegate. /// This is the preferred technique for handling the event in a derived class. /// </summary> /// <param name="e">Data for the <see cref="FormatOptionHelpText" /> event.</param> protected virtual void OnFormatOptionHelpText(FormatOptionHelpTextEventArgs e) { EventHandler <FormatOptionHelpTextEventArgs> handler = FormatOptionHelpText; if (handler != null) { handler(this, e); } }
private void AddOption(string requiredWord, int maxLength, BaseOptionAttribute option, int widthOfHelpText, bool fireEvent = true) { _optionsHelp.Append(" "); var optionName = new StringBuilder(maxLength); if (option.HasShortName) { if (_addDashesToOption) { optionName.Append('-'); } optionName.AppendFormat("{0}", option.ShortName); if (option.HasMetaValue) { optionName.AppendFormat(" {0}", option.MetaValue); } if (option.HasLongName) { optionName.Append(", "); } } if (option.HasLongName) { if (_addDashesToOption) { optionName.Append("--"); } optionName.AppendFormat("{0}", option.LongName); if (option.HasMetaValue) { optionName.AppendFormat("={0}", option.MetaValue); } } _optionsHelp.Append(optionName.Length < maxLength ? optionName.ToString().PadRight(maxLength) : optionName.ToString()); _optionsHelp.Append(" "); if (option.HasDefaultValue) { option.HelpText = "(Default: {0}) ".FormatLocal(option.DefaultValue) + option.HelpText; } if (option.Required) { option.HelpText = "{0} ".FormatInvariant(requiredWord) + option.HelpText; } if (fireEvent) { var e = new FormatOptionHelpTextEventArgs(option); OnFormatOptionHelpText(e); option.HelpText = e.Option.HelpText; } if (!string.IsNullOrEmpty(option.HelpText)) { do { int wordBuffer = 0; string[] words = option.HelpText.Split(new[] { ' ' }); for (int 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) { _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); } }