#region License // // Copyright 2015-2013 Giacomo Stelluti Scala // // // Permission is hereby granted, free of charge, to any person obtaining a copy // of this software and associated documentation files (the "Software"), to deal // in the Software without restriction, including without limitation the rights // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell // copies of the Software, and to permit persons to whom the Software is // furnished to do so, subject to the following conditions: // // The above copyright notice and this permission notice shall be included in // all copies or substantial portions of the Software. // // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN // THE SOFTWARE. #endregion #region Using Directives using System; using System.Text; using CommandLine.Infrastructure; using CommandLine.Text; #endregion namespace CommandLine { /// /// Provides base properties for creating an attribute, used to define multiple lines of text. /// public abstract class MultilineTextAttribute : Attribute { private readonly string _line1; private readonly string _line2; private readonly string _line3; private readonly string _line4; private readonly string _line5; /// /// Initializes a new instance of the class. Used in derived type /// using one line of text. /// /// The first line of text. protected MultilineTextAttribute(string line1) { Assumes.NotNullOrEmpty(line1, "line1"); _line1 = line1; } /// /// Initializes a new instance of the class. Used in type /// using two lines of text. /// /// The first line of text. /// The second line of text. protected MultilineTextAttribute(string line1, string line2) : this(line1) { Assumes.NotNullOrEmpty(line2, "line2"); _line2 = line2; } /// /// Initializes a new instance of the class. Used in type /// using three lines of text. /// /// The first line of text. /// The second line of text. /// The third line of text. protected MultilineTextAttribute(string line1, string line2, string line3) : this(line1, line2) { Assumes.NotNullOrEmpty(line3, "line3"); _line3 = line3; } /// /// Initializes a new instance of the class. Used in type /// using four lines of text. /// /// The first line of text. /// The second line of text. /// The third line of text. /// The fourth line of text. protected MultilineTextAttribute(string line1, string line2, string line3, string line4) : this(line1, line2, line3) { Assumes.NotNullOrEmpty(line4, "line4"); _line4 = line4; } /// /// Initializes a new instance of the class. Used in type /// using five lines of text. /// /// The first line of text. /// The second line of text. /// The third line of text. /// The fourth line of text. /// The fifth line of text. protected MultilineTextAttribute(string line1, string line2, string line3, string line4, string line5) : this(line1, line2, line3, line4) { Assumes.NotNullOrEmpty(line5, "line5"); _line5 = line5; } /// /// Gets the all non-blank lines as string. /// /// A string of all non-blank lines. public virtual string Value { get { var value = new StringBuilder(string.Empty); var strArray = new[] { _line1, _line2, _line3, _line4, _line5 }; for (int i = 0; i < GetLastLineWithText(strArray); i++) { value.AppendLine(strArray[i]); } return value.ToString(); } } /// /// Gets the first line of text. /// public string Line1 { get { return _line1; } } /// /// Gets the second line of text. /// public string Line2 { get { return _line2; } } /// /// Gets third line of text. /// public string Line3 { get { return _line3; } } /// /// Gets the fourth line of text. /// public string Line4 { get { return _line4; } } /// /// Gets the fifth line of text. /// public string Line5 { get { return _line5; } } internal void AddToHelpText(Action action) { var strArray = new[] { _line1, _line2, _line3, _line4, _line5 }; Array.ForEach( strArray, line => { if (!string.IsNullOrEmpty(line)) { action(line); } }); } internal void AddToHelpText(HelpText helpText, bool before) { // before flag only distinguishes which action is called, // so refactor common code and call with appropriate action if (before) { AddToHelpText(helpText.AddPreOptionsLine); } else { AddToHelpText(helpText.AddPostOptionsLine); } } /// /// Returns the last line with text. Preserves blank lines if user intended by skipping a line. /// /// The last index of line of the non-blank line. /// /// The string array to process. protected virtual int GetLastLineWithText(string[] value) { int index = Array.FindLastIndex(value, str => !string.IsNullOrEmpty(str)); // remember FindLastIndex returns zero-based index return index + 1; } } }