Skip to content

Commit f5777f1

Browse files
committed
Finalizing demo
1 parent 7368f16 commit f5777f1

2 files changed

Lines changed: 31 additions & 23 deletions

File tree

demo/ReadText.Demo/Options.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,21 @@ namespace ReadText.Demo
55
interface IOptions
66
{
77
[Option('n', "lines",
8-
SetName = "amount",
8+
Default = 5U,
9+
SetName = "amount",
910
HelpText = "Lines to be printed from the beginning or end of the file.")]
1011
uint? Lines { get; set; }
1112

1213
[Option('c', "bytes",
13-
SetName = "amount",
14-
HelpText = "Bytes to be printed from the beginning or end of the file.")]
14+
SetName = "amount2",
15+
HelpText = "Bytes to be printed from the beginning or end of the file.")]
1516
uint? Bytes { get; set; }
1617

1718
[Option('q', "quiet",
1819
HelpText = "Supresses summary messages.")]
1920
bool Quiet { get; set; }
2021

21-
[Value(0)]
22+
[Value(0, Required = true)]
2223
string FileName { get; set; }
2324
}
2425

demo/ReadText.Demo/Program.cs

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,34 +20,36 @@ public static int Main(string[] args)
2020
: ReadBytes(opts.FileName, fromTop, (int)opts.Bytes);
2121
};
2222
Func<IOptions, string> header = opts =>
23-
{
24-
if (opts.Quiet)
2523
{
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-
};
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+
};
3641

3742
var result = Parser.Default.ParseArguments<HeadOptions, TailOptions>(args);
3843
var texts = result
3944
.Return(
4045
(HeadOptions opts) => Tuple.Create(header(opts), reader(opts)),
4146
(TailOptions opts) => Tuple.Create(header(opts), reader(opts)),
42-
_ => Tuple.Create(string.Empty, string.Empty));
47+
_ => MakeError());
4348

44-
if (texts.Item1.Length > 0)
45-
{
46-
Console.WriteLine(texts.Item1);
47-
}
48-
Console.WriteLine(texts.Item2);
49+
printIfNotEmpty(texts.Item1);
50+
printIfNotEmpty(texts.Item2);
4951

50-
return 0;
52+
return texts.Equals(MakeError()) ? 1 : 0;
5153
}
5254

5355
private static string ReadLines(string fileName, bool fromTop, int count)
@@ -69,5 +71,10 @@ private static string ReadBytes(string fileName, bool fromTop, int count)
6971
}
7072
return Encoding.UTF8.GetString(bytes, bytes.Length - count, count);
7173
}
74+
75+
private static Tuple<string, string> MakeError()
76+
{
77+
return Tuple.Create("\0", "\0");
78+
}
7279
}
7380
}

0 commit comments

Comments
 (0)