BadScript 2
Loading...
Searching...
No Matches
ReadText.Demo.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.Demo.Program.Main ( string[]  args)
static

Definition at line 13 of file Program.cs.

14 {
15 Func<IOptions, string> reader = opts =>
16 {
17 var fromTop = opts.GetType() == typeof(HeadOptions);
18 return opts.Lines.HasValue
19 ? ReadLines(opts.FileName, fromTop, (int)opts.Lines)
20 : ReadBytes(opts.FileName, fromTop, (int)opts.Bytes);
21 };
22 Func<IOptions, string> header = opts =>
23 {
24 if (opts.Quiet)
25 {
26 return string.Empty;
27 }
28 var fromTop = opts.GetType() == typeof(HeadOptions);
29 var builder = new StringBuilder("Reading ");
30 builder = opts.Lines.HasValue
31 ? builder.Append(opts.Lines).Append(" lines")
32 : builder.Append(opts.Bytes).Append(" bytes");
33 builder = fromTop ? builder.Append(" from top:") : builder.Append(" from bottom:");
34 return builder.ToString();
35 };
36 Action<string> printIfNotEmpty = text =>
37 {
38 if (text.Length == 0) { return; }
39 Console.WriteLine(text);
40 };
41
42 var result = Parser.Default.ParseArguments<HeadOptions, TailOptions>(args);
43 var texts = result
44 .MapResult(
45 (HeadOptions opts) => Tuple.Create(header(opts), reader(opts)),
46 (TailOptions opts) => Tuple.Create(header(opts), reader(opts)),
47 _ => MakeError());
48
49 printIfNotEmpty(texts.Item1);
50 printIfNotEmpty(texts.Item2);
51
52 return texts.Equals(MakeError()) ? 1 : 0;
53 }
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 Tuple< string, string > MakeError()
Definition Program.cs:75
static string ReadBytes(string fileName, bool fromTop, int count)
Definition Program.cs:65
static string ReadLines(string fileName, bool fromTop, int count)
Definition Program.cs:55

◆ MakeError()

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

Definition at line 75 of file Program.cs.

76 {
77 return Tuple.Create("\0", "\0");
78 }

◆ ReadBytes()

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

Definition at line 65 of file Program.cs.

66 {
67 var bytes = File.ReadAllBytes(fileName);
68 if (fromTop)
69 {
70 return Encoding.UTF8.GetString(bytes, 0, count);
71 }
72 return Encoding.UTF8.GetString(bytes, bytes.Length - count, count);
73 }

◆ ReadLines()

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

Definition at line 55 of file Program.cs.

56 {
57 var lines = File.ReadAllLines(fileName);
58 if (fromTop)
59 {
60 return string.Join(Environment.NewLine, lines.Take(count));
61 }
62 return string.Join(Environment.NewLine, lines.Reverse().Take(count));
63 }

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