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
43 var texts = result
44 .MapResult(
45 (HeadOptions opts) => Tuple.Create(header(opts), reader(opts)),
46 (TailOptions opts) => Tuple.Create(header(opts), reader(opts)),
48
49 printIfNotEmpty(texts.Item1);
50 printIfNotEmpty(texts.Item2);
51
53 }
Provides methods to parse command line arguments.
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...
static Parser Default
Gets the singleton instance created with basic defaults.
static Tuple< string, string > MakeError()
static string ReadBytes(string fileName, bool fromTop, int count)
static string ReadLines(string fileName, bool fromTop, int count)