BadScript 2
Loading...
Searching...
No Matches
LocalizableSentenceBuilder.cs
Go to the documentation of this file.
1using System;
2using System.Collections.Generic;
3using System.Linq;
4using System.Text;
5using CommandLine;
7
9{
11 {
12 public override Func<string> RequiredWord
13 {
14 get { return () => Properties.Resources.SentenceRequiredWord; }
15 }
16
17 public override Func<string> ErrorsHeadingText
18 {
19 // Cannot be pluralized
20 get { return () => Properties.Resources.SentenceErrorsHeadingText; }
21 }
22
23 public override Func<string> UsageHeadingText
24 {
25 get { return () => Properties.Resources.SentenceUsageHeadingText; }
26 }
27
28 public override Func<bool, string> HelpCommandText
29 {
30 get
31 {
32 return isOption => isOption
33 ? Properties.Resources.SentenceHelpCommandTextOption
34 : Properties.Resources.SentenceHelpCommandTextVerb;
35 }
36 }
37
38 public override Func<bool, string> VersionCommandText
39 {
40 get { return _ => Properties.Resources.SentenceVersionCommandText; }
41 }
42
43 public override Func<Error, string> FormatError
44 {
45 get
46 {
47 return error =>
48 {
49 switch (error.Tag)
50 {
51 case ErrorType.BadFormatTokenError:
52 return String.Format(Properties.Resources.SentenceBadFormatTokenError, ((BadFormatTokenError)error).Token);
53 case ErrorType.MissingValueOptionError:
54 return String.Format(Properties.Resources.SentenceMissingValueOptionError, ((MissingValueOptionError)error).NameInfo.NameText);
55 case ErrorType.UnknownOptionError:
56 return String.Format(Properties.Resources.SentenceUnknownOptionError, ((UnknownOptionError)error).Token);
57 case ErrorType.MissingRequiredOptionError:
58 var errMisssing = ((MissingRequiredOptionError)error);
59 return errMisssing.NameInfo.Equals(NameInfo.EmptyName)
60 ? Properties.Resources.SentenceMissingRequiredOptionError
61 : String.Format(Properties.Resources.SentenceMissingRequiredOptionError, errMisssing.NameInfo.NameText);
62 case ErrorType.BadFormatConversionError:
63 var badFormat = ((BadFormatConversionError)error);
64 return badFormat.NameInfo.Equals(NameInfo.EmptyName)
65 ? Properties.Resources.SentenceBadFormatConversionErrorValue
66 : String.Format(Properties.Resources.SentenceBadFormatConversionErrorOption, badFormat.NameInfo.NameText);
67 case ErrorType.SequenceOutOfRangeError:
68 var seqOutRange = ((SequenceOutOfRangeError)error);
69 return seqOutRange.NameInfo.Equals(NameInfo.EmptyName)
70 ? Properties.Resources.SentenceSequenceOutOfRangeErrorValue
71 : String.Format(Properties.Resources.SentenceSequenceOutOfRangeErrorOption,
72 seqOutRange.NameInfo.NameText);
73 case ErrorType.BadVerbSelectedError:
74 return String.Format(Properties.Resources.SentenceBadVerbSelectedError, ((BadVerbSelectedError)error).Token);
75 case ErrorType.NoVerbSelectedError:
76 return Properties.Resources.SentenceNoVerbSelectedError;
77 case ErrorType.RepeatedOptionError:
78 return String.Format(Properties.Resources.SentenceRepeatedOptionError, ((RepeatedOptionError)error).NameInfo.NameText);
79 case ErrorType.SetValueExceptionError:
80 var setValueError = (SetValueExceptionError)error;
81 return String.Format(Properties.Resources.SentenceSetValueExceptionError, setValueError.NameInfo.NameText, setValueError.Exception.Message);
82 }
83 throw new InvalidOperationException();
84 };
85 }
86 }
87
88 public override Func<IEnumerable<MutuallyExclusiveSetError>, string> FormatMutuallyExclusiveSetErrors
89 {
90 get
91 {
92 return errors =>
93 {
94 var bySet = from e in errors
95 group e by e.SetName into g
96 select new { SetName = g.Key, Errors = g.ToList() };
97
98 var msgs = bySet.Select(
99 set =>
100 {
101 var names = String.Join(
102 String.Empty,
103 (from e in set.Errors select String.Format("'{0}', ", e.NameInfo.NameText)).ToArray());
104 var namesCount = set.Errors.Count();
105
106 var incompat = String.Join(
107 String.Empty,
108 (from x in
109 (from s in bySet where !s.SetName.Equals(set.SetName) from e in s.Errors select e)
110 .Distinct()
111 select String.Format("'{0}', ", x.NameInfo.NameText)).ToArray());
112 //TODO: Pluralize by namesCount
113 return
114 String.Format(Properties.Resources.SentenceMutuallyExclusiveSetErrors,
115 names.Substring(0, names.Length - 2), incompat.Substring(0, incompat.Length - 2));
116 }).ToArray();
117 return string.Join(Environment.NewLine, msgs);
118 };
119 }
120 }
121 }
122}
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
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 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
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
Exposes standard delegates to provide a mean to customize part of help screen generation....
Models an error generated when an unknown option is detected.
Definition Error.cs:383
override Func< IEnumerable< MutuallyExclusiveSetError >, string > FormatMutuallyExclusiveSetErrors
ErrorType
Discriminator enumeration of CommandLine.Error derivates.
Definition Error.cs:13