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)
|
- [`THEN` / `STOP`](#then--stop)
|
||||||
- [`ParserError(..)`](#parsererror)
|
- [`ParserError(..)`](#parsererror)
|
||||||
- [`Parser(..)`](#parser)
|
- [`Parser(..)`](#parser)
|
||||||
|
- [`<parser>.onArgs(..)`](#parseronargs)
|
||||||
|
- [`<parser>.onNoArgs(..)`](#parseronnoargs)
|
||||||
- [`<parser>.then(..)`](#parserthen)
|
- [`<parser>.then(..)`](#parserthen)
|
||||||
- [`<parser>.stop(..)`](#parserstop)
|
- [`<parser>.stop(..)`](#parserstop)
|
||||||
- [`<parser>.error(..)`](#parsererror-1)
|
- [`<parser>.error(..)`](#parsererror-1)
|
||||||
@ -643,6 +645,38 @@ Parser(<spec>)
|
|||||||
See [`<parser>(..)`](#parser-1) for more info.
|
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(..)`
|
#### `<parser>.then(..)`
|
||||||
|
|
||||||
Add callback to `then` "event".
|
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)
|
- [Nested parsers](#nested-parsers)
|
||||||
- [Stopping](#stopping)
|
- [Stopping](#stopping)
|
||||||
- [Error reporting](#error-reporting)
|
- [Error reporting](#error-reporting)
|
||||||
|
- [Before parsing begins](#before-parsing-begins)
|
||||||
- [Handling the result](#handling-the-result)
|
- [Handling the result](#handling-the-result)
|
||||||
- [Calling the script](#calling-the-script)
|
- [Calling the script](#calling-the-script)
|
||||||
- [Advanced docs](#advanced-docs)
|
- [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
|
### Handling the result
|
||||||
|
|
||||||
The `<parser>` will call different sets of callbacks on different stop conditions:
|
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:
|
* Repo and docs:
|
||||||
* https://github.com/flynx/argv.js
|
* https://github.com/flynx/argv.js
|
||||||
*
|
*
|
||||||
|
* TODO:
|
||||||
|
* .onNoArgs(..) / onArgs(..) callbacks...
|
||||||
|
*
|
||||||
*
|
*
|
||||||
**********************************************************************/
|
**********************************************************************/
|
||||||
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
|
((typeof define)[0]=='u'?function(f){module.exports=f(require)}:define)
|
||||||
@ -95,8 +98,12 @@ module.extra = {}
|
|||||||
// -> func
|
// -> func
|
||||||
//
|
//
|
||||||
//
|
//
|
||||||
// func(..)
|
// Bind a callback...
|
||||||
|
// func(callback)
|
||||||
// -> this
|
// -> this
|
||||||
|
//
|
||||||
|
// Trigger callbacks...
|
||||||
|
// func(..)
|
||||||
// -> res
|
// -> res
|
||||||
//
|
//
|
||||||
// pre_action(...args)
|
// pre_action(...args)
|
||||||
@ -911,7 +918,17 @@ object.Constructor('Parser', {
|
|||||||
return this },
|
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))
|
// .then(callback(unhandled, root_value, rest))
|
||||||
//
|
//
|
||||||
@ -954,6 +971,7 @@ object.Constructor('Parser', {
|
|||||||
var that = this
|
var that = this
|
||||||
var parsed = Object.create(this)
|
var parsed = Object.create(this)
|
||||||
var opt_pattern = parsed.optionInputPattern
|
var opt_pattern = parsed.optionInputPattern
|
||||||
|
// prep argv...
|
||||||
var rest = parsed.rest =
|
var rest = parsed.rest =
|
||||||
argv == null ?
|
argv == null ?
|
||||||
(typeof(process) != 'unhandled' ?
|
(typeof(process) != 'unhandled' ?
|
||||||
@ -973,7 +991,6 @@ object.Constructor('Parser', {
|
|||||||
if(main != null && rest[0] == process.execPath){
|
if(main != null && rest[0] == process.execPath){
|
||||||
rest.splice(0, 2)
|
rest.splice(0, 2)
|
||||||
rest.unshift(main) }
|
rest.unshift(main) }
|
||||||
|
|
||||||
// script stuff...
|
// script stuff...
|
||||||
var script = parsed.script = rest.shift()
|
var script = parsed.script = rest.shift()
|
||||||
var basename = script.split(/[\\\/]/).pop()
|
var basename = script.split(/[\\\/]/).pop()
|
||||||
@ -981,6 +998,11 @@ object.Constructor('Parser', {
|
|||||||
parsed.scriptPath = script.slice(0,
|
parsed.scriptPath = script.slice(0,
|
||||||
script.length - parsed.scriptName.length)
|
script.length - parsed.scriptName.length)
|
||||||
|
|
||||||
|
// call the pre-parse handlers...
|
||||||
|
rest.length == 0 ?
|
||||||
|
this.onNoArgs(rest)
|
||||||
|
: this.onArgs(rest)
|
||||||
|
|
||||||
// helpers...
|
// helpers...
|
||||||
// XXX should this pass the error as-is to the API???
|
// XXX should this pass the error as-is to the API???
|
||||||
var handleError = function(reason, arg, rest){
|
var handleError = function(reason, arg, rest){
|
||||||
|
|||||||
@ -148,16 +148,17 @@ argv.Parser({
|
|||||||
handler: function(){
|
handler: function(){
|
||||||
throw 'something went really wrong.' } },
|
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){
|
.then(function(unhandled, root_value, rest){
|
||||||
console.log('### finished normally.')
|
console.log('### finished normally.')
|
||||||
console.log(this)
|
console.log(this) })
|
||||||
})
|
|
||||||
.stop(function(arg, rest){
|
.stop(function(arg, rest){
|
||||||
console.log(`### stopped at ${arg}.`)
|
console.log(`### stopped at ${arg}.`) })
|
||||||
})
|
|
||||||
.error(function(reason, arg, rest){
|
.error(function(reason, arg, rest){
|
||||||
console.log(`### something went wrong when parsing ${arg}.`)
|
console.log(`### something went wrong when parsing ${arg}.`) })
|
||||||
})
|
|
||||||
|
|
||||||
|
|
||||||
// run the parser...
|
// run the parser...
|
||||||
|
|||||||
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "ig-argv",
|
"name": "ig-argv",
|
||||||
"version": "2.13.2",
|
"version": "2.14.0",
|
||||||
"description": "simple argv parser",
|
"description": "simple argv parser",
|
||||||
"main": "argv.js",
|
"main": "argv.js",
|
||||||
"scripts": {
|
"scripts": {
|
||||||
@ -28,5 +28,6 @@
|
|||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
"colors": "^1.4.0",
|
"colors": "^1.4.0",
|
||||||
"ig-test": "^1.4.3"
|
"ig-test": "^1.4.3"
|
||||||
}
|
},
|
||||||
|
"vim-cfg": " vim:set ts=2 sw=2 expandtab : "
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user