-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexample-cli.ts
193 lines (187 loc) · 5.64 KB
/
example-cli.ts
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* Example application
* This file defines some basic commands, then injects everything into the app runner
*/
import {
ArrayCliCommandsCollection,
CliApplication,
ConsoleOutputter,
ConsoleUserInputRequestor,
iCliCommand,
iCliOutputter,
} from "./public-api";
/**
* Array of the commands which the program will allow the user to execute actions
*/
const commands: iCliCommand[] = [
{
name: "Print Date",
displayText: "Print the current date",
tokens: ["print-date", "p-d"],
async getRequiredParams() {
return null;
},
execute: async (
params: {},
cliOutputter: iCliOutputter
): Promise<void> => {
cliOutputter.pushMessage("Date ==> ", new Date());
},
},
{
name: "Print",
displayText: "Print some text from the user input",
tokens: ["print", "p"],
async getRequiredParams() {
return [
{
name: "txt",
displayText: "Text to print",
},
];
},
execute: async (
params: { txt: string },
cliOutputter: iCliOutputter
): Promise<void> => {
cliOutputter.pushMessage("Txt from user ==>", params.txt);
},
},
{
name: "Print Sentence",
displayText: "Print an entire sentence based on the user input",
tokens: ["print-sentence", "p-s"],
async getRequiredParams() {
return [
{
name: "txt",
displayText: "Sentence to print",
isValid: (input: string, numAttempts: number) => {
if (input.indexOf(".") > -1) {
return { isValid: true };
} else {
return {
isValid: false,
message: "A sentence must contain a period.",
tryAgain: numAttempts < 3,
};
}
},
},
];
},
execute: async (
params: { txt: string },
cliOutputter: iCliOutputter
): Promise<void> => {
cliOutputter.pushMessage("Sentence from user ==>", params.txt);
},
},
{
name: "Boolean Test",
displayText: "Ask for a boolean yes/no",
tokens: ["b-t"],
async getRequiredParams() {
return [
{
name: "booleanValue",
displayText: "Enter a boolean value",
type: "boolean",
},
];
},
execute: async (
params: { booleanValue: boolean },
cliOutputter: iCliOutputter
) => {
cliOutputter.pushMessage("Boolean result", params.booleanValue);
},
},
{
name: "Choose Something",
displayText: "Choose from a list of options",
tokens: ["ch-s"],
async getRequiredParams() {
return [
{
name: "choice",
displayText: "Make a choice",
choices: [
{
name: "one",
displayText: "First choice",
},
{
name: "two",
displayText: "Second choice",
},
{
name: "three",
displayText: "Third choice",
},
{
name: "four",
displayText: "Fourth choice",
},
],
},
];
},
execute: async (
params: { choice: string },
cliOutputter: iCliOutputter
) => {
cliOutputter.pushMessage("Choice result:", params.choice);
},
},
{
name: "Add Numbers",
displayText: "Add some numbers together",
tokens: ["add-numbers", "a-n"],
async getRequiredParams() {
return [
{
name: "number",
displayText:
"Enter the number to add, or empty to conclude",
type: "number",
doRepeat: () => true,
},
];
},
execute: async (
params: { number: number[] },
cliOutputter: iCliOutputter
): Promise<void> => {
const sum: number = params.number.reduce((acc, n) => acc + n, 0);
cliOutputter.pushMessage(`The sum is ${sum}`);
},
},
];
//create the app object
const app = new CliApplication();
//When quit is dispatched, trigger the process to exit
app.onQuit(() => {
process.exit(0);
});
//create the i/o dependencies
const outputter = new ConsoleOutputter();
const inputRequestor = new ConsoleUserInputRequestor(outputter);
//create the app and inject dependencies
app.startApp(
{
startup: {
initialOutput: "Welcome to the example application",
},
loop: {
enabled: true,
},
},
//Pass the args into a collection object
new ArrayCliCommandsCollection(commands),
//Pass in the process arguments
[...process.argv].slice(2),
//Use the default console outputter and input requestors
outputter,
inputRequestor
);