diff --git a/ADVANCED.md b/ADVANCED.md index 1e0558d..38ba8ae 100644 --- a/ADVANCED.md +++ b/ADVANCED.md @@ -47,6 +47,8 @@ For basics see [README.md](./README.md) - [`THEN` / `STOP`](#then--stop) - [`ParserError(..)`](#parsererror) - [`Parser(..)`](#parser) + - [`.onArgs(..)`](#parseronargs) + - [`.onNoArgs(..)`](#parseronnoargs) - [`.then(..)`](#parserthen) - [`.stop(..)`](#parserstop) - [`.error(..)`](#parsererror-1) @@ -643,6 +645,38 @@ Parser() See [`(..)`](#parser-1) for more info. +#### `.onArgs(..)` + +Add callback triggered when one or more arguments are passed to the parser. +``` +.onArgs() + -> +``` + +``` +callback() + -> +``` + +Note that this is triggered _before_ parsing begins. + + +#### `.onNoArgs(..)` + +Add callback triggered when no arguments are passed to the parser. +``` +.onNoArgs() + -> +``` + +``` +callback() + -> +``` + +Note that this is triggered _before_ parsing begins. + + #### `.then(..)` Add callback to `then` "event". diff --git a/README.md b/README.md index 1e2e0a9..e626027 100644 --- a/README.md +++ b/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 `` ;) }) ``` +### Before parsing begins + +The `` can notify the user if any arguments were passed or not before the parsing starts: + +- [`.onArgs(..)`](./ADVANCED.md#parseronargs) triggered when one or + more arguments were passed + ```javascript + .onArgs(function(args){ + console.log('### input arguments:', args) }) + ``` + +- [`.onNoArgs(..)`](./ADVANCED.md#parseronnoargs) triggered when no + arguments were passed + ```javascript + .onNoArgs(function(args){ + console.log('### no arguments passed.') }) + ``` + + ### Handling the result The `` will call different sets of callbacks on different stop conditions: diff --git a/argv.js b/argv.js index 3350b74..f46589c 100644 --- a/argv.js +++ b/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){ diff --git a/examples/options.js b/examples/options.js index d62ff9a..0983c0f 100644 --- a/examples/options.js +++ b/examples/options.js @@ -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... diff --git a/package.json b/package.json index 4c9b211..eee3905 100644 --- a/package.json +++ b/package.json @@ -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 : " }