BadScript 2
Loading...
Searching...
No Matches
MultiLineTextAttribute.cs
Go to the documentation of this file.
1// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
2
3using System;
4using System.Linq;
5using System.Text;
6
7namespace CommandLine.Text
8{
12 public abstract class MultilineTextAttribute : Attribute
13 {
19 protected MultilineTextAttribute(string line1)
20 : this(line1, string.Empty, string.Empty, string.Empty, string.Empty) { }
21
28 protected MultilineTextAttribute(string line1, string line2)
29 : this(line1, line2, string.Empty, string.Empty, string.Empty) { }
30
38 protected MultilineTextAttribute(string line1, string line2, string line3)
39 : this(line1, line2, line3, string.Empty, string.Empty) { }
40
49 protected MultilineTextAttribute(string line1, string line2, string line3, string line4)
50 : this(line1, line2, line3, line4, string.Empty) { }
51
61 protected MultilineTextAttribute(string line1, string line2, string line3, string line4, string line5)
62 {
63 if (line1 == null)
64 {
65 throw new ArgumentException("line1");
66 }
67
68 if (line2 == null)
69 {
70 throw new ArgumentException("line2");
71 }
72
73 if (line3 == null)
74 {
75 throw new ArgumentException("line3");
76 }
77
78 if (line4 == null)
79 {
80 throw new ArgumentException("line4");
81 }
82
83 if (line5 == null)
84 {
85 throw new ArgumentException("line5");
86 }
87
88 Line1 = line1;
89 Line2 = line2;
90 Line3 = line3;
91 Line4 = line4;
92 Line5 = line5;
93 }
94
99 public virtual string Value
100 {
101 get
102 {
103 StringBuilder value = new StringBuilder(string.Empty);
104 string[] strArray = { Line1, Line2, Line3, Line4, Line5 };
105
106 for (int i = 0; i < GetLastLineWithText(strArray); i++)
107 {
108 value.AppendLine(strArray[i]);
109 }
110
111 return value.ToString();
112 }
113 }
114
118 public string Line1 { get; }
119
123 public string Line2 { get; }
124
128 public string Line3 { get; }
129
133 public string Line4 { get; }
134
138 public string Line5 { get; }
139
140 internal HelpText AddToHelpText(HelpText helpText, Func<string, HelpText> func)
141 {
142 string[] strArray = { Line1, Line2, Line3, Line4, Line5 };
143
144 return strArray.Take(GetLastLineWithText(strArray))
145 .Aggregate(helpText, (current, line) => func(line));
146 }
147
148 internal HelpText AddToHelpText(HelpText helpText, bool before)
149 {
150 // before flag only distinguishes which action is called,
151 // so refactor common code and call with appropriate func
152 return before
153 ? AddToHelpText(helpText, helpText.AddPreOptionsLine)
154 : AddToHelpText(helpText, helpText.AddPostOptionsLine);
155 }
156
164 protected virtual int GetLastLineWithText(string[] value)
165 {
166 int index = Array.FindLastIndex(value, str => !string.IsNullOrEmpty(str));
167
168 // remember FindLastIndex returns zero-based index
169 return index + 1;
170 }
171 }
172}
HelpText AddPreOptionsLine(string value)
Adds a text line after copyright and before options usage strings.
Definition HelpText.cs:494
HelpText AddPostOptionsLine(string value)
Adds a text line at the bottom, after options usage string.
Definition HelpText.cs:505
Provides base properties for creating an attribute, used to define multiple lines of text.
HelpText AddToHelpText(HelpText helpText, bool before)
string Line4
Gets the fourth line of text.
string Line2
Gets the second line of text.
virtual string Value
Gets the all non-blank lines as string.
MultilineTextAttribute(string line1, string line2, string line3, string line4)
Initializes a new instance of the MultilineTextAttribute class. Used in type using four lines of text...
MultilineTextAttribute(string line1, string line2, string line3)
Initializes a new instance of the MultilineTextAttribute class. Used in type using three lines of tex...
virtual int GetLastLineWithText(string[] value)
Returns the last line with text. Preserves blank lines if user intended by skipping a line.
MultilineTextAttribute(string line1, string line2)
Initializes a new instance of the MultilineTextAttribute class. Used in type using two lines of text.
string Line1
Gets the first line of text.
string Line5
Gets the fifth line of text.
MultilineTextAttribute(string line1, string line2, string line3, string line4, string line5)
Initializes a new instance of the MultilineTextAttribute class. Used in type using five lines of text...
MultilineTextAttribute(string line1)
Initializes a new instance of the MultilineTextAttribute class. Used in derived type using one line o...
HelpText AddToHelpText(HelpText helpText, Func< string, HelpText > func)