예제 #1
0
        private void AddOption(string requiredWord, int maxLength, BaseOptionAttribute option, int widthOfHelpText, bool fireEvent = true)
        {
            this._optionsHelp.Append("  ");
            var optionName = new StringBuilder(maxLength);
            if (option.HasShortName)
            {
                if (this._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 (this._addDashesToOption)
                {
                    optionName.Append("--");
                }

                optionName.AppendFormat("{0}", option.LongName);

                if (option.HasMetaValue)
                {
                    optionName.AppendFormat("={0}", option.MetaValue);
                }
            }

            this._optionsHelp.Append(optionName.Length < maxLength ?
                optionName.ToString().PadRight(maxLength) :
                optionName.ToString());

            this._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;
                    var words = option.HelpText.Split(new[] { ' ' });
                    for (int i = 0; i < words.Length; i++)
                    {
                        if (words[i].Length < (widthOfHelpText - wordBuffer))
                        {
                            this._optionsHelp.Append(words[i]);
                            wordBuffer += words[i].Length;
                            if ((widthOfHelpText - wordBuffer) > 1 && i != words.Length - 1)
                            {
                                this._optionsHelp.Append(" ");
                                wordBuffer++;
                            }
                        }
                        else if (words[i].Length >= widthOfHelpText && wordBuffer == 0)
                        {
                            this._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)
                    {
                        this._optionsHelp.Append(Environment.NewLine);
                        this._optionsHelp.Append(new string(' ', maxLength + 6));
                    }
                }
                while (option.HelpText.Length > widthOfHelpText);
            }

            this._optionsHelp.Append(option.HelpText);
            this._optionsHelp.Append(Environment.NewLine);
            if (_additionalNewLineAfterOption)
            {
                this._optionsHelp.Append(Environment.NewLine);
            }
        }
예제 #2
0
        /// <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);
            }
        }