-
Notifications
You must be signed in to change notification settings - Fork 481
/
Copy pathSentenceBuilder.cs
216 lines (192 loc) · 10.8 KB
/
SentenceBuilder.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright 2005-2015 Giacomo Stelluti Scala & Contributors. All rights reserved. See License.md in the project root for license information.
using CommandLine.Infrastructure;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CommandLine.Text
{
/// <summary>
/// Exposes standard delegates to provide a mean to customize part of help screen generation.
/// This type is consumed by <see cref="CommandLine.Text.HelpText"/>.
/// </summary>
public abstract class SentenceBuilder
{
/// <summary>
/// Create instance of <see cref="CommandLine.Text.SentenceBuilder"/>,
/// </summary>
/// <returns>The <see cref="CommandLine.Text.SentenceBuilder"/> instance.</returns>
public static SentenceBuilder Create()
{
return Factory();
}
/// <summary>
/// Factory to allow custom SentenceBuilder injection
/// </summary>
public static Func<SentenceBuilder> Factory { get; set; } = () => new DefaultSentenceBuilder();
/// <summary>
/// Gets a delegate that returns the word 'required'.
/// </summary>
public abstract Func<string> RequiredWord { get; }
/// <summary>
/// Gets a delegate that returns the word 'group'.
/// </summary>
public abstract Func<string> OptionGroupWord { get; }
/// <summary>
/// Gets a delegate that returns that errors block heading text.
/// </summary>
public abstract Func<string> ErrorsHeadingText { get; }
/// <summary>
/// Gets a delegate that returns usage text block heading text.
/// </summary>
public abstract Func<string> UsageHeadingText { get; }
/// <summary>
/// Get a delegate that returns the help text of help command.
/// The delegates must accept a boolean that is equal <value>true</value> for options; otherwise <value>false</value> for verbs.
/// </summary>
public abstract Func<bool, string> HelpCommandText { get; }
/// <summary>
/// Get a delegate that returns the help text of vesion command.
/// The delegates must accept a boolean that is equal <value>true</value> for options; otherwise <value>false</value> for verbs.
/// </summary>
public abstract Func<bool, string> VersionCommandText { get; }
/// <summary>
/// Gets a delegate that handles singular error formatting.
/// The delegates must accept an <see cref="Error"/> and returns a string.
/// </summary>
public abstract Func<Error, string> FormatError { get; }
/// <summary>
/// Gets a delegate that handles mutually exclusive set errors formatting.
/// The delegates must accept a sequence of <see cref="MutuallyExclusiveSetError"/> and returns a string.
/// </summary>
public abstract Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutuallyExclusiveSetErrors { get; }
private class DefaultSentenceBuilder : SentenceBuilder
{
public override Func<string> RequiredWord
{
get { return () => "Required."; }
}
public override Func<string> ErrorsHeadingText
{
get { return () => "ERROR(S):"; }
}
public override Func<string> UsageHeadingText
{
get { return () => "USAGE:"; }
}
public override Func<string> OptionGroupWord
{
get { return () => "Group"; }
}
public override Func<bool, string> HelpCommandText
{
get
{
return isOption => isOption
? "Display this help screen."
: "Display more information on a specific command.";
}
}
public override Func<bool, string> VersionCommandText
{
get { return _ => "Display version information."; }
}
public override Func<Error, string> FormatError
{
get
{
return error =>
{
switch (error.Tag)
{
case ErrorType.BadFormatTokenError:
return "Token '".JoinTo(((BadFormatTokenError)error).Token, "' is not recognized.");
case ErrorType.MissingValueOptionError:
return "Option '".JoinTo(((MissingValueOptionError)error).NameInfo.NameText,
"' has no value.");
case ErrorType.UnknownOptionError:
return "Option '".JoinTo(((UnknownOptionError)error).Token, "' is unknown.");
case ErrorType.MissingRequiredOptionError:
var errMisssing = ((MissingRequiredOptionError)error);
return errMisssing.NameInfo.Equals(NameInfo.EmptyName)
? "A required value not bound to option name is missing."
: "Required option '".JoinTo(errMisssing.NameInfo.NameText, "' is missing.");
case ErrorType.BadFormatConversionError:
var badFormat = ((BadFormatConversionError)error);
return badFormat.NameInfo.Equals(NameInfo.EmptyName)
? "A value not bound to option name is defined with a bad format."
: "Option '".JoinTo(badFormat.NameInfo.NameText, "' is defined with a bad format.");
case ErrorType.SequenceOutOfRangeError:
var seqOutRange = ((SequenceOutOfRangeError)error);
return seqOutRange.NameInfo.Equals(NameInfo.EmptyName)
? "A sequence value not bound to option name is defined with fewer items than required."
: "A sequence option '".JoinTo(seqOutRange.NameInfo.NameText,
"' is defined with fewer or more items than required.");
case ErrorType.BadVerbSelectedError:
return "Verb '".JoinTo(((BadVerbSelectedError)error).Token, "' is not recognized.");
case ErrorType.NoVerbSelectedError:
return "No verb selected.";
case ErrorType.RepeatedOptionError:
return "Option '".JoinTo(((RepeatedOptionError)error).NameInfo.NameText,
"' is defined multiple times.");
case ErrorType.SetValueExceptionError:
var setValueError = (SetValueExceptionError)error;
return "Error setting value to option '".JoinTo(setValueError.NameInfo.NameText, "': ", setValueError.Exception.Message);
case ErrorType.MissingGroupOptionError:
var missingGroupOptionError = (MissingGroupOptionError)error;
return "At least one option from group '".JoinTo(
missingGroupOptionError.Group,
"' (",
string.Join(", ", missingGroupOptionError.Names.Select(n => n.NameText)),
") is required.");
case ErrorType.GroupOptionAmbiguityError:
var groupOptionAmbiguityError = (GroupOptionAmbiguityError)error;
return "Both SetName and Group are not allowed in option: (".JoinTo(groupOptionAmbiguityError.Option.NameText, ")");
case ErrorType.MultipleDefaultVerbsError:
return MultipleDefaultVerbsError.ErrorMessage;
}
throw new InvalidOperationException();
};
}
}
public override Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutuallyExclusiveSetErrors
{
get
{
return errors =>
{
var bySet = from e in errors
group e by e.SetName into g
select new { SetName = g.Key, Errors = g.ToList() };
var msgs = bySet.Select(
set =>
{
var names = string.Join(
string.Empty,
(from e in set.Errors select "'".JoinTo(e.NameInfo.NameText, "', ")).ToArray());
var namesCount = set.Errors.Count();
var incompat = string.Join(
string.Empty,
(from x in
(from s in bySet where !s.SetName.Equals(set.SetName) from e in s.Errors select e)
.Distinct()
select "'".JoinTo(x.NameInfo.NameText, "', ")).ToArray());
return
new StringBuilder("Option")
.AppendWhen(namesCount > 1, "s")
.Append(": ")
.Append(names.Substring(0, names.Length - 2))
.Append(' ')
.AppendIf(namesCount > 1, "are", "is")
.Append(" not compatible with: ")
.Append(incompat.Substring(0, incompat.Length - 2))
.Append('.')
.ToString();
}).ToArray();
return string.Join(Environment.NewLine, msgs);
};
}
}
}
}
}