UNPKG

easy-cli

Version:

Easy Command Line Interface. In development.

1 lines 5.48 kB
(function(){function s(e,t){var n={}.hasOwnProperty;for(var r in t)n.call(t,r)&&(e[r]=t[r]);return e}var e,t,n,r=[].join,i="".replace;e=require("wordwrap"),t=require("./text_cli_options"),exports.parser=n=function(){function o(e){function f(){}function l(e,t){var n=this;this.show[e]=function(){return n["show_"+e].apply(n,arguments)}}var t,n,r,i,s,o,u,a;e.aliases||(e.aliases={}),e.booleans||(e.booleans=[]),e.defaults||(e.defaults={}),e.examples||(e.examples={}),e.descriptions||(e.descriptions={}),e.options||(e.options={}),e.display_settings||(e.display_settings={}),e.dependencies||(e.dependencies=[]),e.argv&&this.set_argv(e.argv);if(Object.keys(e.options).length>0)for(t in e.options){n=e.options[t];for(r in n){i=n[r];switch(r){case"boolean":i===!0&&(e.booleans||(e.booleans=[])).push(t);break;case"alias":(s=e.aliases||(e.aliases={}))[t]||(s[t]=[]),i instanceof Array||(i=[i]),(s=e.aliases)[t]=s[t].concat(i);break;case"description":(e.descriptions||(e.descriptions={}))[t]=i;break;case"default":(e.defaults||(e.defaults={}))[t]=i}}}this.set_aliases(e.aliases),this.set_defaults(e.defaults),this.set_descriptions(e.descriptions),this.set_examples(e.examples),this.set_booleans(e.booleans),this.display_settings=e.display_settings,this.dependencies=e.dependencies,this.convert_number=e.convert_number,this.filename=e.filename;for(r in e)r.match(/^show_/)&&(e[r]instanceof Function?this[r]=e[r]:e[r]||(this[r]=f));for(r=0,o=(u=["defaults","usage","examples","options","help","dependencies"]).length-1;r<=o;++r)a=u[r],l.call(this,a,r)}o.displayName="parser";var e=o.prototype,n=o;return e.name="name_undefined",e.booleans={},e.defaults={},e.aliases={},e.descriptions={},e.examples=[],e.raw_argv=null,e.argv={},e.parsed=!1,e.show={},e.set_booleans=function(e){var t,n,r=[];e!=null&&!(e instanceof Array)&&(e=[e]);for(t in e)n=e[t],r.push(this.booleans[n]=!0);return r},e.set_defaults=function(e){return s(this.argv,e),s(this.defaults,e)},e.set_examples=function(e){this.examples=e},e.set_aliases=function(e){var t;for(t in e)e[t]instanceof Array||(e[t]=[e[t]]);return this.aliases=e},e.set_descriptions=function(e){this.descriptions=e},e.string_override=function(e,t){},e.with_dashes=function(e){return e.length>1?"--"+e:"-"+e},e.show_usage=function(){return this.show_help()},e.show_help=function(){return this.show_options(),this.show_defaults(),this.show_examples()},e.show_dependencies=function(){var e,t=[];for(e in this.dependencies)t.push(console.log(e+": "+this.dependencies[e]));return t},e.show_options=function(){function p(e){return h.with_dashes(e)}var e,n,i,o,u,a,f,l,c,h=this;e=Object.keys(this.descriptions),n=Object.keys(this.descriptions),i={};for(o=0,u=n.length-1;o<=u;++o)a=n[o],n[o]=this.with_dashes(a),(f=this.aliases[a])?(f instanceof Array?(f=f.map(p),f=r.call(f,", ")):f=this.with_dashes(f),n[o]=this.with_dashes(a)+(", "+f)):n[o]=this.with_dashes(a),i[n[o]]=this.descriptions[e[o]];return c=s({title:"Options:",options:i,hmargin:2},(l=this.display_settings).options||(l.options={})),t(c,!0)},e.show_defaults=function(){var e,n,r,i;e={};for(n in this.defaults)e[n]=this.defaults[n]+" ["+typeof this.defaults[n]+"]";return i=s({title:"Defaults:",options:e},(r=this.display_settings).defaults||(r.defaults={})),t(i,!0)},e.show_examples=function(){var e,n,r,i,o,u;e=this.filename||module.parent.filename,n=require("path").basename(e),r={};if(this.examples instanceof Array)for(i in this.examples)r[this.examples[i]]="";else for(i in this.examples)r[n+" "+i]=this.examples[i];return u=s({title:"Examples:",options:r,align_right:!1},(o=this.display_settings).examples||(o.examples={})),t(u,!0)},e.show_nice_argv=function(){var e;if(Object.keys(this.argv).length>0){console.log("argv: {");for(e in this.argv)console.log(" "+e+": "+this.argv[e]);return console.log("}")}return console.log("argv: {}")},e.set_argv=function(e){return this.raw_argv=e},e.parse=function(){var e,t,n,r,s,o,u,a,f,l,c,h,p,d,v,m;this.raw_argv===null&&this.set_argv(process.argv.slice(2)),e=[].concat(this.raw_argv),this.argv_no_alias={};for(t in e){n=e[t];if(r=n.match(/^-([A-z]+)/))if((s=r[1].split("")).length>1){e=e.slice(0,t).concat(e.slice(t+1)),this.set_booleans(s);for(t in s)s[t]="-"+s[t];e=e.concat(s)}}for(t in this.aliases)for(o in this.aliases[t])u=this.aliases[t][o],(a=e.indexOf(this.with_dashes(u)))!==-1&&(e[a]=this.with_dashes(t));for(f in this.booleans)l=this.with_dashes(f),(a=e.indexOf(l))!==-1?(e=e.slice(0,a).concat(e.slice(a+1)),this.argv[f]=!0):(a=e.indexOf("--not-"+f))!==-1?(e=e.slice(0,a).concat(e.slice(a+1)),this.argv[f]=!1):this.argv[f]=null,this.argv_no_alias[f]=this.argv[f];for(t=0,c=e.length-1;t<=c;++t)n=e[t],n!=null&&(r=n.match(/^(--|-)/))&&(n=i.call(n,RegExp("^"+r[1]),""),e[t+1]!=null&&!e[t+1].match(/^(--|-)/)?(this.argv[n]=e[t+1],e[t]=null,e[t+1]=null,t++):(this.argv[n]=null,e[t]=null),this.argv_no_alias[n]=this.argv[n]);for(t in this.argv)r=t.match(/^(.*)\[([0-9]+)\]$/),r&&(((h=this.argv)[p=r[1]]||(h[p]=[]))[r[2]]=this.argv[t],((h=this.argv_no_alias)[p=r[1]]||(h[p]=[]))[r[2]]=this.argv[t],delete this.argv[t],delete this.argv_no_alias[t]);for(t in this.aliases)for(o in this.aliases[t])u=this.aliases[t][o],this.argv[u]=this.argv[t];d=[].concat(e),e=[];for(t=0,c=d.length-1;t<=c;++t)typeof d[t]!="undefined"&&d[t]!==null&&e.push(d[t]);this.argv._=e,this.argv_not_null={};for(v=0,m=(h=this.argv).length;v<m;++v)t=h[v],this.argv[t]!==null&&(this.argv_not_null[t]=this.argv[t]);return this.argv_no_alias._=e,this.argv},o}()}).call(this)