Skip to content

Commit

Permalink
First cut at README
Browse files Browse the repository at this point in the history
  • Loading branch information
shadowspawn committed May 9, 2021
1 parent 5b7349d commit 712697e
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ Read this in other languages: English | [简体中文](./Readme_zh-CN.md)
- [Custom argument processing](#custom-argument-processing)
- [Action handler](#action-handler)
- [Stand-alone executable (sub)commands](#stand-alone-executable-subcommands)
- [Life cycle hooks](#life-cycle-hooks)
- [Automated help](#automated-help)
- [Custom help](#custom-help)
- [Display help from code](#display-help-from-code)
Expand Down Expand Up @@ -554,6 +555,35 @@ program.parse(process.argv);
If the program is designed to be installed globally, make sure the executables have proper modes, like `755`.
## Life cycle hooks
You can add callback hooks to a command for life cycle events.
Example file: [hook](./examples/hook.js)
```js
program
.option('-t, --trace', 'display trace statements for commands')
.hook('beforeAction', (context) => {
if (context.hookedCommand.opts().trace) {
const actionCommand = context.command;
console.log(`About to call action handler for subcommand: ${actionCommand.name()}`);
console.log('arguments: %O', actionCommand.args);
console.log('options: %o', actionCommand.opts());
}
});
```
The supported events are:
- `beforeAction`: called before action handler for this command and its subcommands
- `afterAction`: called after action handler for this command and its subcommands
The hook callback is passed a context object with properties:
- `command`: the command running the action handler
- `hookedCommand`: the command which the hook was added to
## Automated help
The help information is auto-generated based on the information commander already knows about your program. The default
Expand Down

0 comments on commit 712697e

Please sign in to comment.