@thi.ng/args
Advanced tools
Comparing version 0.3.0 to 0.3.1
@@ -6,2 +6,13 @@ # Change Log | ||
## [0.3.1](https://github.com/thi-ng/umbrella/compare/@thi.ng/args@0.3.0...@thi.ng/args@0.3.1) (2021-03-21) | ||
### Bug Fixes | ||
* **args:** fix usage() show defaults logic ([ae31158](https://github.com/thi-ng/umbrella/commit/ae31158c9496d7c116ee2b4a22ca843888d2bddd)) | ||
# [0.3.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/args@0.2.7...@thi.ng/args@0.3.0) (2021-03-20) | ||
@@ -8,0 +19,0 @@ |
@@ -101,3 +101,3 @@ 'use strict'; | ||
: ""; | ||
const defaults = opts.showDefaults && spec.default | ||
const defaults = opts.showDefaults && spec.default !== undefined | ||
? ansi(` (default: ${strings$1.stringify()(spec.defaultHint != undefined | ||
@@ -104,0 +104,0 @@ ? spec.defaultHint |
@@ -1,1 +0,1 @@ | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/strings"),require("@thi.ng/checks"),require("@thi.ng/errors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/strings","@thi.ng/checks","@thi.ng/errors"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.args={}),e.thi.ng.strings,e.thi.ng.checks,e.thi.ng.errors)}(this,(function(e,t,s,i){"use strict";const n={default:95,hint:90,multi:90,param:96,required:33};class r{constructor(e){this.value=e}deref(){return this.value}}const a=e=>s.isNumericFloat(e)?parseFloat(e):i.illegalArgs(`not a numeric value: ${e}`),l=e=>e.map(a),o=e=>s.isHex(e)?parseInt(e,16):i.illegalArgs(`not a hex value: ${e}`),c=e=>e.map(o),u=e=>s.isNumericInt(e)?parseInt(e):i.illegalArgs(`not an integer: ${e}`),g=e=>e.map(u),h=e=>JSON.parse(e),d=e=>t=>e.includes(t)?t:i.illegalArgs(`invalid option: ${t}`),p=(e="=",t=!1)=>s=>s.reduce(((s,n)=>{const r=n.indexOf(e);return t&&r<1&&i.illegalArgs(`got '${n}', but expected a 'key${e}value' pair`),r>0?s[n.substr(0,r)]=n.substr(r+1):s[n]="true",s}),{}),f=(e,t,s=",")=>n=>{const a=n.split(s);return a.length!==t&&i.illegalArgs(`got '${n}', but expected a tuple of ${t} values`),new r(a.map(e))},m=(e,t)=>s=>Object.assign({coerce:e,hint:t},s),b=(e,t)=>s=>Object.assign({hint:$(t,s.delim),multi:!0,coerce:e},s),$=(e,t)=>e+(t?`[${t}..]`:""),O=m((e=>e),"STR"),j=b((e=>e),"STR"),x=m(a,"NUM"),v=m(o,"HEX"),A=m(u,"INT"),y=b(l,"NUM"),k=b(c,"HEX"),I=b(g,"INT"),w=(e,t)=>`${t?t+": ":""}${e.map((e=>`'${e}'`)).join(", ")}`,T=(e,s,i,n=",")=>Object.assign({coerce:f(e,s,n),hint:[...t.repeat("N",s)].join(n)},i),N=(e,s={})=>{const i=!1!==(s=Object.assign({lineWidth:80,paramWidth:32,showDefaults:!0,prefix:"",suffix:""},s)).color?Object.assign(Object.assign({},n),s.color):{},r=t.repeat(" ",s.paramWidth),a=(e,t)=>null!=t?`[${t}m${e}[0m`:e;return[s.prefix,...Object.keys(e).sort().map((n=>{const l=e[n],o=l.hint?a(" "+l.hint,i.hint):"",c=a(`--${t.kebab(n)}`,i.param),u=`${l.alias?`${a("-"+l.alias,i.param)}${o}, `:""}${c}${o}`,g=!1===l.optional&&void 0===l.default,h=[];g&&h.push("required"),l.multi&&h.push("multiple");const d=h.length?a(`[${h.join(", ")}] `,g?i.required:i.multi):"",p=s.showDefaults&&l.default?a(` (default: ${t.stringify()(null!=l.defaultHint?l.defaultHint:l.default)})`,i.default):"";return t.padRight(s.paramWidth)(u,t.stripAnsi(u).length)+t.wordWrapLines(d+(l.desc||"")+p,s.lineWidth-s.paramWidth).map(((e,t)=>(t>0?r:"")+e)).join("\n")})),s.suffix].join("\n")},H=(e,t,s)=>{const n=W(e),r={};let a,l,o=s.start;for(;o<t.length;){const i=t[o];if(a){if(E(l,r,a,i))break;a=null,o++}else{if(s.help.includes(i))return void console.log(N(e,s.usageOpts));const t=q(e,n,r,i);if(a=t.id,l=t.spec,o+=~~(t.state<2),t.state)break}}return a&&i.illegalArgs(`missing value for: --${a}`),{result:D(e,r),index:o,rest:t.slice(o),done:o>=t.length}},W=e=>Object.entries(e).reduce(((e,[t,s])=>s.alias?(e[s.alias]=t,e):e),{}),q=(e,s,n,r)=>{if("-"===r[0]){let a;if("-"===r[1]){if("--"===r)return{state:1};a=t.camel(r.substr(2))}else a=s[r[1]],!a&&i.illegalArgs(`unknown option: ${r}`);const l=e[a];return!l&&i.illegalArgs(a),l.flag&&(n[a]=!0,a=void 0,l.fn&&!l.fn("true"))?{state:1,spec:l}:{state:0,id:a,spec:l}}return{state:2}},E=(e,t,n,r)=>(/^-[a-z]/i.test(r)&&i.illegalArgs(`missing value for: --${n}`),e.multi?s.isArray(t[n])?t[n].push(r):t[n]=[r]:t[n]=r,e.fn&&!e.fn(r)),D=(e,t)=>{let s;for(let n in e)s=e[n],void 0===t[n]?void 0!==s.default?t[n]=s.default:!1===s.optional&&i.illegalArgs(`missing arg: --${n}`):s.coerce&&F(s,t,n);return t},F=(e,t,s)=>{try{e.multi&&e.delim&&(t[s]=t[s].reduce(((t,s)=>(t.push(...s.split(e.delim)),t)),[])),t[s]=e.coerce(t[s])}catch(e){throw new Error(`arg --${s}: ${e.message}`)}};e.DEFAULT_THEME=n,e.Tuple=r,e.coerceFloat=a,e.coerceFloats=l,e.coerceHexInt=o,e.coerceHexInts=c,e.coerceInt=u,e.coerceInts=g,e.coerceJson=h,e.coerceKV=p,e.coerceOneOf=d,e.coerceString=e=>e,e.coerceTuple=f,e.flag=e=>Object.assign({flag:!0,default:!1},e),e.float=x,e.floats=y,e.hex=v,e.hexes=k,e.int=A,e.ints=I,e.json=e=>Object.assign({coerce:h,hint:"JSON"},e),e.kvPairs=(e,t="=",s)=>Object.assign({coerce:p(t,s),hint:`key${t}val`,multi:!0},e),e.oneOf=(e,t)=>Object.assign(Object.assign({coerce:d(e),hint:"ID"},t),{desc:w(e,t.desc)}),e.oneOfMulti=(e,t)=>Object.assign(Object.assign({coerce:t=>t.map(d(e)),hint:$("ID",t.delim),multi:!0},t),{desc:w(e,t.desc)}),e.parse=(e,t,s)=>{s=Object.assign({start:2,showUsage:!0,help:["--help","-h"]},s);try{return H(e,t,s)}catch(t){throw s.showUsage&&console.log(t.message+"\n\n"+N(e,s.usageOpts)),t}},e.size=(e,t,s="x")=>T(u,e,t,s),e.string=O,e.strings=j,e.tuple=T,e.usage=N,Object.defineProperty(e,"__esModule",{value:!0})})); | ||
!function(e,t){"object"==typeof exports&&"undefined"!=typeof module?t(exports,require("@thi.ng/strings"),require("@thi.ng/checks"),require("@thi.ng/errors")):"function"==typeof define&&define.amd?define(["exports","@thi.ng/strings","@thi.ng/checks","@thi.ng/errors"],t):t(((e="undefined"!=typeof globalThis?globalThis:e||self).thi=e.thi||{},e.thi.ng=e.thi.ng||{},e.thi.ng.args={}),e.thi.ng.strings,e.thi.ng.checks,e.thi.ng.errors)}(this,(function(e,t,s,i){"use strict";const n={default:95,hint:90,multi:90,param:96,required:33};class r{constructor(e){this.value=e}deref(){return this.value}}const a=e=>s.isNumericFloat(e)?parseFloat(e):i.illegalArgs(`not a numeric value: ${e}`),l=e=>e.map(a),o=e=>s.isHex(e)?parseInt(e,16):i.illegalArgs(`not a hex value: ${e}`),c=e=>e.map(o),u=e=>s.isNumericInt(e)?parseInt(e):i.illegalArgs(`not an integer: ${e}`),g=e=>e.map(u),h=e=>JSON.parse(e),d=e=>t=>e.includes(t)?t:i.illegalArgs(`invalid option: ${t}`),p=(e="=",t=!1)=>s=>s.reduce(((s,n)=>{const r=n.indexOf(e);return t&&r<1&&i.illegalArgs(`got '${n}', but expected a 'key${e}value' pair`),r>0?s[n.substr(0,r)]=n.substr(r+1):s[n]="true",s}),{}),f=(e,t,s=",")=>n=>{const a=n.split(s);return a.length!==t&&i.illegalArgs(`got '${n}', but expected a tuple of ${t} values`),new r(a.map(e))},m=(e,t)=>s=>Object.assign({coerce:e,hint:t},s),b=(e,t)=>s=>Object.assign({hint:$(t,s.delim),multi:!0,coerce:e},s),$=(e,t)=>e+(t?`[${t}..]`:""),O=m((e=>e),"STR"),j=b((e=>e),"STR"),v=m(a,"NUM"),x=m(o,"HEX"),A=m(u,"INT"),y=b(l,"NUM"),k=b(c,"HEX"),I=b(g,"INT"),w=(e,t)=>`${t?t+": ":""}${e.map((e=>`'${e}'`)).join(", ")}`,T=(e,s,i,n=",")=>Object.assign({coerce:f(e,s,n),hint:[...t.repeat("N",s)].join(n)},i),N=(e,s={})=>{const i=!1!==(s=Object.assign({lineWidth:80,paramWidth:32,showDefaults:!0,prefix:"",suffix:""},s)).color?Object.assign(Object.assign({},n),s.color):{},r=t.repeat(" ",s.paramWidth),a=(e,t)=>null!=t?`[${t}m${e}[0m`:e;return[s.prefix,...Object.keys(e).sort().map((n=>{const l=e[n],o=l.hint?a(" "+l.hint,i.hint):"",c=a(`--${t.kebab(n)}`,i.param),u=`${l.alias?`${a("-"+l.alias,i.param)}${o}, `:""}${c}${o}`,g=!1===l.optional&&void 0===l.default,h=[];g&&h.push("required"),l.multi&&h.push("multiple");const d=h.length?a(`[${h.join(", ")}] `,g?i.required:i.multi):"",p=s.showDefaults&&void 0!==l.default?a(` (default: ${t.stringify()(null!=l.defaultHint?l.defaultHint:l.default)})`,i.default):"";return t.padRight(s.paramWidth)(u,t.stripAnsi(u).length)+t.wordWrapLines(d+(l.desc||"")+p,s.lineWidth-s.paramWidth).map(((e,t)=>(t>0?r:"")+e)).join("\n")})),s.suffix].join("\n")},H=(e,t,s)=>{const n=W(e),r={};let a,l,o=s.start;for(;o<t.length;){const i=t[o];if(a){if(E(l,r,a,i))break;a=null,o++}else{if(s.help.includes(i))return void console.log(N(e,s.usageOpts));const t=q(e,n,r,i);if(a=t.id,l=t.spec,o+=~~(t.state<2),t.state)break}}return a&&i.illegalArgs(`missing value for: --${a}`),{result:D(e,r),index:o,rest:t.slice(o),done:o>=t.length}},W=e=>Object.entries(e).reduce(((e,[t,s])=>s.alias?(e[s.alias]=t,e):e),{}),q=(e,s,n,r)=>{if("-"===r[0]){let a;if("-"===r[1]){if("--"===r)return{state:1};a=t.camel(r.substr(2))}else a=s[r[1]],!a&&i.illegalArgs(`unknown option: ${r}`);const l=e[a];return!l&&i.illegalArgs(a),l.flag&&(n[a]=!0,a=void 0,l.fn&&!l.fn("true"))?{state:1,spec:l}:{state:0,id:a,spec:l}}return{state:2}},E=(e,t,n,r)=>(/^-[a-z]/i.test(r)&&i.illegalArgs(`missing value for: --${n}`),e.multi?s.isArray(t[n])?t[n].push(r):t[n]=[r]:t[n]=r,e.fn&&!e.fn(r)),D=(e,t)=>{let s;for(let n in e)s=e[n],void 0===t[n]?void 0!==s.default?t[n]=s.default:!1===s.optional&&i.illegalArgs(`missing arg: --${n}`):s.coerce&&F(s,t,n);return t},F=(e,t,s)=>{try{e.multi&&e.delim&&(t[s]=t[s].reduce(((t,s)=>(t.push(...s.split(e.delim)),t)),[])),t[s]=e.coerce(t[s])}catch(e){throw new Error(`arg --${s}: ${e.message}`)}};e.DEFAULT_THEME=n,e.Tuple=r,e.coerceFloat=a,e.coerceFloats=l,e.coerceHexInt=o,e.coerceHexInts=c,e.coerceInt=u,e.coerceInts=g,e.coerceJson=h,e.coerceKV=p,e.coerceOneOf=d,e.coerceString=e=>e,e.coerceTuple=f,e.flag=e=>Object.assign({flag:!0,default:!1},e),e.float=v,e.floats=y,e.hex=x,e.hexes=k,e.int=A,e.ints=I,e.json=e=>Object.assign({coerce:h,hint:"JSON"},e),e.kvPairs=(e,t="=",s)=>Object.assign({coerce:p(t,s),hint:`key${t}val`,multi:!0},e),e.oneOf=(e,t)=>Object.assign(Object.assign({coerce:d(e),hint:"ID"},t),{desc:w(e,t.desc)}),e.oneOfMulti=(e,t)=>Object.assign(Object.assign({coerce:t=>t.map(d(e)),hint:$("ID",t.delim),multi:!0},t),{desc:w(e,t.desc)}),e.parse=(e,t,s)=>{s=Object.assign({start:2,showUsage:!0,help:["--help","-h"]},s);try{return H(e,t,s)}catch(t){throw s.showUsage&&console.log(t.message+"\n\n"+N(e,s.usageOpts)),t}},e.size=(e,t,s="x")=>T(u,e,t,s),e.string=O,e.strings=j,e.tuple=T,e.usage=N,Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/args", | ||
"version": "0.3.0", | ||
"version": "0.3.1", | ||
"description": "Declarative, functional & typechecked CLI argument/options parser, value coercions etc.", | ||
@@ -86,3 +86,3 @@ "module": "./index.js", | ||
}, | ||
"gitHead": "af16a3a01375ed0f84e3ab639b8c686d01212448" | ||
"gitHead": "578bc1ab7886b3ab41a363bce8271ddf01654898" | ||
} |
@@ -30,3 +30,3 @@ import { kebab, padRight, repeat, stringify, stripAnsi, wordWrapLines, } from "@thi.ng/strings"; | ||
: ""; | ||
const defaults = opts.showDefaults && spec.default | ||
const defaults = opts.showDefaults && spec.default !== undefined | ||
? ansi(` (default: ${stringify()(spec.defaultHint != undefined | ||
@@ -33,0 +33,0 @@ ? spec.defaultHint |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
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
85522