@thi.ng/args
Advanced tools
Comparing version 1.0.4 to 1.1.0
@@ -6,2 +6,13 @@ # Change Log | ||
# [1.1.0](https://github.com/thi-ng/umbrella/compare/@thi.ng/args@1.0.4...@thi.ng/args@1.1.0) (2021-08-19) | ||
### Features | ||
* **args:** capitalize usage section headings ([eaa0f23](https://github.com/thi-ng/umbrella/commit/eaa0f23a88cfb98da05b245b720a6fbb260ea7da)) | ||
## [1.0.4](https://github.com/thi-ng/umbrella/compare/@thi.ng/args@1.0.3...@thi.ng/args@1.0.4) (2021-08-17) | ||
@@ -8,0 +19,0 @@ |
@@ -106,3 +106,3 @@ 'use strict'; | ||
...groups.map(([gid, ids]) => [ | ||
...(opts.showGroupNames ? [`${gid}:\n`] : []), | ||
...(opts.showGroupNames ? [`${strings$1.capitalize(gid)}:\n`] : []), | ||
...format(ids), | ||
@@ -109,0 +109,0 @@ "", |
@@ -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,i,s){"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=>i.isNumericFloat(e)?parseFloat(e):s.illegalArgs(`not a numeric value: ${e}`),l=e=>e.map(a),o=e=>i.isHex(e)?parseInt(e,16):s.illegalArgs(`not a hex value: ${e}`),c=e=>e.map(o),u=e=>i.isNumericInt(e)?parseInt(e):s.illegalArgs(`not an integer: ${e}`),g=e=>e.map(u),p=e=>JSON.parse(e),h=e=>t=>e.includes(t)?t:s.illegalArgs(`invalid option: ${t}`);function f(e="=",t=!1,i=!1){return n=>n.reduce(((n,r)=>{const a=r.indexOf(e);if(t&&a<1&&s.illegalArgs(`got '${r}', but expected a 'key${e}value' pair`),a>0){const e=r.substr(0,a),t=r.substr(a+1);i?n[e]?n[e].push(t):n[e]=[t]:n[e]=t}else n[r]=i?["true"]:"true";return n}),{})}const d=(e,t,i=",")=>n=>{const a=n.split(i);return a.length!==t&&s.illegalArgs(`got '${n}', but expected a tuple of ${t} values`),new r(a.map(e))},m=(e,t)=>i=>Object.assign({coerce:e,hint:t,group:"main"},i),b=(e,t)=>i=>Object.assign({hint:$(t,i.delim),multi:!0,coerce:e,group:"main"},i),$=(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"),k=b(l,"NUM"),y=b(c,"HEX"),I=b(g,"INT"),w=(e,t)=>`${t?t+": ":""}${e.map((e=>`"${e}"`)).join(", ")}`,N=(e,i,s,n=",")=>Object.assign({coerce:d(e,i,n),hint:[...t.repeat("N",i)].join(n),group:"main"},s),T=(e,i={})=>{const s=!1!==(i=Object.assign({lineWidth:80,paramWidth:32,showDefaults:!0,prefix:"",suffix:"",groups:["flags","main"]},i)).color?Object.assign(Object.assign({},n),i.color):{},r=t.repeat(" ",i.paramWidth),a=t=>t.map((t=>W(t,e[t],i,s,r))),l=Object.keys(e).sort(),o=i.groups?i.groups.map((t=>[t,l.filter((i=>e[i].group===t))])).filter((e=>!!e[1].length)):[["options",l]];return[...D(i.prefix,i.lineWidth),...o.map((([e,t])=>[...i.showGroupNames?[`${e}:\n`]:[],...a(t),""].join("\n"))),...D(i.suffix,i.lineWidth)].join("\n")},W=(e,i,s,n,r)=>{const a=H(i,n),l=`${S(i,n,a)}${M(`--${t.kebab(e)}`,n.param)}${a}`,o=!1===i.optional&&void 0===i.default,c=[];o&&c.push("required"),i.multi&&c.push("multiple");const u=q(c,n,o)+(i.desc||"")+E(i,s,n);return t.padRight(s.paramWidth)(l,t.lengthAnsi(l))+D(u,s.lineWidth-s.paramWidth).map(((e,t)=>t>0?r+e:e)).join("\n")},H=(e,t)=>e.hint?M(" "+e.hint,t.hint):"",S=(e,t,i)=>e.alias?`${M("-"+e.alias,t.param)}${i}, `:"",q=(e,t,i)=>e.length?M(`[${e.join(", ")}] `,i?t.required:t.multi):"",E=(e,i,s)=>i.showDefaults&&null!=e.default&&!1!==e.default?M(` (default: ${t.stringify(!0)(null!=e.defaultHint?e.defaultHint:e.default)})`,s.default):"",M=(e,t)=>null!=t?`[${t}m${e}[0m`:e,D=(e,i)=>e?t.wordWrapLines(e,{width:i,splitter:t.SPLIT_ANSI,hard:!0}):[],F=(e,t,i)=>{const n=U(e),r={};let a,l,o=i.start;for(;o<t.length;){const s=t[o];if(a){if(_(l,r,a,s))break;a=null,o++}else{if(i.help.includes(s))return void console.log(T(e,i.usageOpts));const t=P(e,n,r,s);if(a=t.id,l=t.spec,o+=~~(t.state<2),t.state)break}}return a&&s.illegalArgs(`missing value for: --${a}`),{result:J(e,r),index:o,rest:t.slice(o),done:o>=t.length}},U=e=>Object.entries(e).reduce(((e,[t,i])=>i.alias?(e[i.alias]=t,e):e),{}),P=(e,i,n,r)=>{if("-"===r[0]){let a;if("-"===r[1]){if("--"===r)return{state:1};a=t.camel(r.substr(2))}else a=i[r.substr(1)],!a&&s.illegalArgs(`unknown option: ${r}`);const l=e[a];return!l&&s.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,t,n,r)=>(/^-[a-z]/i.test(r)&&s.illegalArgs(`missing value for: --${n}`),e.multi?i.isArray(t[n])?t[n].push(r):t[n]=[r]:t[n]=r,e.fn&&!e.fn(r)),J=(e,t)=>{let i;for(let n in e)i=e[n],void 0===t[n]?void 0!==i.default?t[n]=i.default:!1===i.optional&&s.illegalArgs(`missing arg: --${n}`):i.coerce&&L(i,t,n);return t},L=(e,t,i)=>{try{e.multi&&e.delim&&(t[i]=t[i].reduce(((t,i)=>(t.push(...i.split(e.delim)),t)),[])),t[i]=e.coerce(t[i])}catch(e){throw new Error(`arg --${i}: ${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=p,e.coerceKV=f,e.coerceOneOf=h,e.coerceString=e=>e,e.coerceTuple=d,e.flag=e=>Object.assign({flag:!0,default:!1,group:"flags"},e),e.float=v,e.floats=k,e.hex=x,e.hexes=y,e.int=A,e.ints=I,e.json=e=>Object.assign({coerce:p,hint:"JSON",group:"main"},e),e.kvPairs=(e,t="=",i)=>Object.assign({coerce:f(t,i),hint:`key${t}val`,multi:!0,group:"main"},e),e.kvPairsMulti=(e,t="=",i)=>Object.assign({coerce:f(t,i,!0),hint:`key${t}val(s)`,multi:!0,group:"main"},e),e.oneOf=(e,t)=>Object.assign(Object.assign({coerce:h(e),hint:"ID",group:"main"},t),{desc:w(e,t.desc)}),e.oneOfMulti=(e,t)=>Object.assign(Object.assign({coerce:t=>t.map(h(e)),hint:$("ID",t.delim),multi:!0,group:"main"},t),{desc:w(e,t.desc)}),e.parse=(e,t,i)=>{i=Object.assign({start:2,showUsage:!0,help:["--help","-h"]},i);try{return F(e,t,i)}catch(t){throw i.showUsage&&console.log(t.message+"\n\n"+T(e,i.usageOpts)),t}},e.size=(e,t,i="x")=>N(u,e,t,i),e.string=O,e.strings=j,e.tuple=N,e.usage=T,e.vec=(e,t,i=",")=>N(a,e,t,i),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,i,s){"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=>i.isNumericFloat(e)?parseFloat(e):s.illegalArgs(`not a numeric value: ${e}`),l=e=>e.map(a),o=e=>i.isHex(e)?parseInt(e,16):s.illegalArgs(`not a hex value: ${e}`),c=e=>e.map(o),u=e=>i.isNumericInt(e)?parseInt(e):s.illegalArgs(`not an integer: ${e}`),g=e=>e.map(u),p=e=>JSON.parse(e),h=e=>t=>e.includes(t)?t:s.illegalArgs(`invalid option: ${t}`);function f(e="=",t=!1,i=!1){return n=>n.reduce(((n,r)=>{const a=r.indexOf(e);if(t&&a<1&&s.illegalArgs(`got '${r}', but expected a 'key${e}value' pair`),a>0){const e=r.substr(0,a),t=r.substr(a+1);i?n[e]?n[e].push(t):n[e]=[t]:n[e]=t}else n[r]=i?["true"]:"true";return n}),{})}const d=(e,t,i=",")=>n=>{const a=n.split(i);return a.length!==t&&s.illegalArgs(`got '${n}', but expected a tuple of ${t} values`),new r(a.map(e))},m=(e,t)=>i=>Object.assign({coerce:e,hint:t,group:"main"},i),b=(e,t)=>i=>Object.assign({hint:$(t,i.delim),multi:!0,coerce:e,group:"main"},i),$=(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"),k=b(l,"NUM"),y=b(c,"HEX"),I=b(g,"INT"),w=(e,t)=>`${t?t+": ":""}${e.map((e=>`"${e}"`)).join(", ")}`,N=(e,i,s,n=",")=>Object.assign({coerce:d(e,i,n),hint:[...t.repeat("N",i)].join(n),group:"main"},s),T=(e,i={})=>{const s=!1!==(i=Object.assign({lineWidth:80,paramWidth:32,showDefaults:!0,prefix:"",suffix:"",groups:["flags","main"]},i)).color?Object.assign(Object.assign({},n),i.color):{},r=t.repeat(" ",i.paramWidth),a=t=>t.map((t=>W(t,e[t],i,s,r))),l=Object.keys(e).sort(),o=i.groups?i.groups.map((t=>[t,l.filter((i=>e[i].group===t))])).filter((e=>!!e[1].length)):[["options",l]];return[...D(i.prefix,i.lineWidth),...o.map((([e,s])=>[...i.showGroupNames?[`${t.capitalize(e)}:\n`]:[],...a(s),""].join("\n"))),...D(i.suffix,i.lineWidth)].join("\n")},W=(e,i,s,n,r)=>{const a=H(i,n),l=`${S(i,n,a)}${M(`--${t.kebab(e)}`,n.param)}${a}`,o=!1===i.optional&&void 0===i.default,c=[];o&&c.push("required"),i.multi&&c.push("multiple");const u=q(c,n,o)+(i.desc||"")+E(i,s,n);return t.padRight(s.paramWidth)(l,t.lengthAnsi(l))+D(u,s.lineWidth-s.paramWidth).map(((e,t)=>t>0?r+e:e)).join("\n")},H=(e,t)=>e.hint?M(" "+e.hint,t.hint):"",S=(e,t,i)=>e.alias?`${M("-"+e.alias,t.param)}${i}, `:"",q=(e,t,i)=>e.length?M(`[${e.join(", ")}] `,i?t.required:t.multi):"",E=(e,i,s)=>i.showDefaults&&null!=e.default&&!1!==e.default?M(` (default: ${t.stringify(!0)(null!=e.defaultHint?e.defaultHint:e.default)})`,s.default):"",M=(e,t)=>null!=t?`[${t}m${e}[0m`:e,D=(e,i)=>e?t.wordWrapLines(e,{width:i,splitter:t.SPLIT_ANSI,hard:!0}):[],F=(e,t,i)=>{const n=U(e),r={};let a,l,o=i.start;for(;o<t.length;){const s=t[o];if(a){if(_(l,r,a,s))break;a=null,o++}else{if(i.help.includes(s))return void console.log(T(e,i.usageOpts));const t=P(e,n,r,s);if(a=t.id,l=t.spec,o+=~~(t.state<2),t.state)break}}return a&&s.illegalArgs(`missing value for: --${a}`),{result:z(e,r),index:o,rest:t.slice(o),done:o>=t.length}},U=e=>Object.entries(e).reduce(((e,[t,i])=>i.alias?(e[i.alias]=t,e):e),{}),P=(e,i,n,r)=>{if("-"===r[0]){let a;if("-"===r[1]){if("--"===r)return{state:1};a=t.camel(r.substr(2))}else a=i[r.substr(1)],!a&&s.illegalArgs(`unknown option: ${r}`);const l=e[a];return!l&&s.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,t,n,r)=>(/^-[a-z]/i.test(r)&&s.illegalArgs(`missing value for: --${n}`),e.multi?i.isArray(t[n])?t[n].push(r):t[n]=[r]:t[n]=r,e.fn&&!e.fn(r)),z=(e,t)=>{let i;for(let n in e)i=e[n],void 0===t[n]?void 0!==i.default?t[n]=i.default:!1===i.optional&&s.illegalArgs(`missing arg: --${n}`):i.coerce&&J(i,t,n);return t},J=(e,t,i)=>{try{e.multi&&e.delim&&(t[i]=t[i].reduce(((t,i)=>(t.push(...i.split(e.delim)),t)),[])),t[i]=e.coerce(t[i])}catch(e){throw new Error(`arg --${i}: ${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=p,e.coerceKV=f,e.coerceOneOf=h,e.coerceString=e=>e,e.coerceTuple=d,e.flag=e=>Object.assign({flag:!0,default:!1,group:"flags"},e),e.float=v,e.floats=k,e.hex=x,e.hexes=y,e.int=A,e.ints=I,e.json=e=>Object.assign({coerce:p,hint:"JSON",group:"main"},e),e.kvPairs=(e,t="=",i)=>Object.assign({coerce:f(t,i),hint:`key${t}val`,multi:!0,group:"main"},e),e.kvPairsMulti=(e,t="=",i)=>Object.assign({coerce:f(t,i,!0),hint:`key${t}val(s)`,multi:!0,group:"main"},e),e.oneOf=(e,t)=>Object.assign(Object.assign({coerce:h(e),hint:"ID",group:"main"},t),{desc:w(e,t.desc)}),e.oneOfMulti=(e,t)=>Object.assign(Object.assign({coerce:t=>t.map(h(e)),hint:$("ID",t.delim),multi:!0,group:"main"},t),{desc:w(e,t.desc)}),e.parse=(e,t,i)=>{i=Object.assign({start:2,showUsage:!0,help:["--help","-h"]},i);try{return F(e,t,i)}catch(t){throw i.showUsage&&console.log(t.message+"\n\n"+T(e,i.usageOpts)),t}},e.size=(e,t,i="x")=>N(u,e,t,i),e.string=O,e.strings=j,e.tuple=N,e.usage=T,e.vec=(e,t,i=",")=>N(a,e,t,i),Object.defineProperty(e,"__esModule",{value:!0})})); |
{ | ||
"name": "@thi.ng/args", | ||
"version": "1.0.4", | ||
"version": "1.1.0", | ||
"description": "Declarative, functional & typechecked CLI argument/options parser, value coercions etc.", | ||
@@ -74,3 +74,3 @@ "module": "./index.js", | ||
}, | ||
"gitHead": "3365151f112c39f224541f5ac4cedf3370eb9ba4" | ||
"gitHead": "2c2f921759dc30055f86795231fa1289ddb14a6e" | ||
} |
@@ -1,2 +0,2 @@ | ||
import { kebab, lengthAnsi, padRight, repeat, SPLIT_ANSI, stringify, wordWrapLines, } from "@thi.ng/strings"; | ||
import { capitalize, kebab, lengthAnsi, padRight, repeat, SPLIT_ANSI, stringify, wordWrapLines, } from "@thi.ng/strings"; | ||
import { DEFAULT_THEME } from "./api"; | ||
@@ -21,3 +21,3 @@ export const usage = (specs, opts = {}) => { | ||
...groups.map(([gid, ids]) => [ | ||
...(opts.showGroupNames ? [`${gid}:\n`] : []), | ||
...(opts.showGroupNames ? [`${capitalize(gid)}:\n`] : []), | ||
...format(ids), | ||
@@ -24,0 +24,0 @@ "", |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
100017
144
3
5
1
49