|
1 | | -using System; |
2 | | -using System.Collections.Generic; |
3 | | -using System.Linq; |
4 | | -using CommandLine; |
5 | | -using CommandLine.Text; |
6 | | - |
7 | | -namespace ReadText.Demo |
8 | | -{ |
9 | | - class Program |
10 | | - { |
11 | | - public static void Main(string[] args) |
12 | | - { |
13 | | - var result = Parser.Default.ParseArguments<HeadOptions, TailOptions>(args); |
14 | | - if (result.Errors.Count() > 0) |
15 | | - { |
16 | | - Environment.Exit(1); |
17 | | - } |
18 | | - |
19 | | - if (result.Value.GetType () == typeof(HeadOptions)) |
20 | | - { |
21 | | - } |
22 | | - |
23 | | - // TODO: complete... |
24 | | - } |
25 | | - } |
26 | | -} |
| 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.Demo |
| 10 | +{ |
| 11 | + class Program |
| 12 | + { |
| 13 | + public static int Main(string[] args) |
| 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 | + |
| 37 | + var result = Parser.Default.ParseArguments<HeadOptions, TailOptions>(args); |
| 38 | + var texts = result |
| 39 | + .Return( |
| 40 | + (HeadOptions opts) => Tuple.Create(header(opts), reader(opts)), |
| 41 | + (TailOptions opts) => Tuple.Create(header(opts), reader(opts)), |
| 42 | + _ => Tuple.Create(string.Empty, string.Empty)); |
| 43 | + |
| 44 | + if (texts.Item1.Length > 0) |
| 45 | + { |
| 46 | + Console.WriteLine(texts.Item1); |
| 47 | + } |
| 48 | + Console.WriteLine(texts.Item2); |
| 49 | + |
| 50 | + return 0; |
| 51 | + } |
| 52 | + |
| 53 | + private static string ReadLines(string fileName, bool fromTop, int count) |
| 54 | + { |
| 55 | + var lines = File.ReadAllLines(fileName); |
| 56 | + if (fromTop) |
| 57 | + { |
| 58 | + return string.Join(Environment.NewLine, lines.Take(count)); |
| 59 | + } |
| 60 | + return string.Join(Environment.NewLine, lines.Reverse().Take(count)); |
| 61 | + } |
| 62 | + |
| 63 | + private static string ReadBytes(string fileName, bool fromTop, int count) |
| 64 | + { |
| 65 | + var bytes = File.ReadAllBytes(fileName); |
| 66 | + if (fromTop) |
| 67 | + { |
| 68 | + return Encoding.UTF8.GetString(bytes, 0, count); |
| 69 | + } |
| 70 | + return Encoding.UTF8.GetString(bytes, bytes.Length - count, count); |
| 71 | + } |
| 72 | + } |
| 73 | +} |
0 commit comments