adde .onArgs(..) and .onNoArgs(..) pre-parsing events...

Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
Alex A. Naanou 2020-08-23 11:09:11 +03:00
parent 889007f85a
commit 81a45fd839
5 changed files with 89 additions and 11 deletions

View File

@ -47,6 +47,8 @@ For basics see [README.md](./README.md)
- [`THEN` / `STOP`](#then--stop)
- [`ParserError(..)`](#parsererror)
- [`Parser(..)`](#parser)
- [`<parser>.onArgs(..)`](#parseronargs)
- [`<parser>.onNoArgs(..)`](#parseronnoargs)
- [`<parser>.then(..)`](#parserthen)
- [`<parser>.stop(..)`](#parserstop)
- [`<parser>.error(..)`](#parsererror-1)
@ -643,6 +645,38 @@ Parser(<spec>)
See [`<parser>(..)`](#parser-1) for more info.
#### `<parser>.onArgs(..)`
Add callback triggered when one or more arguments are passed to the parser.
```
<parser>.onArgs(<callback>)
-> <parser>
```
```
callback(<args>)
-> <obj>
```
Note that this is triggered _before_ parsing begins.
#### `<parser>.onNoArgs(..)`
Add callback triggered when no arguments are passed to the parser.
```
<parser>.onNoArgs(<callback>)
-> <parser>
```
```
callback(<args>)
-> <obj>
```
Note that this is triggered _before_ parsing begins.
#### `<parser>.then(..)`
Add callback to `then` "event".

View File

@ -65,6 +65,7 @@ This code is an evolution of that parser.
- [Nested parsers](#nested-parsers)
- [Stopping](#stopping)
- [Error reporting](#error-reporting)
- [Before parsing begins](#before-parsing-begins)
- [Handling the result](#handling-the-result)
- [Calling the script](#calling-the-script)
- [Advanced docs](#advanced-docs)
@ -479,6 +480,25 @@ And to close things off for the `<spec>` ;)
})
```
### Before parsing begins
The `<parser>` can notify the user if any arguments were passed or not before the parsing starts:
- [`<parser>.onArgs(..)`](./ADVANCED.md#parseronargs) triggered when one or
more arguments were passed
```javascript
.onArgs(function(args){
console.log('### input arguments:', args) })
```
- [`<parser>.onNoArgs(..)`](./ADVANCED.md#parseronnoargs) triggered when no
arguments were passed
```javascript
.onNoArgs(function(args){
console.log('### no arguments passed.') })
```
### Handling the result
The `<parser>` will call different sets of callbacks on different stop conditions:

28
argv.js
View File

@ -15,6 +15,9 @@
* Repo and docs:
* https://github.com/flynx/argv.js
*
* TODO:
* .onNoArgs(..) / onArgs(..) callbacks...
*
*
**********************************************************************/
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
@ -95,8 +98,12 @@ module.extra = {}
// -> func
//
//
// func(..)
// Bind a callback...
// func(callback)
// -> this
//
// Trigger callbacks...
// func(..)
// -> res
//
// pre_action(...args)
@ -911,7 +918,17 @@ object.Constructor('Parser', {
return this },
// Post parsing callbacks...
// Pre-parsing callbacks...
//
// .onArgs(callback(args))
//
// .onNoArgs(callback())
//
onArgs: afterCallback('onArgs'),
onNoArgs: afterCallback('onNoArgs'),
// Post-parsing callbacks...
//
// .then(callback(unhandled, root_value, rest))
//
@ -954,6 +971,7 @@ object.Constructor('Parser', {
var that = this
var parsed = Object.create(this)
var opt_pattern = parsed.optionInputPattern
// prep argv...
var rest = parsed.rest =
argv == null ?
(typeof(process) != 'unhandled' ?
@ -973,7 +991,6 @@ object.Constructor('Parser', {
if(main != null && rest[0] == process.execPath){
rest.splice(0, 2)
rest.unshift(main) }
// script stuff...
var script = parsed.script = rest.shift()
var basename = script.split(/[\\\/]/).pop()
@ -981,6 +998,11 @@ object.Constructor('Parser', {
parsed.scriptPath = script.slice(0,
script.length - parsed.scriptName.length)
// call the pre-parse handlers...
rest.length == 0 ?
this.onNoArgs(rest)
: this.onArgs(rest)
// helpers...
// XXX should this pass the error as-is to the API???
var handleError = function(reason, arg, rest){

View File

@ -148,16 +148,17 @@ argv.Parser({
handler: function(){
throw 'something went really wrong.' } },
})
.onArgs(function(args){
console.log('### input arguments:', args) })
.onNoArgs(function(args){
console.log('### no arguments passed.') })
.then(function(unhandled, root_value, rest){
console.log('### finished normally.')
console.log(this)
})
console.log(this) })
.stop(function(arg, rest){
console.log(`### stopped at ${arg}.`)
})
console.log(`### stopped at ${arg}.`) })
.error(function(reason, arg, rest){
console.log(`### something went wrong when parsing ${arg}.`)
})
console.log(`### something went wrong when parsing ${arg}.`) })
// run the parser...

View File

@ -1,6 +1,6 @@
{
"name": "ig-argv",
"version": "2.13.2",
"version": "2.14.0",
"description": "simple argv parser",
"main": "argv.js",
"scripts": {
@ -28,5 +28,6 @@
"devDependencies": {
"colors": "^1.4.0",
"ig-test": "^1.4.3"
}
},
"vim-cfg": " vim:set ts=2 sw=2 expandtab : "
}