mirror of
https://github.com/flynx/argv.js.git
synced 2025-10-28 10:20:09 +00:00
adde .onArgs(..) and .onNoArgs(..) pre-parsing events...
Signed-off-by: Alex A. Naanou <alex.nanou@gmail.com>
This commit is contained in:
parent
889007f85a
commit
81a45fd839
34
ADVANCED.md
34
ADVANCED.md
@ -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".
|
||||
|
||||
20
README.md
20
README.md
@ -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
28
argv.js
@ -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){
|
||||
|
||||
@ -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...
|
||||
|
||||
@ -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 : "
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user