Comparing version 0.2.8 to 0.3.0
164
cli.js
/** | ||
* Copyright (c) 2010 Chris O'Hara <cohara87@gmail.com> | ||
* | ||
* | ||
* Permission is hereby granted, free of charge, to any person obtaining | ||
@@ -11,6 +11,6 @@ * a copy of this software and associated documentation files (the | ||
* the following conditions: | ||
* | ||
* | ||
* The above copyright notice and this permission notice shall be | ||
* included in all copies or substantial portions of the Software. | ||
* | ||
* | ||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, | ||
@@ -25,5 +25,5 @@ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF | ||
//Note: cli includes kof/node-natives and creationix/stack. I couldn't find | ||
//Note: cli includes kof/node-natives and creationix/stack. I couldn't find | ||
//license information for either - contact me if you want your license added | ||
var cli = exports, | ||
@@ -48,3 +48,3 @@ argv, curr_opt, curr_val, full_opt, is_long, | ||
* Bind kof's node-natives (https://github.com/kof/node-natives) to `cli.native` | ||
* | ||
* | ||
* Rather than requiring node natives (e.g. var fs = require('fs')), all | ||
@@ -77,3 +77,3 @@ * native modules can be accessed like `cli.native.fs` | ||
* `cli.disable(plugin1, [plugin2, ...])` | ||
* | ||
* | ||
* Methods are chainable - `cli.enable(plugin).disable(plugin2)`. | ||
@@ -150,14 +150,16 @@ * | ||
*/ | ||
cli.setArgv = function (arr, keep_arg0) { | ||
if (!(arr instanceof Array)) { | ||
arr = arr.split(' '); | ||
cli.setArgv = function (argv, keep_arg0) { | ||
if (typeof argv == 'string') { | ||
argv = argv.split(' '); | ||
} else { | ||
argv = argv.slice(); | ||
} | ||
cli.app = arr.shift(); | ||
cli.app = argv.shift(); | ||
//Strip off argv[0] if it's 'node' | ||
if (!keep_arg0 && 'node' === cli.app) { | ||
cli.app = arr.shift(); | ||
if (!keep_arg0 && 'node' === cli.native.path.basename(cli.app)) { | ||
cli.app = argv.shift(); | ||
} | ||
cli.app = cli.native.path.basename(cli.app); | ||
argv_parsed = false; | ||
cli.args = cli.argv = argv = arr; | ||
cli.args = cli.argv = argv; | ||
cli.argc = argv.length; | ||
@@ -178,5 +180,5 @@ }; | ||
} | ||
curr_val = null; | ||
//If we're currently in a group of short opts (e.g. -abc), return the next opt | ||
@@ -188,9 +190,9 @@ if (short_tags.length) { | ||
} | ||
if (!argv.length) { | ||
return false; | ||
} | ||
curr_opt = argv.shift(); | ||
//If an escape sequence is found (- or --), subsequent opts are ignored | ||
@@ -203,3 +205,3 @@ if (curr_opt === '-' || curr_opt === '--') { | ||
} | ||
//If the next element in argv isn't an opt, add it to the list of args | ||
@@ -214,3 +216,3 @@ if (curr_opt[0] !== '-') { | ||
} | ||
//Accept grouped short opts, e.g. -abc => -a -b -c | ||
@@ -221,5 +223,5 @@ if (!is_long && curr_opt.length > 1) { | ||
} | ||
var eq, len; | ||
//Check if the long opt is in the form --option=VALUE | ||
@@ -240,6 +242,6 @@ if (is_long && (eq = curr_opt.indexOf('=')) >= 0) { | ||
} | ||
//Save the opt representation for later | ||
full_opt = (is_long ? '--' : '-') + curr_opt; | ||
return curr_opt; | ||
@@ -260,3 +262,3 @@ }; | ||
* See README.md for more information. | ||
* | ||
* | ||
* @param {Object} opts | ||
@@ -395,3 +397,3 @@ * @param {Object} commands (optional) | ||
* | ||
* @param {String} command | ||
* @param {String} command | ||
* @return {String} full_command | ||
@@ -416,3 +418,3 @@ * @api public | ||
for (j = 0; j < l; j++) | ||
if (list[j].length >= i && list[j][i] === command[i]) | ||
if (list[j].length >= i && list[j][i] === command[i]) | ||
tmp_list.push(list[j]); | ||
@@ -433,12 +435,12 @@ list = tmp_list; | ||
/** | ||
* Adds methods to output styled status messages to stderr. | ||
* Adds methods to output styled status messages to stderr. | ||
* | ||
* Added methods are cli.info(msg), cli.error(msg), cli.ok(msg), and | ||
* Added methods are cli.info(msg), cli.error(msg), cli.ok(msg), and | ||
* cli.debug(msg). | ||
* | ||
* To control status messages, use the 'status' plugin | ||
* 1) debug() messages are hidden by default. Display them with | ||
* 1) debug() messages are hidden by default. Display them with | ||
* the --debug opt. | ||
* 2) to hide all status messages, use the -s or --silent opt. | ||
* | ||
* | ||
* @api private | ||
@@ -449,9 +451,9 @@ */ | ||
switch (type) { | ||
case 'info': | ||
pre = no_color ? 'INFO:' : '\x1B[33mINFO\x1B[0m:'; | ||
case 'info': | ||
pre = no_color ? 'INFO:' : '\x1B[33mINFO\x1B[0m:'; | ||
break; | ||
case 'debug': | ||
pre = no_color ? 'DEBUG:' : '\x1B[36mDEBUG\x1B[0m:'; | ||
pre = no_color ? 'DEBUG:' : '\x1B[36mDEBUG\x1B[0m:'; | ||
break; | ||
case 'error': | ||
case 'error': | ||
case 'fatal': | ||
@@ -461,4 +463,4 @@ pre = no_color ? 'ERROR:' : '\x1B[31mERROR\x1B[0m:'; | ||
case 'ok': | ||
pre = no_color ? 'OK:' : '\x1B[32mOK\x1B[0m:'; | ||
break; | ||
pre = no_color ? 'OK:' : '\x1B[32mOK\x1B[0m:'; | ||
break; | ||
} | ||
@@ -487,3 +489,3 @@ msg = pre + ' ' + msg; | ||
* setApp('./package.json'); //Pull name/version from package.json | ||
* | ||
* | ||
* @param {String} name | ||
@@ -504,5 +506,5 @@ * @return cli (for chaining) | ||
/** | ||
* Parses the version number from package.json. If no path is specified, cli | ||
* Parses the version number from package.json. If no path is specified, cli | ||
* will attempt to locate a package.json in ./, ../ or ../../ | ||
* | ||
* | ||
* @param {String} path (optional) | ||
@@ -534,3 +536,3 @@ * @api public | ||
try_all([ | ||
__dirname + '/package.json', | ||
__dirname + '/package.json', | ||
__dirname + '/../package.json', | ||
@@ -546,3 +548,3 @@ __dirname + '/../../package.json' | ||
* Sets the usage string - default is `app [OPTIONS] [ARGS]`. | ||
* | ||
* | ||
* @param {String} u | ||
@@ -570,5 +572,5 @@ * @return cli (for chaining) | ||
/** | ||
* Automatically build usage information from the opts list. If the help | ||
* Automatically build usage information from the opts list. If the help | ||
* plugin is enabled (default), this info is displayed with -h, --help. | ||
* | ||
* | ||
* @api public | ||
@@ -579,6 +581,6 @@ */ | ||
switch_pad = 25; | ||
var trunc_desc = function (pref, desc, len) { | ||
var pref_len = pref.length, | ||
desc_len = cli.width - pref_len, | ||
desc_len = cli.width - pref_len, | ||
truncated = ''; | ||
@@ -599,3 +601,3 @@ if (desc.length <= desc_len) { | ||
}; | ||
usage = usage || cli.app + ' [OPTIONS]' + (command_list ? ' <command>' : '') + ' [ARGS]'; | ||
@@ -605,3 +607,3 @@ console.log('\x1b[1mUsage\x1b[0m:\n ' + usage); | ||
for (opt in opt_list) { | ||
if (opt.length === 1) { | ||
@@ -614,3 +616,3 @@ long = opt_list[opt][0]; | ||
} | ||
//Parse opt_list | ||
@@ -620,3 +622,3 @@ desc = opt_list[opt][1].trim(); | ||
optional = opt_list[opt].length === 4 ? opt_list[opt][3] : null; | ||
//Build usage line | ||
@@ -633,3 +635,3 @@ if (short === long) { | ||
line += ' '; | ||
if (type) { | ||
@@ -653,3 +655,3 @@ if (type instanceof Array) { | ||
console.log(line); | ||
seen_opts.push(short); | ||
@@ -668,3 +670,3 @@ seen_opts.push(long); | ||
} | ||
} | ||
} | ||
if (enable.catchall && seen_opts.indexOf('c') === -1 && seen_opts.indexOf('catch') === -1) { | ||
@@ -698,3 +700,3 @@ console.log(pad(' -c, --catch', switch_pad) + 'Catch unanticipated errors'); | ||
* Generates an error message when an opt is incorrectly used. | ||
* | ||
* | ||
* @param {String} expects (e.g. 'a value') | ||
@@ -705,3 +707,3 @@ * @param {String} type (e.g. 'VALUE') | ||
cli.getOptError = function (expects, type) { | ||
var err = full_opt + ' expects ' + expects | ||
var err = full_opt + ' expects ' + expects | ||
+ '. Use `' + cli.app + ' ' + full_opt + (is_long ? '=' : ' ') + type + '`'; | ||
@@ -715,3 +717,3 @@ return err; | ||
* will return the default value (if specified) or exit with err_msg. | ||
* | ||
* | ||
* @param {String} default_val | ||
@@ -724,5 +726,5 @@ * @param {Function} validate_func | ||
err_msg = err_msg || cli.getOptError('a value', 'VALUE'); | ||
var value; | ||
try { | ||
@@ -735,3 +737,3 @@ if (curr_val) { | ||
} | ||
//Grouped short opts aren't allowed to have values | ||
@@ -741,4 +743,4 @@ if (short_tags.length) { | ||
} | ||
//If there's no args left or the next arg is an opt, return the | ||
//If there's no args left or the next arg is an opt, return the | ||
//default value (if specified) - otherwise fail | ||
@@ -748,9 +750,9 @@ if (!argv.length || argv[0][0] === '-') { | ||
} | ||
value = argv.shift(); | ||
if (value.match(/^[0-9]+$/)) { | ||
value = parseInt(value, 10); | ||
} | ||
//Run the value through a validation/transformation function if specified | ||
@@ -761,3 +763,3 @@ if (validate_func) { | ||
} catch (e) { | ||
//The value didn't pass the validation/transformation. Unshift the value and | ||
@@ -841,3 +843,3 @@ //return the default value (if specified) | ||
* Gets all data from STDIN (with optional encoding) and sends it to callback. | ||
* | ||
* | ||
* @param {String} encoding (optional - default is 'utf8') | ||
@@ -863,6 +865,6 @@ * @param {Function} callback | ||
/** | ||
* Gets all data from STDIN, splits the data into lines and sends it | ||
* Gets all data from STDIN, splits the data into lines and sends it | ||
* to callback (callback isn't called until all of STDIN is read. To | ||
* process each line as it's received, see the method below | ||
* | ||
* | ||
* @param {Function} callback | ||
@@ -879,6 +881,6 @@ * @api public | ||
/** | ||
* Asynchronously reads a file line by line. When a line is received, | ||
* Asynchronously reads a file line by line. When a line is received, | ||
* callback is called with (line, sep) - when EOF is reached, callback | ||
* receives (null, null, true) | ||
* | ||
* | ||
* @param {String} file (optional - default is 'stdin') | ||
@@ -956,3 +958,3 @@ * @param {String} encoding (optional - default is 'utf8') | ||
} | ||
if (typeof arg === 'function') { | ||
@@ -962,6 +964,6 @@ callback = arg; | ||
} | ||
var lock_file = '/tmp/' + cli.app + '.pid', | ||
log_file = '/tmp/' + cli.app + '.log'; | ||
var start = function () { | ||
@@ -973,3 +975,3 @@ daemon.daemonize(log_file, lock_file, function (err) { | ||
}; | ||
var stop = function () { | ||
@@ -990,3 +992,3 @@ try { | ||
}; | ||
switch(arg) { | ||
@@ -1003,3 +1005,3 @@ case 'stop': | ||
try { | ||
console.log(cli.native.fs.readFileSync(log_file, 'utf8')); | ||
cli.native.fs.createReadStream(log_file, {encoding: 'utf8'}).pipe(process.stdout); | ||
} catch (e) { | ||
@@ -1064,3 +1066,3 @@ return cli.error('No daemon log file'); | ||
layers = Array.prototype.slice.call(arguments); | ||
//Allow createServer(a,b,c) and createServer([a,b,c]) | ||
@@ -1088,5 +1090,5 @@ if (layers.length && layers[0] instanceof Array) { | ||
* A wrapper for child_process.exec(). | ||
* | ||
* If the child_process exits successfully, `callback` receives an array of | ||
* stdout lines. The current process exits if the child process has an error | ||
* | ||
* If the child_process exits successfully, `callback` receives an array of | ||
* stdout lines. The current process exits if the child process has an error | ||
* and `errback` isn't defined. | ||
@@ -1093,0 +1095,0 @@ * |
{ "name" : "cli", | ||
"description" : "A tool for rapidly building command line apps", | ||
"version" : "0.2.8", | ||
"version" : "0.3.0", | ||
"homepage" : "http://github.com/chriso/cli", | ||
@@ -16,3 +16,3 @@ "keywords" : ["cli","command line","opts","parseopt","opt","args","console","argsparse","optparse","daemon","autocomplete","command","autocompletion"], | ||
}, | ||
"contributors": [ | ||
"contributors": [ | ||
{ "name": "Douglas Meyer", "github": "https://github.com/DouglasMeyer" } | ||
@@ -19,0 +19,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
Dynamic require
Supply chain riskDynamic require can indicate the package is performing dangerous or unsafe dynamic code execution.
Found 1 instance in 1 package
Minified code
QualityThis package contains minified code. This may be harmless in some cases where minified code is included in packaged libraries, however packages on npm should not minify code.
Found 1 instance in 1 package
1
1
48837
14
1176