Socket
Socket
Sign inDemoInstall

cli

Package Overview
Dependencies
Maintainers
0
Versions
59
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

cli - npm Package Compare versions

Comparing version 0.2.3-2 to 0.2.3-3

2

cli-min.js

@@ -23,2 +23,2 @@ /**

*/
var cli=exports,argv,curr_opt,curr_val,full_opt,is_long,short_tags=[],opt_list,parsed={},usage,argv_parsed,command_list,daemon,daemon_arg,hide_status,show_debug;cli.app=null,cli.version=null,cli.argv=[],cli.argc=0,cli.options={},cli.args=[],cli.command,cli.native={};var define_native=function(a){Object.defineProperty(cli.native,a,{enumerable:true,configurable:true,get:function(){delete cli.native[a];return cli.native[a]=require(a)}})};var natives=process.binding("natives");for(var module in natives)define_native(module);cli.output=cli.native.util.print,cli.exit=process.exit;var enable={help:true,version:false,daemon:false,status:false,timeout:false,catchall:false};cli.enable=function(){Array.prototype.slice.call(arguments).forEach(function(a){switch(a){case"daemon":try{daemon=require("daemon");if(typeof daemon.start!=="function")throw"Invalid module"}catch(b){cli.fatal("daemon.node not installed. Please run `npm install daemon`")}break;case"catchall":process.on("uncaughtException",function(a){cli.error("Uncaught exception: "+(a.msg||a))});break;case"help":case"version":case"status":case"autocomplete":case"timeout":break;default:cli.fatal("Unknown plugin \""+a+"\"")}enable[a]=true});return cli},cli.disable=function(){Array.prototype.slice.call(arguments).forEach(function(a){enable[a]&&(enable[a]=false)});return cli},cli.setArgv=function(a,b){a instanceof Array||(a=a.split(" ")),cli.app=a.shift(),!b&&"node"===cli.app&&(cli.app=a.shift()),cli.app=cli.native.path.basename(cli.app),argv_parsed=false,cli.args=cli.argv=argv=a,cli.argc=argv.length},cli.setArgv(process.argv),cli.next=function(){argv_parsed||(cli.args=[],argv_parsed=true),curr_val=null;if(short_tags.length){curr_opt=short_tags.shift(),full_opt="-"+curr_opt;return curr_opt}if(!argv.length)return false;curr_opt=argv.shift();if(curr_opt==="-"||curr_opt==="--"){while(argv.length)cli.args.push(argv.shift());return false}if(curr_opt[0]!=="-"){cli.args.push(curr_opt);return cli.next()}is_long=curr_opt[1]==="-",curr_opt=curr_opt.substr(is_long?2:1);if(!is_long&&curr_opt.length>1){short_tags=curr_opt.split("");return cli.next()}var a,b;if(is_long&&(a=curr_opt.indexOf("="))>=0){curr_val=curr_opt.substr(a+1),curr_opt=curr_opt.substr(0,a),b=curr_val.length;if(curr_val[0]==="\""&&curr_val[b-1]==="\""||curr_val[0]==="'"&&curr_val[b-1]==="'")curr_val=curr_val.substr(1,b-2);curr_val.match(/^[0-9]+$/)&&(curr_val=parseInt(curr_val,10))}full_opt=(is_long?"--":"-")+curr_opt;return curr_opt},cli.parse=function(a,b){var c,d,e=cli.options,f,g=!a;opt_list=a||{},command_list=b||[];while(o=cli.next()){f=false;for(opt in opt_list){if(!(opt_list[opt]instanceof Array))continue;opt_list[opt][0]===false&&(opt_list[opt][0]=opt);if(o===opt||o===opt_list[opt][0]){f=true;if(opt_list[opt].length===2){e[opt]=true;break}c=null,opt_list[opt].length===4&&(c=opt_list[opt][3]);if(opt_list[opt][2]instanceof Array){for(d=0,l=opt_list[opt][2].length;d<l;d++)typeof opt_list[opt][2][d]==="number"&&(opt_list[opt][2][d]+="");e[opt]=cli.getArrayValue(opt_list[opt][2],is_long?null:c);break}opt_list[opt][2].toLowerCase&&(opt_list[opt][2]=opt_list[opt][2].toLowerCase());switch(opt_list[opt][2]){case"string":case 1:case true:e[opt]=cli.getValue(c);break;case"int":case"number":case"num":case"time":case"seconds":case"secs":case"minutes":case"mins":case"x":case"n":e[opt]=cli.getInt(c);break;case"float":case"decimal":e[opt]=cli.getFloat(c);break;case"path":case"file":case"directory":case"dir":e[opt]=cli.getPath(c,opt_list[opt][2]);break;case"email":e[opt]=cli.getEmail(c);break;case"url":case"uri":case"domain":case"host":e[opt]=cli.getUrl(c,opt_list[opt][2]);break;case"ip":e[opt]=cli.getIp(c);break;case"bool":case"boolean":case"on":e[opt]=true;case"false":case"off":case false:case 0:e[opt]=false;default:cli.fatal("Unknown opt type \""+opt_list[opt][2]+"\"")}break}}if(!f){if(enable.version&&(o==="v"||o==="version"))typeof cli.version==="undefined"&&cli.parsePackageJson(),console.log(cli.app+" v"+cli.version),process.exit();else{if(enable.daemon&&(o==="d"||o==="daemon")){daemon_arg=cli.getArrayValue(["start","stop","restart","pid","log"],is_long?null:"start");continue}if(enable.catchall&&(o==="c"||o==="catch"))continue;if(enable.status&&(o==="s"||o==="silent"||o==="debug")){hide_status=o==="s"||o==="silent",show_debug=o==="debug";continue}if(enable.timeout&&(o==="t"||o==="timeout")){var h=cli.getInt();setTimeout(function(){cli.fatal("Process timed out after "+h+"s")},h*1e3);continue}}enable.help&&(o==="h"||o==="help")&&(cli.getUsage(),console.log("HLELLO"),process.exit());if(g){e[o]=curr_val||true;continue}cli.fatal("Unknown option "+full_opt)}}for(opt in opt_list){c=opt_list[opt].length===4?opt_list[opt][3]:null;if(opt_list[opt]instanceof Array)typeof e[opt]==="undefined"&&(e[opt]=c);else{e[opt]=opt_list[opt];continue}}command_list&&(cli.args.length===0?cli.fatal("A command is required"+(enable.help?". Please see --help for more information":"")):cli.command=cli.autocompleteCommand(cli.args.shift())),cli.argc=cli.args.length;return e},cli.autocompleteCommand=function(a){var b;command_list instanceof Array?b=command_list:b=Object.keys(command_list);var c,d=0,e=a.length,f;if(b.length===0||b.indexOf(a)!==-1)return a;for(c=0;c<e;c++){f=[],l=b.length;if(l<=1)break;for(d=0;d<l;d++)b[d].length>=c&&b[d][c]===a[c]&&f.push(b[d]);b=f}l=b.length;if(l===1)return b[0];l===0?cli.fatal("Unknown command \""+a+"\""+(enable.help?". Please see --help for more information":"")):(b.sort(),cli.fatal("The command \""+a+"\" is ambiguous and could mean \""+b.join("\", \"")+"\""))},["info","error","ok","debug","fatal"].forEach(function(a){cli[a]=function(b){switch(a){case"info":b="\u001b[33mINFO\u001b[0m: "+b;break;case"debug":b="\u001b[36mDEBUG\u001b[0m: "+b;break;case"error":case"fatal":b="\u001b[31mERROR\u001b[0m: "+b;break;case"ok":b="\u001b[32mOK\u001b[0m: "+b}a==="fatal"&&(console.error(b),process.exit(1));enable.status&&(hide_status||!show_debug&&a==="debug")||(a==="error"?console.error(b):console.log(b))}}),cli.setApp=function(a,b){a.indexOf("package.json")!==-1?cli.parsePackageJson(a):(cli.app=a,cli.version=b);return cli},cli.parsePackageJson=function(a){var b=function(a){var b=JSON.parse(cli.native.fs.readFileSync(a,"utf8"));cli.version=b.version,cli.app=b.name};var c=function(a,b,c){for(var d=0,e=a.length;d<e;d++)try{b(a[d]);return}catch(f){d===e-1&&cli.fatal(c)}};try{if(a)return b(a);c([__dirname+"/package.json",__dirname+"/../package.json",__dirname+"/../../package.json"],b)}catch(d){cli.fatal("Could not detect "+cli.app+" version")}},cli.setUsage=function(a){usage=a;return cli};var pad=function(a,b){typeof b==="undefined"&&(b=a,a="");if(a.length<b){b-=a.length;while(b--)a+=" "}return a};cli.getUsage=function(){var a,b,c,d,e=[],f=25;var g=function(a,b,c){var d=a.length,e=65-d,f="";if(b.length<=e)return b;var g=b.split(" "),h=0,i;while(g.length)f+=(i=g.shift())+" ",h+=i.length,g.length&&h+g[0].length>e&&(f+="\n"+pad(d),h=0);return f};usage=usage||cli.app+" [OPTIONS]"+(command_list?" <command>":"")+" [ARGS]",console.log("\u001b[1mUsage\u001b[0m:\n "+usage),console.log("\n\u001b[1mOptions\u001b[0m: ");for(opt in opt_list){opt.length===1?(long=opt_list[opt][0],a=opt):(long=opt,a=opt_list[opt][0]),b=opt_list[opt][1].trim(),type=opt_list[opt].length>=3?opt_list[opt][2]:null,c=opt_list[opt].length===4?opt_list[opt][3]:null,a===long?a.length===1?d=" -"+a:d=" --"+long:d=" -"+a+", --"+long,d+=" ";if(type){type instanceof Array&&(b+=". VALUE must be either ["+type.join("|")+"]",type="VALUE");if(type===true||type===1)type=long.toUpperCase();type=type.toUpperCase();if(type==="FLOAT"||type==="INT")type="NUMBER";d+=c?"["+type+"]":type}d=pad(d,f),d+=g(d,b),d+=c?" (Default is "+c+")":"",console.log(d),e.push(a),e.push(long)}enable.timeout&&e.indexOf("t")===-1&&e.indexOf("timeout")===-1&&console.log(pad(" -t, --timeout N",f)+"Exit if the process takes longer than N seconds"),enable.status&&(e.indexOf("s")===-1&&e.indexOf("silent")===-1&&console.log(pad(" -s, --silent",f)+"Hide all console status messages"),e.indexOf("debug")===-1&&console.log(pad(" --debug",f)+"Show debug information")),enable.catchall&&e.indexOf("c")===-1&&e.indexOf("catch")===-1&&console.log(pad(" -c, --catch",f)+"Catch unanticipated errors"),enable.daemon&&e.indexOf("d")===-1&&e.indexOf("daemon")===-1&&console.log(pad(" -d, --daemon [ARG]",f)+"Daemonize the process. Control the daemon using [start, stop, restart, log, pid]"),enable.version&&e.indexOf("v")===-1&&e.indexOf("version")===-1&&console.log(pad(" -v, --version",f)+"Display the current version"),enable.help&&e.indexOf("h")===-1&&e.indexOf("help")===-1&&console.log(pad(" -h, --help",f)+"Display help and usage details");if(command_list){console.log("\n\u001b[1mCommands\u001b[0m: ");var h;command_list instanceof Array?h=command_list:h=Object.keys(command_list),command_list.sort(),console.log(" "+g(" ",command_list.join(", ")))}process.exit()},cli.getOptError=function(a,b){var c=full_opt+" expects "+a+". Use `"+cli.app+" "+full_opt+(is_long?"=":" ")+b+"`";return c},cli.getValue=function(a,b,c){c=c||cli.getOptError("a value","VALUE");var d;try{if(curr_val){b&&(curr_val=b(curr_val));return curr_val}if(short_tags.length)throw"Short tags";if(!argv.length||argv[0][0]==="-")throw"No value";d=argv.shift(),d.match(/^[0-9]+$/)&&(d=parseInt(d,10)),b&&(d=b(d))}catch(e){d&&argv.unshift(d);return a||cli.fatal(c)}return d},cli.getInt=function(a){return cli.getValue(a,function(a){if(typeof a==="number")return a;if(!a.match(/^(?:-?(?:0|[1-9][0-9]*))$/))throw"Invalid int";return parseInt(a)},cli.getOptError("a number","NUMBER"))},cli.getFloat=function(a){return cli.getValue(a,function(a){if(!a.match(/^(?:-?(?:0|[1-9][0-9]*))?(?:\.[0-9]*)?$/))throw"Invalid float";return parseFloat(a,10)},cli.getOptError("a number","NUMBER"))},cli.getUrl=function(a,b){b=b||"url";return cli.getValue(a,function(a){if(!a.match(/^(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?:\w+:\w+@)?((?:(?:[-\w\d{1-3}]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|edu|co\.uk|ac\.uk|it|fr|tv|museum|asia|local|travel|[a-z]{2})?)|((\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)(\.(\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)){3}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$ |\/.,*:;=]|%[a-f\d]{2})*)?$/))throw"Invalid URL";return a},cli.getOptError("a "+b,b.toUpperCase()))},cli.getEmail=function(a){return cli.getValue(a,function(a){if(!a.match(/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/))throw"Invalid email";return a},cli.getOptError("an email","EMAIL"))},cli.getIp=function(a){return cli.getValue(a,function(a){if(!a.match(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/))throw"Invalid IP";return a},cli.getOptError("an IP","IP"))},cli.getPath=function(a,b){b=b||"path";return cli.getValue(a,function(a){if(a.match(/[?*:;{}]/))throw"Invalid path";return a},cli.getOptError("a "+b,b.toUpperCase()))},cli.getArrayValue=function(a,b){return cli.getValue(b,function(b){if(a.indexOf(b)===-1)throw"Unexpected value";return b},cli.getOptError("either ["+a.join("|")+"]","VALUE"))},cli.withStdin=function(a,b){typeof a==="function"&&(b=a,a="utf8");var c=process.openStdin(),d="";c.setEncoding(a),c.on("data",function(a){d+=a}),c.on("end",function(){b.apply(cli,[d])})},cli.withStdinLines=function(a){cli.withStdin(function(b){var c=b.indexOf("\r\n")!==-1?"\r\n":"\n";a.apply(cli,[b.split(c),c])})},cli.withInput=function(a,b,c){typeof b==="function"?(c=b,b="utf8"):typeof a==="function"&&(c=a,b="utf8",a="stdin");if(a==="stdin")a=process.openStdin();else try{a=cli.native.fs.createReadStream(a),a.on("error",cli.fatal)}catch(d){return cli.fatal(d)}a.setEncoding(b);var e=[],f,g;a.on("data",function(a){if(!f){if(!g)if(a.indexOf("\r\n")!==-1)g="\r\n";else if(a.indexOf("\n")!==-1)g="\n";else{last_line=a;return}e=a.split(g),a=f?null:e.pop();while(e.length)c.apply(cli,[e.shift(),g,false])}}),a.on("end",function(){f=true,data.length&&c.apply(cli,[data,g||"",false]),c.apply(cli,[null,null,true])})},cli.daemon=function(a,b){typeof daemon==="undefined"&&cli.fatal("Daemon is not initialized"),typeof a==="function"&&(b=a,a="start");var c="/tmp/"+cli.app+".pid",d="/tmp/"+cli.app+".log";var e=function(){daemon.run(d,c,function(a){if(a)return cli.error("Error starting daemon: "+a);b()})};var f=function(){try{cli.native.fs.readFileSync(c)}catch(a){return cli.error("Daemon is not running")}daemon.stop(c,function(a,b){if(a&&a.errno===3)return cli.error("Daemon is not running");if(a)return cli.error("Error stopping daemon: "+a.errno);cli.ok("Successfully stopped daemon with pid: "+b)})};switch(a){case"stop":f();break;case"restart":daemon.stop(c,function(){e()});break;case"log":try{console.log(cli.native.fs.readFileSync(d,"utf8"))}catch(g){return cli.error("No daemon log file")}break;case"pid":try{var h=cli.native.fs.readFileSync(c,"utf8");cli.native.fs.statSync("/proc/"+h),cli.info(h)}catch(g){return cli.error("Daemon is not running")}break;default:e()}},cli.main=function(a){var b=function(){a.apply(cli,[cli.args,cli.options])};enable.daemon&&daemon_arg?cli.daemon(daemon_arg,b):b()},cli.createServer=function(){var a=function(a,b,c){if(c){console.error(c.stack),b.writeHead(500,{"Content-Type":"text/plain"});return b.end(c.stack+"\n")}b.writeHead(404,{"Content-Type":"text/plain"}),b.end("Not Found\n")};var b=error=a,c=Array.prototype.slice.call(arguments);c.length&&c[0]instanceof Array&&(c=c[0]),c.reverse().forEach(function(a){var c=b;b=function(b,d){try{a(b,d,function(a){if(a)return error(b,d,a);c(b,d)})}catch(e){error(b,d,e)}}});return cli.native.http.createServer(b)},cli.exec=function(a,b,c){cli.native.child_process.exec(a,function(a,d,e){a=a||e;if(a){if(c)return c(a);return cli.fatal("exec() failed\n"+a)}b&&b(d.split("\n"))})};var last_progress_call,progress_len=74;cli.progress=function(a){if(!(a<0||a>1)){var b=(new Date).getTime();if(last_progress_call&&b-last_progress_call<100){a===1&&setTimeout(function(){cli.progress(1)},150);return}last_progress_call=b;var c=function(){if(a===1)return" 100%\n";var b=Math.floor(a*100)+"%";a<.1&&(b=" "+b);return" "+b};var d=Math.floor(progress_len*a),e="";while(d--)e+="#";cli.native.util.print(pad(e,progress_len)+c()+"\r")}}
var cli=exports,argv,curr_opt,curr_val,full_opt,is_long,short_tags=[],opt_list,parsed={},usage,argv_parsed,command_list,daemon,daemon_arg,no_color,show_debug;cli.app=null,cli.version=null,cli.argv=[],cli.argc=0,cli.options={},cli.args=[],cli.command,cli.native={};var define_native=function(a){Object.defineProperty(cli.native,a,{enumerable:true,configurable:true,get:function(){delete cli.native[a];return cli.native[a]=require(a)}})};var natives=process.binding("natives");for(var module in natives)define_native(module);cli.output=cli.native.util.print,cli.exit=process.exit;var enable={help:true,version:false,daemon:false,status:false,timeout:false,catchall:false};cli.enable=function(){Array.prototype.slice.call(arguments).forEach(function(a){switch(a){case"daemon":try{daemon=require("daemon");if(typeof daemon.start!=="function")throw"Invalid module"}catch(b){cli.fatal("daemon.node not installed. Please run `npm install daemon`")}break;case"catchall":process.on("uncaughtException",function(a){cli.error("Uncaught exception: "+(a.msg||a))});break;case"help":case"version":case"status":case"autocomplete":case"timeout":break;default:cli.fatal("Unknown plugin \""+a+"\"")}enable[a]=true});return cli},cli.disable=function(){Array.prototype.slice.call(arguments).forEach(function(a){enable[a]&&(enable[a]=false)});return cli},cli.setArgv=function(a,b){a instanceof Array||(a=a.split(" ")),cli.app=a.shift(),!b&&"node"===cli.app&&(cli.app=a.shift()),cli.app=cli.native.path.basename(cli.app),argv_parsed=false,cli.args=cli.argv=argv=a,cli.argc=argv.length},cli.setArgv(process.argv),cli.next=function(){argv_parsed||(cli.args=[],argv_parsed=true),curr_val=null;if(short_tags.length){curr_opt=short_tags.shift(),full_opt="-"+curr_opt;return curr_opt}if(!argv.length)return false;curr_opt=argv.shift();if(curr_opt==="-"||curr_opt==="--"){while(argv.length)cli.args.push(argv.shift());return false}if(curr_opt[0]!=="-"){cli.args.push(curr_opt);return cli.next()}is_long=curr_opt[1]==="-",curr_opt=curr_opt.substr(is_long?2:1);if(!is_long&&curr_opt.length>1){short_tags=curr_opt.split("");return cli.next()}var a,b;if(is_long&&(a=curr_opt.indexOf("="))>=0){curr_val=curr_opt.substr(a+1),curr_opt=curr_opt.substr(0,a),b=curr_val.length;if(curr_val[0]==="\""&&curr_val[b-1]==="\""||curr_val[0]==="'"&&curr_val[b-1]==="'")curr_val=curr_val.substr(1,b-2);curr_val.match(/^[0-9]+$/)&&(curr_val=parseInt(curr_val,10))}full_opt=(is_long?"--":"-")+curr_opt;return curr_opt},cli.parse=function(a,b){var c,d,e=cli.options,f,g=!a;opt_list=a||{},command_list=b||[];while(o=cli.next()){f=false;for(opt in opt_list){if(!(opt_list[opt]instanceof Array))continue;opt_list[opt][0]===false&&(opt_list[opt][0]=opt);if(o===opt||o===opt_list[opt][0]){f=true;if(opt_list[opt].length===2){e[opt]=true;break}c=null,opt_list[opt].length===4&&(c=opt_list[opt][3]);if(opt_list[opt][2]instanceof Array){for(d=0,l=opt_list[opt][2].length;d<l;d++)typeof opt_list[opt][2][d]==="number"&&(opt_list[opt][2][d]+="");e[opt]=cli.getArrayValue(opt_list[opt][2],is_long?null:c);break}opt_list[opt][2].toLowerCase&&(opt_list[opt][2]=opt_list[opt][2].toLowerCase());switch(opt_list[opt][2]){case"string":case 1:case true:e[opt]=cli.getValue(c);break;case"int":case"number":case"num":case"time":case"seconds":case"secs":case"minutes":case"mins":case"x":case"n":e[opt]=cli.getInt(c);break;case"float":case"decimal":e[opt]=cli.getFloat(c);break;case"path":case"file":case"directory":case"dir":e[opt]=cli.getPath(c,opt_list[opt][2]);break;case"email":e[opt]=cli.getEmail(c);break;case"url":case"uri":case"domain":case"host":e[opt]=cli.getUrl(c,opt_list[opt][2]);break;case"ip":e[opt]=cli.getIp(c);break;case"bool":case"boolean":case"on":e[opt]=true;case"false":case"off":case false:case 0:e[opt]=false;default:cli.fatal("Unknown opt type \""+opt_list[opt][2]+"\"")}break}}console.log(f+" "+enable.help+" "+o+"\n");if(!f){if(enable.help&&(o==="h"||o==="help"))cli.getUsage(),process.exit();else if(enable.version&&(o==="v"||o==="version"))typeof cli.version==="undefined"&&cli.parsePackageJson(),console.log(cli.app+" v"+cli.version),process.exit();else{if(enable.daemon&&(o==="d"||o==="daemon")){daemon_arg=cli.getArrayValue(["start","stop","restart","pid","log"],is_long?null:"start");continue}if(enable.catchall&&(o==="c"||o==="catch"))continue;if(enable.status&&(o==="k"||o==="no-color"||o==="debug")){no_color=o==="k"||o==="no-color",show_debug=o==="debug";continue}if(enable.timeout&&(o==="t"||o==="timeout")){var h=cli.getInt();setTimeout(function(){cli.fatal("Process timed out after "+h+"s")},h*1e3);continue}if(g){e[o]=curr_val||true;continue}}cli.fatal("Unknown option "+full_opt)}}for(opt in opt_list){c=opt_list[opt].length===4?opt_list[opt][3]:null;if(opt_list[opt]instanceof Array)typeof e[opt]==="undefined"&&(e[opt]=c);else{e[opt]=opt_list[opt];continue}}command_list.length&&(cli.args.length===0?cli.fatal("A command is required"+(enable.help?". Please see --help for more information":"")):cli.command=cli.autocompleteCommand(cli.args.shift())),cli.argc=cli.args.length;return e},cli.autocompleteCommand=function(a){var b;command_list instanceof Array?b=command_list:b=Object.keys(command_list);var c,d=0,e=a.length,f;if(b.length===0||b.indexOf(a)!==-1)return a;for(c=0;c<e;c++){f=[],l=b.length;if(l<=1)break;for(d=0;d<l;d++)b[d].length>=c&&b[d][c]===a[c]&&f.push(b[d]);b=f}l=b.length;if(l===1)return b[0];l===0?cli.fatal("Unknown command \""+a+"\""+(enable.help?". Please see --help for more information":"")):(b.sort(),cli.fatal("The command \""+a+"\" is ambiguous and could mean \""+b.join("\", \"")+"\""))},cli.status=function(a,b){var c;switch(b){case"info":c=no_color?"INFO:":"\u001b[33mINFO\u001b[0m:";break;case"debug":c=no_color?"DEBUG:":"\u001b[36mDEBUG\u001b[0m:";break;case"error":case"fatal":c=no_color?"ERROR:":"\u001b[31mERROR\u001b[0m:";break;case"ok":c=no_color?"OK:":"\u001b[32mOK\u001b[0m:"}a=c+" "+a,b==="fatal"&&(console.error(a),process.exit(1));enable.status&&!show_debug&&b==="debug"||console.error(a)},["info","error","ok","debug","fatal"].forEach(function(a){cli[a]=function(b){cli.status(b,a)}}),cli.setApp=function(a,b){a.indexOf("package.json")!==-1?cli.parsePackageJson(a):(cli.app=a,cli.version=b);return cli},cli.parsePackageJson=function(a){var b=function(a){var b=JSON.parse(cli.native.fs.readFileSync(a,"utf8"));cli.version=b.version,cli.app=b.name};var c=function(a,b,c){for(var d=0,e=a.length;d<e;d++)try{b(a[d]);return}catch(f){d===e-1&&cli.fatal(c)}};try{if(a)return b(a);c([__dirname+"/package.json",__dirname+"/../package.json",__dirname+"/../../package.json"],b)}catch(d){cli.fatal("Could not detect "+cli.app+" version")}},cli.setUsage=function(a){usage=a;return cli};var pad=function(a,b){typeof b==="undefined"&&(b=a,a="");if(a.length<b){b-=a.length;while(b--)a+=" "}return a};cli.getUsage=function(){var a,b,c,d,e=[],f=25;var g=function(a,b,c){var d=a.length,e=65-d,f="";if(b.length<=e)return b;var g=b.split(" "),h=0,i;while(g.length)f+=(i=g.shift())+" ",h+=i.length,g.length&&h+g[0].length>e&&(f+="\n"+pad(d),h=0);return f};usage=usage||cli.app+" [OPTIONS]"+(command_list?" <command>":"")+" [ARGS]",console.log("\u001b[1mUsage\u001b[0m:\n "+usage),console.log("\n\u001b[1mOptions\u001b[0m: ");for(opt in opt_list){opt.length===1?(long=opt_list[opt][0],a=opt):(long=opt,a=opt_list[opt][0]),b=opt_list[opt][1].trim(),type=opt_list[opt].length>=3?opt_list[opt][2]:null,c=opt_list[opt].length===4?opt_list[opt][3]:null,a===long?a.length===1?d=" -"+a:d=" --"+long:d=" -"+a+", --"+long,d+=" ";if(type){type instanceof Array&&(b+=". VALUE must be either ["+type.join("|")+"]",type="VALUE");if(type===true||type===1)type=long.toUpperCase();type=type.toUpperCase();if(type==="FLOAT"||type==="INT")type="NUMBER";d+=c?"["+type+"]":type}d=pad(d,f),d+=g(d,b),d+=c?" (Default is "+c+")":"",console.log(d),e.push(a),e.push(long)}enable.timeout&&e.indexOf("t")===-1&&e.indexOf("timeout")===-1&&console.log(pad(" -t, --timeout N",f)+"Exit if the process takes longer than N seconds"),enable.status&&(e.indexOf("k")===-1&&e.indexOf("no-color")===-1&&console.log(pad(" -k, --no-color",f)+"Omit color from output"),e.indexOf("debug")===-1&&console.log(pad(" --debug",f)+"Show debug information")),enable.catchall&&e.indexOf("c")===-1&&e.indexOf("catch")===-1&&console.log(pad(" -c, --catch",f)+"Catch unanticipated errors"),enable.daemon&&e.indexOf("d")===-1&&e.indexOf("daemon")===-1&&console.log(pad(" -d, --daemon [ARG]",f)+"Daemonize the process. Control the daemon using [start, stop, restart, log, pid]"),enable.version&&e.indexOf("v")===-1&&e.indexOf("version")===-1&&console.log(pad(" -v, --version",f)+"Display the current version"),enable.help&&e.indexOf("h")===-1&&e.indexOf("help")===-1&&console.log(pad(" -h, --help",f)+"Display help and usage details");if(command_list.length){console.log("\n\u001b[1mCommands\u001b[0m: ");var h;command_list instanceof Array?h=command_list:h=Object.keys(command_list),command_list.sort(),console.log(" "+g(" ",command_list.join(", ")))}process.exit()},cli.getOptError=function(a,b){var c=full_opt+" expects "+a+". Use `"+cli.app+" "+full_opt+(is_long?"=":" ")+b+"`";return c},cli.getValue=function(a,b,c){c=c||cli.getOptError("a value","VALUE");var d;try{if(curr_val){b&&(curr_val=b(curr_val));return curr_val}if(short_tags.length)throw"Short tags";if(!argv.length||argv[0][0]==="-")throw"No value";d=argv.shift(),d.match(/^[0-9]+$/)&&(d=parseInt(d,10)),b&&(d=b(d))}catch(e){d&&argv.unshift(d);return a||cli.fatal(c)}return d},cli.getInt=function(a){return cli.getValue(a,function(a){if(typeof a==="number")return a;if(!a.match(/^(?:-?(?:0|[1-9][0-9]*))$/))throw"Invalid int";return parseInt(a)},cli.getOptError("a number","NUMBER"))},cli.getFloat=function(a){return cli.getValue(a,function(a){if(!a.match(/^(?:-?(?:0|[1-9][0-9]*))?(?:\.[0-9]*)?$/))throw"Invalid float";return parseFloat(a,10)},cli.getOptError("a number","NUMBER"))},cli.getUrl=function(a,b){b=b||"url";return cli.getValue(a,function(a){if(!a.match(/^(?:(?:ht|f)tp(?:s?)\:\/\/|~\/|\/)?(?:\w+:\w+@)?((?:(?:[-\w\d{1-3}]+\.)+(?:com|org|net|gov|mil|biz|info|mobi|name|aero|jobs|edu|co\.uk|ac\.uk|it|fr|tv|museum|asia|local|travel|[a-z]{2})?)|((\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)(\.(\b25[0-5]\b|\b[2][0-4][0-9]\b|\b[0-1]?[0-9]?[0-9]\b)){3}))(?::[\d]{1,5})?(?:(?:(?:\/(?:[-\w~!$+|.,=]|%[a-f\d]{2})+)+|\/)+|\?|#)?(?:(?:\?(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)(?:&(?:[-\w~!$+|.,*:]|%[a-f\d{2}])+=?(?:[-\w~!$+|.,*:=]|%[a-f\d]{2})*)*)*(?:#(?:[-\w~!$ |\/.,*:;=]|%[a-f\d]{2})*)?$/))throw"Invalid URL";return a},cli.getOptError("a "+b,b.toUpperCase()))},cli.getEmail=function(a){return cli.getValue(a,function(a){if(!a.match(/^(?:[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+\.)*[\w\!\#\$\%\&\'\*\+\-\/\=\?\^\`\{\|\}\~]+@(?:(?:(?:[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!\.)){0,61}[a-zA-Z0-9]?\.)+[a-zA-Z0-9](?:[a-zA-Z0-9\-](?!$)){0,61}[a-zA-Z0-9]?)|(?:\[(?:(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\.){3}(?:[01]?\d{1,2}|2[0-4]\d|25[0-5])\]))$/))throw"Invalid email";return a},cli.getOptError("an email","EMAIL"))},cli.getIp=function(a){return cli.getValue(a,function(a){if(!a.match(/^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/))throw"Invalid IP";return a},cli.getOptError("an IP","IP"))},cli.getPath=function(a,b){b=b||"path";return cli.getValue(a,function(a){if(a.match(/[?*:;{}]/))throw"Invalid path";return a},cli.getOptError("a "+b,b.toUpperCase()))},cli.getArrayValue=function(a,b){return cli.getValue(b,function(b){if(a.indexOf(b)===-1)throw"Unexpected value";return b},cli.getOptError("either ["+a.join("|")+"]","VALUE"))},cli.withStdin=function(a,b){typeof a==="function"&&(b=a,a="utf8");var c=process.openStdin(),d="";c.setEncoding(a),c.on("data",function(a){d+=a}),c.on("end",function(){b.apply(cli,[d])})},cli.withStdinLines=function(a){cli.withStdin(function(b){var c=b.indexOf("\r\n")!==-1?"\r\n":"\n";a.apply(cli,[b.split(c),c])})},cli.withInput=function(a,b,c){typeof b==="function"?(c=b,b="utf8"):typeof a==="function"&&(c=a,b="utf8",a="stdin");if(a==="stdin")a=process.openStdin();else try{a=cli.native.fs.createReadStream(a),a.on("error",cli.fatal)}catch(d){return cli.fatal(d)}a.setEncoding(b);var e=[],f,g;a.on("data",function(a){if(!f){if(!g)if(a.indexOf("\r\n")!==-1)g="\r\n";else if(a.indexOf("\n")!==-1)g="\n";else{last_line=a;return}e=a.split(g),a=f?null:e.pop();while(e.length)c.apply(cli,[e.shift(),g,false])}}),a.on("end",function(){f=true,data.length&&c.apply(cli,[data,g||"",false]),c.apply(cli,[null,null,true])})},cli.daemon=function(a,b){typeof daemon==="undefined"&&cli.fatal("Daemon is not initialized"),typeof a==="function"&&(b=a,a="start");var c="/tmp/"+cli.app+".pid",d="/tmp/"+cli.app+".log";var e=function(){daemon.run(d,c,function(a){if(a)return cli.error("Error starting daemon: "+a);b()})};var f=function(){try{cli.native.fs.readFileSync(c)}catch(a){return cli.error("Daemon is not running")}daemon.stop(c,function(a,b){if(a&&a.errno===3)return cli.error("Daemon is not running");if(a)return cli.error("Error stopping daemon: "+a.errno);cli.ok("Successfully stopped daemon with pid: "+b)})};switch(a){case"stop":f();break;case"restart":daemon.stop(c,function(){e()});break;case"log":try{console.log(cli.native.fs.readFileSync(d,"utf8"))}catch(g){return cli.error("No daemon log file")}break;case"pid":try{var h=cli.native.fs.readFileSync(c,"utf8");cli.native.fs.statSync("/proc/"+h),cli.info(h)}catch(g){return cli.error("Daemon is not running")}break;default:e()}},cli.main=function(a){var b=function(){a.apply(cli,[cli.args,cli.options])};enable.daemon&&daemon_arg?cli.daemon(daemon_arg,b):b()},cli.createServer=function(){var a=function(a,b,c){if(c){console.error(c.stack),b.writeHead(500,{"Content-Type":"text/plain"});return b.end(c.stack+"\n")}b.writeHead(404,{"Content-Type":"text/plain"}),b.end("Not Found\n")};var b=error=a,c=Array.prototype.slice.call(arguments);c.length&&c[0]instanceof Array&&(c=c[0]),c.reverse().forEach(function(a){var c=b;b=function(b,d){try{a(b,d,function(a){if(a)return error(b,d,a);c(b,d)})}catch(e){error(b,d,e)}}});return cli.native.http.createServer(b)},cli.exec=function(a,b,c){cli.native.child_process.exec(a,function(a,d,e){a=a||e;if(a){if(c)return c(a);return cli.fatal("exec() failed\n"+a)}b&&b(d.split("\n"))})};var last_progress_call,progress_len=74;cli.progress=function(a){if(!(a<0||a>1)){var b=(new Date).getTime();if(last_progress_call&&b-last_progress_call<100){a===1&&setTimeout(function(){cli.progress(1)},50);return}last_progress_call=b;var c=function(){if(a===1)return" 100%\n";var b=Math.floor(a*100)+"%";a<.1&&(b=" "+b);return" "+b};var d=Math.floor(progress_len*a),e="";while(d--)e+="#";cli.native.util.print(pad(e,progress_len)+c()+"\r")}}

@@ -31,3 +31,3 @@ /**

usage, argv_parsed, command_list,
daemon, daemon_arg, hide_status, show_debug;
daemon, daemon_arg, no_color, show_debug;

@@ -82,3 +82,3 @@ cli.app = null;

daemon: false, //Adds -d,--daemon [ARG] => (see cli.daemon() below)
status: false, //Adds -s,--silent & --debug => hide console messages or display debug messages
status: false, //Adds -k,--no-color & --debug => display plain status messages /display debug messages
timeout: false, //Adds -t,--timeout N => timeout the process after N seconds

@@ -279,27 +279,14 @@ catchall: false, //Adds -c,--catch => catch and output uncaughtExceptions

switch (opt_list[opt][2]) {
case 'string':
case 1:
case true:
case 'string': case 1: case true:
parsed[opt] = cli.getValue(default_val);
break;
case 'int':
case 'number':
case 'num':
case 'time':
case 'seconds':
case 'secs':
case 'minutes':
case 'mins':
case 'x':
case 'n':
case 'int': case 'number': case 'num':
case 'time': case 'seconds': case 'secs': case 'minutes': case 'mins':
case 'x': case 'n':
parsed[opt] = cli.getInt(default_val);
break;
case 'float':
case 'decimal':
case 'float': case 'decimal':
parsed[opt] = cli.getFloat(default_val);
break;
case 'path':
case 'file':
case 'directory':
case 'dir':
case 'path': case 'file': case 'directory': case 'dir':
parsed[opt] = cli.getPath(default_val, opt_list[opt][2]);

@@ -310,6 +297,3 @@ break;

break;
case 'url':
case 'uri':
case 'domain':
case 'host':
case 'url': case 'uri': case 'domain': case 'host':
parsed[opt] = cli.getUrl(default_val, opt_list[opt][2]);

@@ -320,10 +304,5 @@ break;

break;
case 'bool':
case 'boolean':
case 'on':
case 'bool': case 'boolean': case 'on':
parsed[opt] = true;
case 'false':
case 'off':
case false:
case 0:
case 'false': case 'off': case false: case 0:
parsed[opt] = false;

@@ -336,4 +315,8 @@ default:

}
console.log(seen + ' ' + enable.help + ' ' + o + '\n');
if (!seen) {
if (enable.version && (o === 'v' || o === 'version')) {
if (enable.help && (o === 'h' || o === 'help')) {
cli.getUsage();
process.exit();
} else if (enable.version && (o === 'v' || o === 'version')) {
if (typeof cli.version === 'undefined') {

@@ -344,3 +327,3 @@ cli.parsePackageJson();

process.exit();
} else if (enable.daemon && (o === 'd' || o === 'daemon')) {
} else if (enable.daemon && (o === 'd' || o === 'daemon')) {
daemon_arg = cli.getArrayValue(['start','stop','restart','pid','log'], is_long ? null : 'start');

@@ -350,4 +333,4 @@ continue;

continue;
} else if (enable.status && (o === 's' || o === 'silent' || o === 'debug')) {
hide_status = (o === 's' || o === 'silent');
} else if (enable.status && (o === 'k' || o === 'no-color' || o === 'debug')) {
no_color = (o === 'k' || o === 'no-color');
show_debug = o === 'debug';

@@ -361,9 +344,3 @@ continue;

continue;
}
if (enable.help && (o === 'h' || o === 'help')) {
cli.getUsage();
console.log('HLELLO');
process.exit();
}
if (catch_all) {
} else if (catch_all) {
parsed[o] = curr_val || true;

@@ -385,3 +362,3 @@ continue;

}
if (command_list) {
if (command_list.length) {
if (cli.args.length === 0) {

@@ -436,3 +413,3 @@ cli.fatal('A command is required' + (enable.help ? '. Please see --help for more information' : ''));

/**
* Adds methods to output styled status messages to the console.
* Adds methods to output styled status messages to stderr.
*

@@ -449,34 +426,32 @@ * Added methods are cli.info(msg), cli.error(msg), cli.ok(msg), and

*/
cli.status = function (msg, type) {
var pre;
switch (type) {
case 'info':
pre = no_color ? 'INFO:' : '\x1B[33mINFO\x1B[0m:';
break;
case 'debug':
pre = no_color ? 'DEBUG:' : '\x1B[36mDEBUG\x1B[0m:';
break;
case 'error':
case 'fatal':
pre = no_color ? 'ERROR:' : '\x1B[31mERROR\x1B[0m:';
break;
case 'ok':
pre = no_color ? 'OK:' : '\x1B[32mOK\x1B[0m:';
break;
}
msg = pre + ' ' + msg;
if (type === 'fatal') {
console.error(msg);
process.exit(1);
}
if (enable.status && !show_debug && type === 'debug') {
return;
}
console.error(msg);
};
['info','error','ok','debug','fatal'].forEach(function (type) {
cli[type] = function (msg) {
switch (type) {
case 'info':
msg = '\x1B[33mINFO\x1B[0m: ' + msg;
break;
case 'debug':
msg = '\x1B[36mDEBUG\x1B[0m: ' + msg;
break;
case 'error':
case 'fatal':
msg = '\x1B[31mERROR\x1B[0m: ' + msg;
break;
case 'ok':
msg = '\x1B[32mOK\x1B[0m: ' + msg;
break;
}
if (type === 'fatal') {
console.error(msg);
process.exit(1);
}
if (enable.status && (hide_status || (!show_debug && type === 'debug'))) {
return;
}
if (type === 'error') {
console.error(msg);
} else {
console.log(msg);
}
cli.status(msg, type);
};

@@ -654,4 +629,4 @@ });

if (enable.status) {
if (seen_opts.indexOf('s') === -1 && seen_opts.indexOf('silent') === -1) {
console.log(pad(' -s, --silent', switch_pad) + 'Hide all console status messages');
if (seen_opts.indexOf('k') === -1 && seen_opts.indexOf('no-color') === -1) {
console.log(pad(' -k, --no-color', switch_pad) + 'Omit color from output');
}

@@ -674,3 +649,3 @@ if (seen_opts.indexOf('debug') === -1) {

}
if (command_list) {
if (command_list.length) {
console.log('\n\x1b[1mCommands\x1b[0m: ');

@@ -1099,3 +1074,3 @@ var list;

if (last_progress_call && (now - last_progress_call) < 100) {
if (progress === 1) setTimeout(function () { cli.progress(1); }, 150);
if (progress === 1) setTimeout(function () { cli.progress(1); }, 50);
return; //Throttle progress calls

@@ -1102,0 +1077,0 @@ }

#!/usr/bin/env node
var cli = require('../cli');
var cli = require('cli');

@@ -5,0 +5,0 @@ //The second (optional) argument of cli.parse() is a command list

@@ -7,3 +7,6 @@ #!/usr/bin/env node

cli.progress(++i / 100);
if (i >= 100) clearInterval(interval);
if (i === 100) {
clearInterval(interval);
cli.ok('Finished!');
}
}, 50);
{ "name" : "cli",
"description" : "A tool for rapidly building command line apps",
"version" : "0.2.3-2",
"version" : "0.2.3-3",
"homepage" : "http://github.com/chriso/cli",

@@ -5,0 +5,0 @@ "keywords" : ["cli","command line","opts","parseopt","opt","args","console","argsparse","optparse","daemon","autocomplete","command","autocompletion"],

@@ -7,3 +7,3 @@ **cli is a toolkit for rapidly building NodeJS command line apps - it includes:**

- Output colored/styled messages or [progress bars](https://github.com/chriso/cli/blob/master/examples/progress.js)
- Create apps that accept a list of commands - cli includes [auto-completion](auto-completion)[https://github.com/chriso/cli/blob/master/examples/command.js] support
- Create apps that accept a list of commands - cli includes support for command [auto-completion](https://github.com/chriso/cli/blob/master/examples/command.js)

@@ -78,4 +78,9 @@ Install using `npm install cli` or just bundle [cli.js](https://github.com/chriso/cli/raw/master/cli.js) with your app.

cli has a helper method for working with input (see [./examples/cat.js](https://github.com/chriso/cli/blob/master/examples/cat.js) for an example). `newline` is autodetected as \n or \r\n
cli has methods that collect stdin (newline is autodetected as \n or \r\n)
cli.withStdin(callback); //callback receives stdin as a string
cli.withStdinLines(callback); //callback receives stdin split into an array of lines (lines, newline)
cli also has a lower level method for working with input line by line (see [./examples/cat.js](https://github.com/chriso/cli/blob/master/examples/cat.js) for an example).
cli.withInput(file, function (line, newline, eof) {

@@ -89,7 +94,2 @@ if (!eof) {

cli also has methods that collect all stdin before calling callback
cli.withStdin(callback); //callback receives stdin as a string
cli.withStdinLines(callback); //callback receives (lines, newline)
To output a progress bar, call

@@ -131,3 +131,3 @@

`-s,--silent` will omit all status messages (except for fatal)
`-k,--no-color` will omit ANSI color escapes from the output

@@ -134,0 +134,0 @@ **daemon** - *requires* `npm install daemon`

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc