// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using System;
using System.Text;
using System.Linq;
namespace CommandLine.Text
{
///
/// 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)
: this(line1, string.Empty, string.Empty, string.Empty, string.Empty)
{
}
///
/// 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, line2, string.Empty, string.Empty, string.Empty)
{
}
///
/// 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, line3, string.Empty, string.Empty)
{
}
///
/// 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, line4, string.Empty)
{
}
///
/// 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)
{
if (line1 == null) throw new ArgumentException("line1");
if (line2 == null) throw new ArgumentException("line2");
if (line3 == null) throw new ArgumentException("line3");
if (line4 == null) throw new ArgumentException("line4");
if (line5 == null) throw new ArgumentException("line5");
this.line1 = line1;
this.line2 = line2;
this.line3 = line3;
this.line4 = line4;
this.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 (var 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 HelpText AddToHelpText(HelpText helpText, Func func)
{
var strArray = new[] { line1, line2, line3, line4, line5 };
return strArray.Take(GetLastLineWithText(strArray)).Aggregate(helpText, (current, line) => func(line));
}
internal HelpText AddToHelpText(HelpText helpText, bool before)
{
// before flag only distinguishes which action is called,
// so refactor common code and call with appropriate func
return before
? AddToHelpText(helpText, helpText.AddPreOptionsLine)
: AddToHelpText(helpText, 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)
{
var index = Array.FindLastIndex(value, str => !string.IsNullOrEmpty(str));
// remember FindLastIndex returns zero-based index
return index + 1;
}
}
}