Skip to content

Commit 16ca79c

Browse files
authored
Merge pull request #485 from tkouba/develop
Localized demo
2 parents f956dd7 + 7ede4da commit 16ca79c

File tree

12 files changed

+1195
-0
lines changed

12 files changed

+1195
-0
lines changed
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<Root Type="DevExpress.CodeRush.Foundation.Navigation.QuickFileNav.SolutionFileInfosContainer">
3+
<Options Language="Neutral">
4+
<Items>
5+
<Item Id="{62fa45be-9e28-4fdc-b57e-974ea27ae9a4}">
6+
<Filename>LocalizableAttributeProperty.cs</Filename>
7+
<FilePath>d:\work\arci\commandline\src\commandline\infrastructure\localizableattributeproperty.cs</FilePath>
8+
<Folders>
9+
<Item>Infrastructure</Item>
10+
</Folders>
11+
<ProjectFileName>d:\WORK\ARCI\commandline\src\CommandLine\CommandLine.csproj</ProjectFileName>
12+
<ProjectName>CommandLine</ProjectName>
13+
</Item>
14+
</Items>
15+
</Options>
16+
</Root>
Lines changed: 122 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,122 @@
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+
}
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
using CommandLine;
2+
using CommandLine.Text;
3+
using System.Collections.Generic;
4+
5+
namespace ReadText.LocalizedDemo
6+
{
7+
interface IOptions
8+
{
9+
[Option('n', "lines",
10+
Default = 5U,
11+
SetName = "bylines",
12+
HelpText = "HelpTextLines",
13+
ResourceType = typeof(Properties.Resources))]
14+
uint? Lines { get; set; }
15+
16+
[Option('c', "bytes",
17+
SetName = "bybytes",
18+
HelpText = "HelpTextBytes",
19+
ResourceType = typeof(Properties.Resources))]
20+
uint? Bytes { get; set; }
21+
22+
[Option('q', "quiet",
23+
HelpText = "HelpTextQuiet",
24+
ResourceType = typeof(Properties.Resources))]
25+
bool Quiet { get; set; }
26+
27+
[Value(0, MetaName = "input file",
28+
HelpText = "HelpTextFileName",
29+
Required = true,
30+
ResourceType = typeof(Properties.Resources))]
31+
string FileName { get; set; }
32+
}
33+
34+
[Verb("head", HelpText = "HelpTextVerbHead", ResourceType = typeof(Properties.Resources))]
35+
class HeadOptions : IOptions
36+
{
37+
public uint? Lines { get; set; }
38+
39+
public uint? Bytes { get; set; }
40+
41+
public bool Quiet { get; set; }
42+
43+
public string FileName { get; set; }
44+
45+
[Usage(ApplicationAlias = "ReadText.LocalizedDemo.exe")]
46+
public static IEnumerable<Example> Examples
47+
{
48+
get
49+
{
50+
yield return new Example(Properties.Resources.ExamplesNormalScenario, new HeadOptions { FileName = "file.bin"});
51+
yield return new Example(Properties.Resources.ExamplesSpecifyBytes, new HeadOptions { FileName = "file.bin", Bytes=100 });
52+
yield return new Example(Properties.Resources.ExamplesSuppressSummary, UnParserSettings.WithGroupSwitchesOnly(), new HeadOptions { FileName = "file.bin", Quiet = true });
53+
yield return new Example(Properties.Resources.ExamplesReadMoreLines, new[] { UnParserSettings.WithGroupSwitchesOnly(), UnParserSettings.WithUseEqualTokenOnly() }, new HeadOptions { FileName = "file.bin", Lines = 10 });
54+
}
55+
}
56+
}
57+
58+
[Verb("tail", HelpText = "HelpTextVerbTail", ResourceType = typeof(Properties.Resources))]
59+
class TailOptions : IOptions
60+
{
61+
public uint? Lines { get; set; }
62+
63+
public uint? Bytes { get; set; }
64+
65+
public bool Quiet { get; set; }
66+
67+
public string FileName { get; set; }
68+
}
69+
}
Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
using System;
2+
using System.Collections.Generic;
3+
using System.IO;
4+
using System.Linq;
5+
using System.Text;
6+
using CommandLine;
7+
using CommandLine.Text;
8+
9+
namespace ReadText.LocalizedDemo
10+
{
11+
class Program
12+
{
13+
public static int Main(string[] args)
14+
{
15+
// Set sentence builder to localizable
16+
SentenceBuilder.Factory = () => new LocalizableSentenceBuilder();
17+
18+
Func<IOptions, string> reader = opts =>
19+
{
20+
var fromTop = opts.GetType() == typeof(HeadOptions);
21+
return opts.Lines.HasValue
22+
? ReadLines(opts.FileName, fromTop, (int)opts.Lines)
23+
: ReadBytes(opts.FileName, fromTop, (int)opts.Bytes);
24+
};
25+
Func<IOptions, string> header = opts =>
26+
{
27+
if (opts.Quiet)
28+
{
29+
return string.Empty;
30+
}
31+
var fromTop = opts.GetType() == typeof(HeadOptions);
32+
var builder = new StringBuilder(Properties.Resources.Reading);
33+
builder = opts.Lines.HasValue
34+
? builder.Append(opts.Lines).Append(Properties.Resources.Lines)
35+
: builder.Append(opts.Bytes).Append(Properties.Resources.Bytes);
36+
builder = fromTop ? builder.Append(Properties.Resources.FromTop) : builder.Append(Properties.Resources.FromBottom);
37+
return builder.ToString();
38+
};
39+
Action<string> printIfNotEmpty = text =>
40+
{
41+
if (text.Length == 0) { return; }
42+
Console.WriteLine(text);
43+
};
44+
45+
var result = Parser.Default.ParseArguments<HeadOptions, TailOptions>(args);
46+
var texts = result
47+
.MapResult(
48+
(HeadOptions opts) => Tuple.Create(header(opts), reader(opts)),
49+
(TailOptions opts) => Tuple.Create(header(opts), reader(opts)),
50+
_ => MakeError());
51+
52+
printIfNotEmpty(texts.Item1);
53+
printIfNotEmpty(texts.Item2);
54+
55+
return texts.Equals(MakeError()) ? 1 : 0;
56+
}
57+
58+
private static string ReadLines(string fileName, bool fromTop, int count)
59+
{
60+
var lines = File.ReadAllLines(fileName);
61+
if (fromTop)
62+
{
63+
return string.Join(Environment.NewLine, lines.Take(count));
64+
}
65+
return string.Join(Environment.NewLine, lines.Reverse().Take(count));
66+
}
67+
68+
private static string ReadBytes(string fileName, bool fromTop, int count)
69+
{
70+
var bytes = File.ReadAllBytes(fileName);
71+
if (fromTop)
72+
{
73+
return Encoding.UTF8.GetString(bytes, 0, count);
74+
}
75+
return Encoding.UTF8.GetString(bytes, bytes.Length - count, count);
76+
}
77+
78+
private static Tuple<string, string> MakeError()
79+
{
80+
return Tuple.Create("\0", "\0");
81+
}
82+
}
83+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
using System.Reflection;
2+
using System.Runtime.CompilerServices;
3+
4+
[assembly: AssemblyTitle("ReadText.Demo")]
5+
[assembly: AssemblyDescription("ReadText.Demo for Command Line Parser Library")]
6+
[assembly: AssemblyTrademark("")]
7+
#if DEBUG
8+
[assembly: AssemblyConfiguration("Debug")]
9+
#else
10+
[assembly: AssemblyConfiguration("Release")]
11+
#endif

0 commit comments

Comments
 (0)