BadScript 2
Loading...
Searching...
No Matches
SentenceBuilder.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.Collections.Generic;
5using System.Linq;
6using System.Text;
7
9
10namespace CommandLine.Text
11{
16 public abstract class SentenceBuilder
17 {
21 public static Func<SentenceBuilder> Factory { get; set; } = () => new DefaultSentenceBuilder();
22
26 public abstract Func<string> RequiredWord { get; }
27
31 public abstract Func<string> OptionGroupWord { get; }
32
36 public abstract Func<string> ErrorsHeadingText { get; }
37
41 public abstract Func<string> UsageHeadingText { get; }
42
51 public abstract Func<bool, string> HelpCommandText { get; }
52
61 public abstract Func<bool, string> VersionCommandText { get; }
62
67 public abstract Func<Error, string> FormatError { get; }
68
73 public abstract Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutuallyExclusiveSetErrors { get; }
74
79 public static SentenceBuilder Create()
80 {
81 return Factory();
82 }
83
84#region Nested type: DefaultSentenceBuilder
85
87 {
88 public override Func<string> RequiredWord
89 {
90 get { return () => "Required."; }
91 }
92
93 public override Func<string> ErrorsHeadingText
94 {
95 get { return () => "ERROR(S):"; }
96 }
97
98 public override Func<string> UsageHeadingText
99 {
100 get { return () => "USAGE:"; }
101 }
102
103 public override Func<string> OptionGroupWord
104 {
105 get { return () => "Group"; }
106 }
107
108 public override Func<bool, string> HelpCommandText
109 {
110 get
111 {
112 return isOption => isOption
113 ? "Display this help screen."
114 : "Display more information on a specific command.";
115 }
116 }
117
118 public override Func<bool, string> VersionCommandText
119 {
120 get { return _ => "Display version information."; }
121 }
122
123 public override Func<Error, string> FormatError
124 {
125 get
126 {
127 return error =>
128 {
129 switch (error.Tag)
130 {
131 case ErrorType.BadFormatTokenError:
132 return "Token '".JoinTo(((BadFormatTokenError)error).Token, "' is not recognized.");
133 case ErrorType.MissingValueOptionError:
134 return "Option '".JoinTo(((MissingValueOptionError)error).NameInfo.NameText,
135 "' has no value."
136 );
137 case ErrorType.UnknownOptionError:
138 return "Option '".JoinTo(((UnknownOptionError)error).Token, "' is unknown.");
139 case ErrorType.MissingRequiredOptionError:
141
142 return errMisssing.NameInfo.Equals(NameInfo.EmptyName)
143 ? "A required value not bound to option name is missing."
144 : "Required option '".JoinTo(errMisssing.NameInfo.NameText, "' is missing.");
145 case ErrorType.BadFormatConversionError:
147
148 return badFormat.NameInfo.Equals(NameInfo.EmptyName)
149 ? "A value not bound to option name is defined with a bad format."
150 : "Option '".JoinTo(badFormat.NameInfo.NameText,
151 "' is defined with a bad format."
152 );
153 case ErrorType.SequenceOutOfRangeError:
155
156 return seqOutRange.NameInfo.Equals(NameInfo.EmptyName)
157 ? "A sequence value not bound to option name is defined with fewer items than required."
158 : "A sequence option '".JoinTo(seqOutRange.NameInfo.NameText,
159 "' is defined with fewer or more items than required."
160 );
161 case ErrorType.BadVerbSelectedError:
162 return "Verb '".JoinTo(((BadVerbSelectedError)error).Token, "' is not recognized.");
163 case ErrorType.NoVerbSelectedError:
164 return "No verb selected.";
165 case ErrorType.RepeatedOptionError:
166 return "Option '".JoinTo(((RepeatedOptionError)error).NameInfo.NameText,
167 "' is defined multiple times."
168 );
169 case ErrorType.SetValueExceptionError:
170 SetValueExceptionError setValueError = (SetValueExceptionError)error;
171
172 return "Error setting value to option '".JoinTo(setValueError.NameInfo.NameText,
173 "': ",
174 setValueError.Exception.Message
175 );
176 case ErrorType.MissingGroupOptionError:
177 MissingGroupOptionError missingGroupOptionError = (MissingGroupOptionError)error;
178
179 return "At least one option from group '".JoinTo(missingGroupOptionError.Group,
180 "' (",
181 string.Join(", ", missingGroupOptionError.Names.Select(n => n.NameText)),
182 ") is required."
183 );
184 case ErrorType.GroupOptionAmbiguityError:
185 GroupOptionAmbiguityError groupOptionAmbiguityError = (GroupOptionAmbiguityError)error;
186
187 return "Both SetName and Group are not allowed in option: ("
188 .JoinTo(groupOptionAmbiguityError.Option.NameText, ")");
189 case ErrorType.MultipleDefaultVerbsError:
191 }
192
193 throw new InvalidOperationException();
194 };
195 }
196 }
197
198 public override Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutuallyExclusiveSetErrors
199 {
200 get
201 {
202 return errors =>
203 {
204 var bySet = from e in errors
205 group e by e.SetName
206 into g
207 select new { SetName = g.Key, Errors = g.ToList() };
208
209 string[] msgs = bySet.Select(set =>
210 {
211 string names = string.Join(string.Empty,
212 (from e in set.Errors
213 select "'".JoinTo(e.NameInfo.NameText,
214 "', "
215 )).ToArray()
216 );
217 int namesCount = set.Errors.Count();
218
219 string incompat = string.Join(string.Empty,
220 (from x in
221 (from s in bySet
222 where
223 !s
224 .SetName
225 .Equals(set
226 .SetName
227 )
228 from e in s
229 .Errors
230 select e)
231 .Distinct()
232 select "'".JoinTo(x.NameInfo
233 .NameText,
234 "', "
235 )).ToArray()
236 );
237
238 return
239 new StringBuilder("Option")
240 .AppendWhen(namesCount > 1, "s")
241 .Append(": ")
242 .Append(names.Substring(0, names.Length - 2))
243 .Append(' ')
244 .AppendIf(namesCount > 1, "are", "is")
245 .Append(" not compatible with: ")
246 .Append(incompat.Substring(0, incompat.Length - 2))
247 .Append('.')
248 .ToString();
249 }
250 )
251 .ToArray();
252
253 return string.Join(Environment.NewLine, msgs);
254 };
255 }
256 }
257 }
258
259#endregion
260 }
261}
Models an error generated when a value conversion fails.
Definition Error.cs:418
Models an error generated when an invalid token is detected.
Definition Error.cs:286
Models an error generated when an unknown verb is detected.
Definition Error.cs:445
IEnumerable< NameInfo > Names
Definition Error.cs:557
Models an error generated when a required option is required.
Definition Error.cs:392
Models an error generated when an option lacks its value.
Definition Error.cs:374
Models an error generated when multiple default verbs are defined.
Definition Error.cs:605
Models name information, used in CommandLine.Error instances.
Definition NameInfo.cs:11
string NameText
Gets a formatted text with unified name information.
Definition NameInfo.cs:47
static readonly NameInfo EmptyName
Represents an empty name information. Used when CommandLine.Error are tied to values,...
Definition NameInfo.cs:16
bool Equals(NameInfo other)
Returns a value that indicates whether the current instance and a specified CommandLine....
Definition NameInfo.cs:64
NameInfo NameInfo
Name information relative to this error instance.
Definition Error.cs:310
Models an error generated when an option is repeated two or more times.
Definition Error.cs:436
Models an error generated when a sequence value lacks elements.
Definition Error.cs:427
Models as error generated when exception is thrown at Property.SetValue.
Definition Error.cs:513
Exception Exception
The expection thrown from Property.SetValue.
Definition Error.cs:524
override Func< IEnumerable< MutuallyExclusiveSetError >, string > FormatMutuallyExclusiveSetErrors
Exposes standard delegates to provide a mean to customize part of help screen generation....
static SentenceBuilder Create()
Create instance of CommandLine.Text.SentenceBuilder,.
Func< string > RequiredWord
Gets a delegate that returns the word 'required'.
Func< string > UsageHeadingText
Gets a delegate that returns usage text block heading text.
Func< IEnumerable< MutuallyExclusiveSetError >, string > FormatMutuallyExclusiveSetErrors
Gets a delegate that handles mutually exclusive set errors formatting. The delegates must accept a se...
Func< string > ErrorsHeadingText
Gets a delegate that returns that errors block heading text.
Func< string > OptionGroupWord
Gets a delegate that returns the word 'group'.
static Func< SentenceBuilder > Factory
Factory to allow custom SentenceBuilder injection.
Func< bool, string > HelpCommandText
Get a delegate that returns the help text of help command. The delegates must accept a boolean that i...
Func< Error, string > FormatError
Gets a delegate that handles singular error formatting. The delegates must accept an Error and return...
Func< bool, string > VersionCommandText
Get a delegate that returns the help text of vesion command. The delegates must accept a boolean that...
Models an error generated when an unknown option is detected.
Definition Error.cs:383
ErrorType
Discriminator enumeration of CommandLine.Error derivates.
Definition Error.cs:13