bob-core 0.8.7 → 0.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +272 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -1,5 +1,275 @@
|
|
|
1
|
-
# BOB - Bash Operation Buddy
|
|
1
|
+
# BOB Core - Bash Operation Buddy Core
|
|
2
2
|
|
|
3
3
|
## Introduction
|
|
4
4
|
|
|
5
|
-
BOB (Bash Operation Buddy)
|
|
5
|
+
BOB (Bash Operation Buddy) Core is a library that provides a set of functions to create your own CLI in TypeScript.
|
|
6
|
+
|
|
7
|
+
## Usage
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
npm install bob-core
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
Initialize the CLI:
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { CLI } from 'bob-core';
|
|
17
|
+
|
|
18
|
+
const cli = new CLI();
|
|
19
|
+
|
|
20
|
+
await cli.loadCommandsPath('./commands');
|
|
21
|
+
|
|
22
|
+
cli.run('commandName', 'arg1', 'arg2', 'arg3');
|
|
23
|
+
```
|
|
24
|
+
|
|
25
|
+
Create a command in the `commands` folder:
|
|
26
|
+
|
|
27
|
+
```typescript
|
|
28
|
+
import { Command } from 'bob-core';
|
|
29
|
+
|
|
30
|
+
export default class MyCommand extends Command {
|
|
31
|
+
public name = 'my-command {arg1} {--option1}';
|
|
32
|
+
public description = 'This is my command';
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Define the arguments and options help
|
|
36
|
+
*
|
|
37
|
+
* Optional
|
|
38
|
+
*/
|
|
39
|
+
helpDefinition = {
|
|
40
|
+
arg1: 'This is the first argument',
|
|
41
|
+
'--option1': 'This is the first option'
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
/**
|
|
45
|
+
* Provide examples of how to use the command
|
|
46
|
+
*
|
|
47
|
+
* Optional
|
|
48
|
+
*/
|
|
49
|
+
commandsExamples = [
|
|
50
|
+
{
|
|
51
|
+
command: 'my-command value1 --option1',
|
|
52
|
+
description: 'This is an example'
|
|
53
|
+
}
|
|
54
|
+
]
|
|
55
|
+
|
|
56
|
+
public async handle() {
|
|
57
|
+
console.log('Hello World');
|
|
58
|
+
console.log('Arguments:', this.argument('arg1'));
|
|
59
|
+
console.log('Options:', this.option('option1'));
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
## Cli Help
|
|
65
|
+
|
|
66
|
+
The CLI provides a help command that displays all available commands and their descriptions.
|
|
67
|
+
|
|
68
|
+
```bash
|
|
69
|
+
node cli.js help
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
## Commands
|
|
73
|
+
|
|
74
|
+
```bash
|
|
75
|
+
Bob CLI x.x.x 💪
|
|
76
|
+
|
|
77
|
+
Usage:
|
|
78
|
+
command [options] [arguments]
|
|
79
|
+
|
|
80
|
+
Available commands:
|
|
81
|
+
|
|
82
|
+
help Show help
|
|
83
|
+
test test description
|
|
84
|
+
sub:
|
|
85
|
+
sub sub command description
|
|
86
|
+
sub:sub sub:sub command description
|
|
87
|
+
|
|
88
|
+
```
|
|
89
|
+
|
|
90
|
+
## Command help
|
|
91
|
+
|
|
92
|
+
You can also display the help of a specific command.
|
|
93
|
+
|
|
94
|
+
```bash
|
|
95
|
+
node cli.js test -h
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```bash
|
|
99
|
+
Description:
|
|
100
|
+
test description
|
|
101
|
+
|
|
102
|
+
Usage:
|
|
103
|
+
test <user> [options]
|
|
104
|
+
|
|
105
|
+
Arguments:
|
|
106
|
+
user user description
|
|
107
|
+
test test description [default: null]
|
|
108
|
+
test2 [default: []] (variadic)
|
|
109
|
+
|
|
110
|
+
Options:
|
|
111
|
+
--option, -o, -b option description (boolean) [default: false]
|
|
112
|
+
--flag flag description (string) [default: null]
|
|
113
|
+
--arr arr description (array) [default: null]
|
|
114
|
+
--flag2 flag2 description (string) [default: 2]
|
|
115
|
+
--help, -h Display help for the given command. When no command is given display help for the list command (boolean)
|
|
116
|
+
|
|
117
|
+
Examples:
|
|
118
|
+
Example description 1
|
|
119
|
+
|
|
120
|
+
node cli.js test yayo --option
|
|
121
|
+
|
|
122
|
+
Example description 2
|
|
123
|
+
|
|
124
|
+
node cli.js test anothervalue --flag=2
|
|
125
|
+
```
|
|
126
|
+
|
|
127
|
+
Depending on the command, the help will display the command signature, description, arguments, options and examples.
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
## Commands signature
|
|
131
|
+
|
|
132
|
+
The command signature is a string that defines the command name, arguments and options.
|
|
133
|
+
|
|
134
|
+
Example:
|
|
135
|
+
```typescript
|
|
136
|
+
signature = 'my-command {arg1} {arg2} {arg3} {--option1} {--option2}';
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
### Arguments
|
|
140
|
+
|
|
141
|
+
All user supplied arguments and options are wrapped in curly braces.
|
|
142
|
+
In the following example, the command defines three **required** arguments `arg1`, `arg2` and `arg3`.
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
signature = 'my-command {arg1} {arg2} {arg3}';
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
You may want to make an argument optional by adding a `?` after the argument name or by providing a default value with `=`.
|
|
149
|
+
|
|
150
|
+
```typescript
|
|
151
|
+
signature = 'my-command {arg1} {arg2?} {arg3=defaultValue}';
|
|
152
|
+
|
|
153
|
+
handle() {
|
|
154
|
+
this.argument('arg1'); // 'value' or throw an error if not provided
|
|
155
|
+
this.argument('arg2'); // 'value' or null if not provided
|
|
156
|
+
this.argument('arg3'); // 'value' or 'defaultValue' if not provided
|
|
157
|
+
}
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
You can also define a variadic argument by adding `*` after the argument name.
|
|
161
|
+
Variadic arguments are stored in an array.
|
|
162
|
+
|
|
163
|
+
```typescript
|
|
164
|
+
signature = 'my-command {arg1} {arg2*}';
|
|
165
|
+
|
|
166
|
+
handle() {
|
|
167
|
+
this.argument('arg1'); // 'value1'
|
|
168
|
+
this.argument('arg2'); // ['value2', 'value3']
|
|
169
|
+
}
|
|
170
|
+
```
|
|
171
|
+
|
|
172
|
+
Variadic arguments can also be optional.
|
|
173
|
+
|
|
174
|
+
```typescript
|
|
175
|
+
signature = 'my-command {arg1} {arg2*?}';
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
### Options
|
|
179
|
+
|
|
180
|
+
Options are defined by `{--optionName}`.
|
|
181
|
+
|
|
182
|
+
```typescript
|
|
183
|
+
signature = 'my-command {--option1} {--option2} {--option3}';
|
|
184
|
+
```
|
|
185
|
+
|
|
186
|
+
By default options are boolean with a default value of `false`.
|
|
187
|
+
You can also change the option type to string by adding `=` to the option definition.
|
|
188
|
+
You can also provide a default value by adding `=value`.
|
|
189
|
+
|
|
190
|
+
If the value is 'true' or 'false', the option will be converted to a boolean.
|
|
191
|
+
|
|
192
|
+
```typescript
|
|
193
|
+
signature = 'my-command {--option1} {--option2=true} {--option3=} {--option4=defaultValue} {--option5=}';
|
|
194
|
+
|
|
195
|
+
handle() {
|
|
196
|
+
this.option('option1'); // by default `false` and can be set to `true` by the user
|
|
197
|
+
this.option('option2'); // by default `true` and can be set to `false` by the user
|
|
198
|
+
this.option('option3'); // by default `null` and can be set to "value" by the user
|
|
199
|
+
this.option('option4'); // by default "defaultValue" and can be set to "value" by the user
|
|
200
|
+
}
|
|
201
|
+
```
|
|
202
|
+
|
|
203
|
+
You can also define a variadic option by adding `*` as option value. (e.g. `{--option2=*}`)
|
|
204
|
+
Variadic options are stored in an array.
|
|
205
|
+
|
|
206
|
+
```typescript
|
|
207
|
+
signature = 'my-command {--option1} {--option2=*}';
|
|
208
|
+
|
|
209
|
+
handle() {
|
|
210
|
+
this.option('option1'); // 'value1'
|
|
211
|
+
this.option('option2'); // ['value2', 'value3'] // or [] if not provided
|
|
212
|
+
}
|
|
213
|
+
```
|
|
214
|
+
|
|
215
|
+
## Commands I/O
|
|
216
|
+
|
|
217
|
+
### Arguments
|
|
218
|
+
|
|
219
|
+
```typescript
|
|
220
|
+
this.argument('arg1');
|
|
221
|
+
```
|
|
222
|
+
|
|
223
|
+
You can also provide a default value if the argument is optional.
|
|
224
|
+
|
|
225
|
+
```typescript
|
|
226
|
+
this.argument('arg1', 'defaultValue');
|
|
227
|
+
```
|
|
228
|
+
|
|
229
|
+
If you always need a boolean value, you can use the `argumentBoolean` method.
|
|
230
|
+
|
|
231
|
+
```typescript
|
|
232
|
+
this.argumentBoolean('arg1');
|
|
233
|
+
```
|
|
234
|
+
|
|
235
|
+
If you always need a number value, you can use the `argumentNumber` method.
|
|
236
|
+
|
|
237
|
+
```typescript
|
|
238
|
+
this.argumentNumber('arg1');
|
|
239
|
+
```
|
|
240
|
+
|
|
241
|
+
If you always need a array value, you can use the `argumentArray` method.
|
|
242
|
+
|
|
243
|
+
```typescript
|
|
244
|
+
this.argumentArray('arg1');
|
|
245
|
+
```
|
|
246
|
+
|
|
247
|
+
### Options
|
|
248
|
+
|
|
249
|
+
```typescript
|
|
250
|
+
this.option('option1');
|
|
251
|
+
```
|
|
252
|
+
|
|
253
|
+
You can also provide a default value if the option is optional.
|
|
254
|
+
|
|
255
|
+
```typescript
|
|
256
|
+
this.option('option1', 'defaultValue');
|
|
257
|
+
```
|
|
258
|
+
|
|
259
|
+
If you always need a boolean value, you can use the `optionBoolean` method.
|
|
260
|
+
|
|
261
|
+
```typescript
|
|
262
|
+
this.optionBoolean('option1');
|
|
263
|
+
```
|
|
264
|
+
|
|
265
|
+
If you always need a number value, you can use the `optionNumber` method.
|
|
266
|
+
|
|
267
|
+
```typescript
|
|
268
|
+
this.optionNumber('option1');
|
|
269
|
+
```
|
|
270
|
+
|
|
271
|
+
If you always need a array value, you can use the `optionArray` method.
|
|
272
|
+
|
|
273
|
+
```typescript
|
|
274
|
+
this.optionArray('option1');
|
|
275
|
+
```
|