-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
85 lines (73 loc) · 2.19 KB
/
Copy pathProgram.cs
File metadata and controls
85 lines (73 loc) · 2.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
using System.CommandLine;
namespace cscli;
class Program
{
static int Main(string[] argv)
{
/*
Program Root Command.
*/
var program = new RootCommand("Display a file.");
/*
Define Options For Commands.
*/
var fileOption = new Option<FileInfo?>(
name: "--file",
description: "File to display."
);
/*
Define Sub-Commands.
*/
var subcmdA = new Command("A", "Sub-Command A");
var subcmdB = new Command("B", "Sub-Command B");
var subcmdC = new Command("C", "Sub-Command C");
var nestedSubcmdD = new Command("D", "Nested Sub-Command D");
/*
Bind Options & Nested Sub-Commands To Commands.
*/
// option must be bound to the nested command before the
// before the nested-subcommand is bound to its parent
// command.
nestedSubcmdD.AddOption(fileOption);
subcmdA.AddCommand(nestedSubcmdD);
// binding the commands to the program. This happens after the
// options have been bound to the subcommands.
program.AddCommand(subcmdA);
program.AddCommand(subcmdB);
program.AddCommand(subcmdC);
/*
Set Command Handlers.
*/
subcmdA.SetHandler(() =>
{
DemoExplain();
});
subcmdB.SetHandler(() =>
{
DemoExplain();
});
subcmdC.SetHandler(() =>
{
DemoExplain();
});
// the option variables go after the lambda is defined.
nestedSubcmdD.SetHandler((file) =>
{
DisplayFile(file!);
}, fileOption);
/*
Invoke CLI.
*/
return program.Invoke(argv);
}
static void DisplayFile(FileInfo file)
{
File.ReadLines(file.FullName).ToList()
.ForEach(line => Console.WriteLine(line));
}
static void DemoExplain()
{
Console.WriteLine("\n\t\tDisplay a file by running:");
Console.WriteLine("<executable> A D --file <file>\n");
}
}