Skip to content

Commit a94f80a

Browse files
committed
Updated docs
1 parent 70d090e commit a94f80a

File tree

1 file changed

+68
-8
lines changed

1 file changed

+68
-8
lines changed

README.md

Lines changed: 68 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -90,6 +90,8 @@ const cmd = command({
9090
});
9191

9292
```
93+
94+
### Option builder
9395
Initial builder functions:
9496

9597
- `string(name?: string)` - defines option as a string-type option which requires data to be passed as `--option=value` or `--option value`
@@ -202,6 +204,7 @@ commands.push(command({
202204
name: 'command',
203205
aliases: ['c', 'cmd'],
204206
desc: 'Description goes here',
207+
shortDesc: 'Short description'
205208
hidden: false,
206209
options: commandOptions,
207210
transform: (options) => {
@@ -234,15 +237,17 @@ Parameters:
234237

235238
- `hidden` - sets command as hidden - if `true`, command will be omitted from being displayed in `help` command
236239

237-
- `options` - object containing command options created using `string()` and `boolean()` functions
240+
- `options` - object containing command options created using `string` and `boolean` functions
238241

239242
- `transform` - optional function to preprocess options before they are passed to handler
240243
:warning: - type of return mutates type of handler's input
241244

242245
- `handler` - function, which will be executed in case of successful option parse
246+
:warning: - must be present if your command doesn't have subcommands
247+
If command has subcommands but no handler, help for this command is going to be called instead of handler
243248

244249
- `help` - function or string, which will be executed or printed when help is called for this command
245-
:warning: - must be present if your command doesn't have subcommands, otherwise defaults to help for command
250+
:warning: - if passed, takes prevalence over theme's `commandHelp` event
246251

247252
- `subcommands` - subcommands for command
248253
:warning: - command can't have subcommands and `positional` options at the same time
@@ -251,7 +256,7 @@ Parameters:
251256

252257
### Running commands
253258

254-
After defining commands, you're going to need to execute `run()` function to start command execution
259+
After defining commands, you're going to need to execute `run` function to start command execution
255260

256261
```Typescript
257262
import { command, type Command, run, string, boolean, type TypeOf } from '@drizzle-team/brocli'
@@ -280,20 +285,75 @@ commands.push(command({
280285
// And so on...
281286

282287
run(commands, {
283-
name: 'my-program',
288+
cliName: 'mysoft',
289+
omitKeysOfUndefinedOptions: true,
290+
argSource: customEnvironmentArgvStorage,
284291
version: '1.0.0',
285292
help: () => {
286293
console.log('Command list:');
287294
commands.forEach(c => console.log('This command does ... and has options ...'));
295+
},
296+
theme: async (event) => {
297+
if (event.type === 'commandHelp') {
298+
await myCustomUniversalCommandHelp(event.command);
299+
300+
return true;
301+
}
302+
303+
if (event.type === 'unknownError') {
304+
console.log('Something went wrong...');
305+
306+
return true;
307+
}
308+
309+
return false;
310+
},
311+
hook: (event, command) => {
312+
if(event === 'before') console.log(`Command '${command.name}' started`)
313+
if(event === 'after') console.log(`Command '${command.name}' succesfully finished it's work`)
288314
}
289315
})
290316
```
291317

292-
:speech_balloon: - in case cli arguments are not stored in `process.argv` in your environment, you can pass custom argument source to a second argument of `run()`, however note that first two elements of such source will be ignored as they are expected to store executable and executed file paths instead of args.
293-
:speech_balloon: - custom help and version output handlers or strings can be passed to a second argument to replace default brocli outputs for those operations with your own.
318+
Parameters:
319+
320+
- `cliName` - name that's used to invoke your application from cli.
321+
Used for themes that print usage examples, example:
322+
`app do-task --help` results in `Usage: app do-task <positional> ...`
323+
Default: `undefined`
324+
325+
- `omitKeysOfUndefinedOptions` - flag that determines whether undefined options will be passed to transform\handler or not
326+
Default: `false`
327+
328+
- `argSource` - location of array of args in your environment
329+
:warning: - first two items of this storage will be ignored as they typically contain executable and executed file paths
330+
Default: `process.argv`
331+
332+
- `version` - string or handler used to print your app version
333+
:warning: - if passed, takes prevalence over theme's version event
334+
335+
- `help` - string or handler used to print your app's global help
336+
:warning: - if passed, takes prevalence over theme's `globalHelp` event
337+
338+
- `theme(event: BroCliEvent)` - function that's used to customize messages that are printed on various events
339+
Return:
340+
`true` | `Promise<true>` if you consider event processed
341+
`false` | `Promise<false>` to redirect event to default theme
342+
343+
- `hook(event: EventType, command: Command)` - function that's used to execute code before and after every command's `transform` and `handler` execution
344+
345+
### Additional functions
346+
347+
- `commandsInfo(commands: Command[])` - get simplified representation of your command collection
348+
Can be used to generate docs
349+
350+
- `test(command: Command, args: string)` - test behaviour for command with specified arguments
351+
:warning: - if command has `transform`, it will get called, however `handler` won't
352+
353+
- `getCommandNameWithParents(command: Command)` - get subcommand's name with parent command names
294354

295355
## CLI
296356

297-
In `BroCLI`, command doesn't have to be the first argument, instead it may be contained in any order.
298-
To make this possible, hovewer, option that's stated right before command should have an explicit value, even if it is a flag: `--verbose true <command-name>` (does not apply to reserved flags: [ `--help` | `-h` | `--version` | `-v`])
357+
In `BroCLI`, command doesn't have to be the first argument, instead it may be passed in any order.
358+
To make this possible, hovewer, option that's passed right before command should have an explicit value, even if it is a flag: `--verbose true <command-name>` (does not apply to reserved flags: [ `--help` | `-h` | `--version` | `-v`])
299359
Options are parsed in strict mode, meaning that having any unrecognized options will result in an error.

0 commit comments

Comments
 (0)