|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
| 4 | +using System.Text; |
| 5 | +using CommandLine; |
| 6 | +using CommandLine.Text; |
| 7 | + |
| 8 | +namespace ReadText.LocalizedDemo |
| 9 | +{ |
| 10 | + public class LocalizableSentenceBuilder : SentenceBuilder |
| 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 | +} |
0 commit comments