Comparing version 1.2.2 to 1.3.0
# node-cmdln Changelog | ||
## 1.3.0 | ||
- Add a `Cmdln.emptyLine` hook that is called when no argv is given, i.e. | ||
when your command is called with no args: | ||
$ mycmd | ||
The default behaviour (as before) is to print help output. | ||
A **change in default behaviour** is that this will now exit non-zero. If | ||
you want different behaviour, then override `emptyLine()` in your Cmdln | ||
subclass. | ||
- Improve the `cmdln.main` convenience function's printing of error messages. | ||
An `options.showCode` has been added to allow printing error instances' | ||
`code` attribute, if defined. E.g., with this usage: | ||
cmdln.main(MyCmd, process.argv, {showCode: true}); | ||
You get this output for errors (in this example the error is an unknown | ||
subcommand): | ||
$ node mycmd.js bogus | ||
mycmd bogus: error (UnknownCommand): unknown command: "bogus" | ||
## 1.2.2 | ||
@@ -4,0 +29,0 @@ |
@@ -119,4 +119,13 @@ /* | ||
function NoCommandError() { | ||
CmdlnError.call(this, { | ||
message: 'no command given', | ||
code: 'NoCommand', | ||
exitStatus: 1 | ||
}); | ||
} | ||
util.inherits(NoCommandError, CmdlnError); | ||
// ---- Cmdln object | ||
@@ -209,3 +218,3 @@ | ||
* error object if there was a problem, and subcmd is the sub-command | ||
* string if run. | ||
* string (if there is one, i.e. it might be undefined). | ||
*/ | ||
@@ -237,3 +246,3 @@ Cmdln.prototype.main = function main(argv, callback) { | ||
if (args.length === 0) { | ||
self.printHelp(function (helpErr) { callback(helpErr); }); | ||
self.emptyLine(callback); | ||
return; | ||
@@ -256,2 +265,17 @@ } | ||
/** | ||
* Handler called for an empty line of input. By default this prints help | ||
* output and returns a `NoCommandError` (exitStatus == 1). | ||
* | ||
* Dev Note: Equiv to python-cmdln's Cmdln.emptyline. | ||
* | ||
* @param callback {Function} `function (err)` | ||
*/ | ||
Cmdln.prototype.emptyLine = function emptyLine(callback) { | ||
this.printHelp(function (helpErr) { | ||
callback(helpErr || new NoCommandError()); | ||
}); | ||
}; | ||
/** | ||
* Post-option processing initialization of this Cmdln instance. | ||
@@ -281,3 +305,2 @@ * | ||
/** | ||
@@ -418,9 +441,26 @@ * Print top-level tool help. | ||
* | ||
* This does not have a callback because it calls `process.exit` (with an | ||
* appropriate exit status). | ||
* | ||
* @param cmdClass {Function} The Cmdln subclass ctor. | ||
* @param argv {Array} The argv to process. Optional. Default is `process.argv`.0 | ||
* @param argv {Array} The argv to process. Optional. Default is `process.argv`. | ||
* @param options {Object} | ||
* - `showCode` {Boolean} Default false. Whether to show the error `code` | ||
* in the stderr output, if available on the error objects returned | ||
* by subcommands. E.g.: | ||
* fash: error: no command given # showCode=false | ||
* fash: error (NoCommand): no command given # showCode=true | ||
* See the doc on the `CmdlnError` class above for details on the `code`. | ||
*/ | ||
function main(cmdClass, argv) { | ||
function main(cmdClass, argv, options) { | ||
assert.func(cmdClass, 'cmdClass'); | ||
assert.optionalArrayOfString(argv, 'argv'); | ||
if (!argv) argv = process.argv; | ||
assert.optionalObject(options, 'options'); | ||
if (!options) { | ||
options = {}; | ||
} | ||
assert.optionalBool(options.showCode, 'options.showCode'); | ||
if (!argv) { | ||
argv = process.argv; | ||
} | ||
@@ -430,7 +470,14 @@ var cli = new cmdClass(); | ||
if (err) { | ||
console.error('%s%s: error%s: %s', | ||
cli.name, | ||
(subcmd ? ' ' + subcmd : ''), | ||
(false && err.code ? format(' (%s)', err.code) : ''), | ||
(process.env.DEBUG ? err.stack : err.message)); | ||
// If the `err` has no "message" field, then this probably isn't | ||
// and Error instance. Let's just not print an error message. This | ||
// can happen if the subcmd passes back `true` or similar to | ||
// indicate "yes there was an error". | ||
if (err.message !== undefined) { | ||
var code = (err.body ? err.body.code : err.code); | ||
console.error('%s%s: error%s: %s', | ||
cli.name, | ||
(subcmd ? ' ' + subcmd : ''), | ||
((options.showCode && code) ? format(' (%s)', code) : ''), | ||
(process.env.DEBUG ? err.stack : err.message)); | ||
} | ||
process.exit(err.exitStatus || 1); | ||
@@ -443,2 +490,3 @@ } | ||
// ---- exports | ||
@@ -445,0 +493,0 @@ |
{ | ||
"name": "cmdln", | ||
"version": "1.2.2", | ||
"version": "1.3.0", | ||
"description": "helper lib for creating CLI tools with subcommands; think `git`, `svn`, `zfs`", | ||
@@ -5,0 +5,0 @@ "author": "Trent Mick (http://trentm.com)", |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
83960
821