BadScript 2
Loading...
Searching...
No Matches
ReadText.LocalizedDemo.Program Class Reference

Static Public Member Functions

static int Main (string[] args)
 

Static Private Member Functions

static string ReadLines (string fileName, bool fromTop, int count)
 
static string ReadBytes (string fileName, bool fromTop, int count)
 
static Tuple< string, string > MakeError ()
 

Detailed Description

Definition at line 11 of file Program.cs.

Member Function Documentation

◆ Main()

static int ReadText.LocalizedDemo.Program.Main ( string[]  args)
static

Definition at line 13 of file Program.cs.

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 }
Provides methods to parse command line arguments.
Definition Parser.cs:21
ParserResult< object > ParseArguments(IEnumerable< string > args, params Type[] types)
Parses a string array of command line arguments for verb commands scenario, constructing the proper i...
Definition Parser.cs:216
static Parser Default
Gets the singleton instance created with basic defaults.
Definition Parser.cs:75
static string ReadLines(string fileName, bool fromTop, int count)
Definition Program.cs:58
static string ReadBytes(string fileName, bool fromTop, int count)
Definition Program.cs:68
static Tuple< string, string > MakeError()
Definition Program.cs:78

◆ MakeError()

static Tuple< string, string > ReadText.LocalizedDemo.Program.MakeError ( )
staticprivate

Definition at line 78 of file Program.cs.

79 {
80 return Tuple.Create("\0", "\0");
81 }

◆ ReadBytes()

static string ReadText.LocalizedDemo.Program.ReadBytes ( string  fileName,
bool  fromTop,
int  count 
)
staticprivate

Definition at line 68 of file Program.cs.

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 }

◆ ReadLines()

static string ReadText.LocalizedDemo.Program.ReadLines ( string  fileName,
bool  fromTop,
int  count 
)
staticprivate

Definition at line 58 of file Program.cs.

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 }

The documentation for this class was generated from the following file: