Skip to content

Commit 7368f16

Browse files
committed
Completing demo
1 parent 3198660 commit 7368f16

4 files changed

Lines changed: 130 additions & 70 deletions

File tree

demo/ReadText.Demo/Options.cs

Lines changed: 47 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1,45 +1,48 @@
1-
using System;
2-
using CommandLine;
3-
4-
namespace ReadText.Demo
5-
{
6-
abstract class Options
7-
{
8-
[Option('q', "quiet",
9-
HelpText = "Supresses summary messages.")]
10-
public bool Quiet { get; set; }
11-
12-
[Value(0)]
13-
public string FileName { get; set; }
14-
}
15-
16-
[Verb("head", HelpText = "Displays first lines of a file.")]
17-
class HeadOptions : Options
18-
{
19-
[Option('n', "lines",
20-
DefaultValue = 10,
21-
SetName = "amount",
22-
HelpText = "Lines to be printed from the beginning of the file (default 10).")]
23-
public uint Lines { get; set; }
24-
25-
[Option('c', "bytes",
26-
SetName = "amount",
27-
HelpText = "Bytes to be printed from the beginning of the file.")]
28-
public uint Bytes { get; set; }
29-
}
30-
31-
[Verb("tail", HelpText = "Displays last lines of a file.")]
32-
class TailOptions : Options
33-
{
34-
[Option('n', "lines",
35-
DefaultValue = 10,
36-
SetName = "amount",
37-
HelpText = "Lines to be printed from the end of the file (default 10).")]
38-
public uint Lines { get; set; }
39-
40-
[Option('c', "bytes",
41-
SetName = "amount",
42-
HelpText = "Bytes to be printed from the end of the file.")]
43-
public uint Bytes { get; set; }
44-
}
1+
using CommandLine;
2+
3+
namespace ReadText.Demo
4+
{
5+
interface IOptions
6+
{
7+
[Option('n', "lines",
8+
SetName = "amount",
9+
HelpText = "Lines to be printed from the beginning or end of the file.")]
10+
uint? Lines { get; set; }
11+
12+
[Option('c', "bytes",
13+
SetName = "amount",
14+
HelpText = "Bytes to be printed from the beginning or end of the file.")]
15+
uint? Bytes { get; set; }
16+
17+
[Option('q', "quiet",
18+
HelpText = "Supresses summary messages.")]
19+
bool Quiet { get; set; }
20+
21+
[Value(0)]
22+
string FileName { get; set; }
23+
}
24+
25+
[Verb("head", HelpText = "Displays first lines of a file.")]
26+
class HeadOptions : IOptions
27+
{
28+
public uint? Lines { get; set; }
29+
30+
public uint? Bytes { get; set; }
31+
32+
public bool Quiet { get; set; }
33+
34+
public string FileName { get; set; }
35+
}
36+
37+
[Verb("tail", HelpText = "Displays last lines of a file.")]
38+
class TailOptions : IOptions
39+
{
40+
public uint? Lines { get; set; }
41+
42+
public uint? Bytes { get; set; }
43+
44+
public bool Quiet { get; set; }
45+
46+
public string FileName { get; set; }
47+
}
4548
}

demo/ReadText.Demo/Program.cs

Lines changed: 73 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,73 @@
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+
}

demo/ReadText.Demo/ReadText.Demo.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,9 @@
2929
<Externalconsole>true</Externalconsole>
3030
</PropertyGroup>
3131
<ItemGroup>
32+
<Reference Include="CommandLine">
33+
<HintPath>packages\CommandLineParser.2.0.47-alpha\lib\net40\CommandLine.dll</HintPath>
34+
</Reference>
3235
<Reference Include="System" />
3336
<Reference Include="System.Core" />
3437
</ItemGroup>
@@ -40,5 +43,8 @@
4043
<Compile Include="Properties\AssemblyInfo.cs" />
4144
<Compile Include="Options.cs" />
4245
</ItemGroup>
46+
<ItemGroup>
47+
<None Include="packages.config" />
48+
</ItemGroup>
4349
<Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" />
4450
</Project>

demo/ReadText.Demo/packages.config

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<packages>
3+
<package id="CommandLineParser" version="2.0.47-alpha" targetFramework="net40" />
4+
</packages>

0 commit comments

Comments
 (0)