Comparing version 4.0.0 to 4.1.0
# node-cmdln Changelog | ||
## 4.1.0 | ||
- [Potentially backward incompatible change] Change `cmdln.main()` behaviour on | ||
complete to attempt to "soft exit", by which I mean attempt to avoid calling | ||
`process.exit(code)`, because with node.js that means std handles won't | ||
necessarily be flushed before process exit. Starting in node.js 0.12 | ||
`process.exitCode` was added to set the exit code without the hard exit. | ||
Warning: A side-effect of avoiding `process.exit()` is that apps using | ||
`cmdln.main()` that have active handles open will now *hang* instead | ||
of exiting. To get the old behaviour, use: | ||
cmdln.main(cli, {finale: 'exit'}); | ||
- [issue #11] Add `finale` and `callback` options to `cmdln.main(<cli>, | ||
<options>)`. `finale` defines what to do when done. Valid values are: | ||
- softexit (the default): set `process.exitCode` if supported, else call | ||
`process.exit()` | ||
- exit: call `process.exit()` which can result in std handles not being flushed | ||
- callback: call the given `options.callback` | ||
- none: Do nothing | ||
## 4.0.0 | ||
@@ -4,0 +28,0 @@ |
@@ -1325,2 +1325,9 @@ /* | ||
* `process.argv`. | ||
* - `finale` {String} Optional, default 'softexit'. What to do when | ||
* done. Options are 'softexit' (set `process.exitCode` if supported, | ||
* else call `process.exit()`), 'exit' (call `process.exit()` which | ||
* can result in std handles not being flushed), 'callback' (call | ||
* the given `options.callback`), or 'none'. | ||
* - `callback` {Function} Optional. A function called `function (err)` | ||
* if `options.finale === "callback"`. | ||
* - `showErr` {Boolean} Optional. Whether to show (i.e. print via | ||
@@ -1397,2 +1404,18 @@ * `console.error(...)` an error. If not set, then `<cli>.showErr` | ||
var VALID_FINALES = ['softexit', 'exit', 'callback', 'none']; | ||
var finale; | ||
if (options.hasOwnProperty('finale')) { | ||
assert.ok(VALID_FINALES.indexOf(options.finale) !== -1, | ||
format('invalid options.finale "%s": valid values are "%s"', | ||
options.finale, '", "'.join(VALID_FINALES))); | ||
finale = options.finale | ||
} else { | ||
finale = 'softexit'; | ||
} | ||
if (options.hasOwnProperty('callback')) { | ||
assert.func(options.callback, 'options.callback'); | ||
assert.equal(finale, 'callback', | ||
'options.callback provided, but options.finale is not "callback"'); | ||
} | ||
cli.main(options.argv, function (err) { | ||
@@ -1433,3 +1456,36 @@ var exitStatus = (err ? err.exitStatus || 1 : 0); | ||
} | ||
process.exit(exitStatus); | ||
if (finale === 'exit') { | ||
process.exit(exitStatus); | ||
} else if (finale === 'softexit') { | ||
/* | ||
* We'd like to NOT use `process.exit` because node then doesn't in | ||
* general allow std handles to flush. For some node versions it | ||
* *will* flush if stdout is a TTY. However, you are then screwed | ||
* when piping output to anything. IOW, that is no help. | ||
* | ||
* In node 0.12, `process.exitCode` provided a way to set the exit | ||
* code without the hard immediate `process.exit()`. | ||
* | ||
* Note: A side-effect of avoiding `process.exit()` if we can | ||
* manage it, is that a node tool using this that still has active | ||
* handles will hang instead of exiting. If that is you, use | ||
* `finale: "exit"`. | ||
*/ | ||
var supportsProcessExitCode = true; | ||
var nodeVer = process.versions.node.split('.').map(Number); | ||
if (nodeVer[0] === 0 && nodeVer[1] <= 10) { | ||
supportsProcessExitCode = false; | ||
} | ||
if (supportsProcessExitCode) { | ||
process.exitCode = exitStatus; | ||
} else if (exitStatus !== 0) { | ||
process.exit(exitStatus); | ||
} | ||
} else if (finale === 'callback') { | ||
if (options.callback) { | ||
options.callback(err); | ||
} | ||
} | ||
}); | ||
@@ -1436,0 +1492,0 @@ } |
{ | ||
"name": "cmdln", | ||
"version": "4.0.0", | ||
"version": "4.1.0", | ||
"description": "helper lib for creating CLI tools with subcommands; think `git`, `svn`, `zfs`", | ||
@@ -5,0 +5,0 @@ "author": "Trent Mick (http://trentm.com)", |
@@ -9,2 +9,7 @@ # errHelp | ||
* imgadm optionfor this) for parsable JSON last error line. | ||
Some fields that might be nice to have on this summary line: | ||
argv: original argv | ||
err: if there was an error, details here: message, code, stack | ||
exitStatus | ||
elapsedS | ||
@@ -11,0 +16,0 @@ |
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
88145
1365
8