cleye 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -539,6 +539,18 @@ Type: `Command[]`
539
539
 
540
540
  Array of [commands](#commandoptions-callback) to register.
541
541
 
542
+ ##### ignoreArgv
543
+
544
+ Type:
545
+ ```ts
546
+ type IgnoreArgvCallback = (
547
+ type: 'known-flag' | 'unknown-flag' | 'argument',
548
+ flagOrArgv: string,
549
+ value: string | undefined,
550
+ ) => boolean | void
551
+ ```
552
+
553
+ A callback to ignore argv tokens from being parsed.
542
554
 
543
555
  #### callback(parsed)
544
556
 
package/dist/index.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Flags as Flags$1, TypeFlag } from 'type-flag';
1
+ import { Flags as Flags$1, TypeFlagOptions, TypeFlag } from 'type-flag';
2
2
  import { Options } from 'terminal-columns';
3
3
 
4
4
  declare type CommandOptions<Parameters = string[]> = {
@@ -109,7 +109,9 @@ declare type HasHelp<Options extends {
109
109
  } ? Options['flags'] : Options['flags'] & {
110
110
  help: BooleanConstructor;
111
111
  });
112
- declare type HasHelpOrVersion<Options> = HasVersion<Options> & HasHelp<Options>;
112
+ declare type HasHelpOrVersion<Options extends {
113
+ flags?: Flags;
114
+ }> = (HasVersion<Options> & HasHelp<Options>);
113
115
  declare type HelpDocumentNode<Types extends PropertyKey = keyof Renderers> = {
114
116
  id?: string;
115
117
  type: Types;
@@ -167,6 +169,10 @@ declare type CliOptions<Commands = Command[], Parameters extends string[] = stri
167
169
  Options to configure the help documentation. Pass in `false` to disable handling `--help, -h`.
168
170
  */
169
171
  help?: false | HelpOptions;
172
+ /**
173
+ * Which argv elements to ignore from parsing
174
+ */
175
+ ignoreArgv?: TypeFlagOptions['ignore'];
170
176
  };
171
177
  declare type kebabToCamel<Word extends string> = (Word extends `${infer Prefix}-${infer Suffix}` | `${infer Prefix} ${infer Suffix}` ? `${Prefix}${Capitalize<kebabToCamel<Suffix>>}` : Word);
172
178
  declare type StripBrackets<Parameter extends string> = (Parameter extends `<${infer ParameterName}>` | `[${infer ParameterName}]` ? (ParameterName extends `${infer SpreadName}...` ? SpreadName : ParameterName) : never);
@@ -200,4 +206,4 @@ declare function cli<Options extends CliOptions<[...Commands], [...Parameters]>,
200
206
  }) : never);
201
207
  }[number]);
202
208
 
203
- export { cli, command };
209
+ export { Command, Renderers, cli, command };
package/dist/index.js CHANGED
@@ -1,10 +1,10 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var B=require("type-flag"),I=require("tty"),C=require("terminal-columns");function v(r){return r&&typeof r=="object"&&"default"in r?r:{default:r}}var D=v(B),L=v(I),R=v(C);const T=r=>r.replace(/[-_ ](\w)/g,(e,t)=>t.toUpperCase()),F=r=>r.replace(/\B([A-Z])/g,"-$1").toLowerCase(),H={"> 80":[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"auto"}],"> 40":[{width:"auto",paddingLeft:2,paddingRight:8,preprocess:r=>r.trim()},{width:"100%",paddingLeft:2,paddingBottom:1}],"> 0":{stdoutColumns:1e3,columns:[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"content-width"}]}};function M(r){let e=!1;const n=Object.keys(r).sort((a,i)=>a.localeCompare(i)).map(a=>{const i=r[a],s="alias"in i;return s&&(e=!0),{name:a,flag:i,flagFormatted:`--${F(a)}`,aliasesEnabled:e,aliasFormatted:s?`-${i.alias}`:void 0}}).map(a=>(a.aliasesEnabled=e,[{type:"flagName",data:a},{type:"flagDescription",data:a}]));return{type:"table",data:{tableData:n,tableBreakpoints:H}}}const A=r=>{var e;return!r||((e=r.version)!=null?e:r.help?r.help.version:void 0)},x=r=>{var e;const t="parent"in r&&((e=r.parent)==null?void 0:e.name);return(t?`${t} `:"")+r.name};function U(r){var e;const t=[];r.name&&t.push(x(r));const n=(e=A(r))!=null?e:"parent"in r&&A(r.parent);if(n&&t.push(`v${n}`),t.length!==0)return{id:"name",type:"text",data:`${t.join(" ")}
2
- `}}function V(r){const{help:e}=r;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
3
- `}}function J(r){var e;const t=r.help||{};if("usage"in t)return t.usage?{id:"usage",type:"section",data:{title:"Usage:",body:Array.isArray(t.usage)?t.usage.join(`
4
- `):t.usage}}:void 0;if(r.name){const n=[],a=[x(r)];if(r.flags&&Object.keys(r.flags).length>0&&a.push("[flags...]"),r.parameters&&r.parameters.length>0){const{parameters:i}=r,s=i.indexOf("--"),l=s>-1&&i.slice(s+1).some(o=>o.startsWith("<"));a.push(i.map(o=>o!=="--"?o:l?"--":"[--]").join(" "))}if(a.length>1&&n.push(a.join(" ")),"commands"in r&&((e=r.commands)==null?void 0:e.length)&&n.push(`${r.name} <command>`),n.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:n.join(`
5
- `)}}}}function k(r){var e;if(!("commands"in r)||!((e=r.commands)!=null&&e.length))return;const t=r.commands.map(a=>[a.options.name,a.options.help?a.options.help.description:""]);return{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:t,tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function W(r){if(!(!r.flags||Object.keys(r.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:M(r.flags),indentBody:0}}}function Z(r){const{help:e}=r;if(!e||!e.examples||e.examples.length===0)return;let{examples:t}=e;if(Array.isArray(t)&&(t=t.join(`
6
- `)),t)return{id:"examples",type:"section",data:{title:"Examples:",body:t}}}function z(r){if(!("alias"in r)||!r.alias)return;const{alias:e}=r,t=Array.isArray(e)?e.join(", "):e;return{id:"aliases",type:"section",data:{title:"Aliases:",body:t}}}const G=r=>[U,V,J,k,W,Z,z].map(e=>e(r)).filter(e=>Boolean(e)),K=L.default.WriteStream.prototype.hasColors();class Q{text(e){return e}bold(e){return K?`\x1B[1m${e}\x1B[22m`:e.toLocaleUpperCase()}indentText({text:e,spaces:t}){return e.replace(/^/gm," ".repeat(t))}heading(e){return this.bold(e)}section({title:e,body:t,indentBody:n=2}){return`${(e?`${this.heading(e)}
7
- `:"")+(t?this.indentText({text:this.render(t),spaces:n}):"")}
8
- `}table({tableData:e,tableOptions:t,tableBreakpoints:n}){return R.default(e.map(a=>a.map(i=>this.render(i))),n?C.breakpoints(n):t)}flagParameter(e){return e===Boolean?"":e===String?"<string>":e===Number?"<number>":Array.isArray(e)?this.flagParameter(e[0]):"<value>"}flagOperator(e){return" "}flagName(e){const{flag:t,flagFormatted:n,aliasesEnabled:a,aliasFormatted:i}=e;let s="";if(i?s+=`${i}, `:a&&(s+=" "),s+=n,"placeholder"in t&&typeof t.placeholder=="string")s+=`${this.flagOperator(e)}${t.placeholder}`;else{const l=this.flagParameter("type"in t?t.type:t);l&&(s+=`${this.flagOperator(e)}${l}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){var t;let n="description"in e&&(t=e.description)!=null?t:"";if("default"in e){let{default:a}=e;typeof a=="function"&&(a=a()),a&&(n+=` (default: ${this.flagDefault(a)})`)}return n}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(t=>this.render(t)).join(`
9
- `);if("type"in e&&this[e.type]){const t=this[e.type];if(typeof t=="function")return t.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const w=/^[\w.-]+$/;var X=Object.defineProperty,Y=Object.defineProperties,ee=Object.getOwnPropertyDescriptors,j=Object.getOwnPropertySymbols,re=Object.prototype.hasOwnProperty,te=Object.prototype.propertyIsEnumerable,N=(r,e,t)=>e in r?X(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,m=(r,e)=>{for(var t in e||(e={}))re.call(e,t)&&N(r,t,e[t]);if(j)for(var t of j(e))te.call(e,t)&&N(r,t,e[t]);return r},b=(r,e)=>Y(r,ee(e));const{stringify:d}=JSON,ae=/[|\\{}()[\]^$+*?.]/;function _(r){const e=[];let t,n;for(const a of r){if(n)throw new Error(`Invalid parameter: Spread parameter ${d(n)} must be last`);const i=a[0],s=a[a.length-1];let l;if(i==="<"&&s===">"&&(l=!0,t))throw new Error(`Invalid parameter: Required parameter ${d(a)} cannot come after optional parameter ${d(t)}`);if(i==="["&&s==="]"&&(l=!1,t=a),l===void 0)throw new Error(`Invalid parameter: ${d(a)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let o=a.slice(1,-1);const f=o.slice(-3)==="...";f&&(n=a,o=o.slice(0,-3));const p=o.match(ae);if(p)throw new Error(`Invalid parameter: ${d(a)}. Invalid character found ${d(p[0])}`);e.push({name:o,required:l,spread:f})}return e}function $(r,e,t,n){for(let a=0;a<e.length;a+=1){const{name:i,required:s,spread:l}=e[a],o=T(i);if(o in r)throw new Error(`Invalid parameter: ${d(i)} is used more than once.`);const f=l?t.slice(a):t[a];if(l&&(a=e.length),s&&(!f||l&&f.length===0))return console.error(`Error: Missing required parameter ${d(i)}
10
- `),n(),process.exit(1);r[o]=f}}function ne(r){return r===void 0||r!==!1}function S(r,e,t,n){const a=m({},e.flags),i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,l=ne(s);l&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const o=D.default(a,n),f=()=>{console.log(e.version)};if(i&&o.flags.version===!0)return f(),process.exit(0);const p=new Q,q=l&&(s==null?void 0:s.render)?s.render:c=>p.render(c),h=c=>{const u=G(b(m(m({},e),c?{help:c}:{}),{flags:a}));console.log(q(u,p))};if(l&&o.flags.help===!0)return h(),process.exit(0);if(e.parameters){let{parameters:c}=e,u=o._;const y=c.indexOf("--"),E=c.slice(y+1),g=Object.create(null);if(y>-1&&E.length>0){c=c.slice(0,y);const P=o._["--"];u=u.slice(0,-P.length||void 0),$(g,_(c),u,h),$(g,_(E),P,h)}else $(g,_(c),u,h);Object.assign(o._,g)}const O=b(m({},o),{showVersion:f,showHelp:h});return typeof t=="function"&&t(O),m({command:r},O)}function ie(r,e){const t=new Map;for(const n of e){const a=[n.options.name],{alias:i}=n.options;i&&(Array.isArray(i)?a.push(...i):a.push(i));for(const s of a){if(t.has(s))throw new Error(`Duplicate command name found: ${d(s)}`);t.set(s,n)}}return t.get(r)}function se(r,e,t=process.argv.slice(2)){if(!r)throw new Error("Options is required");if("name"in r&&(!r.name||!w.test(r.name)))throw new Error(`Invalid script name: ${d(r.name)}`);const n=t[0];if(r.commands&&w.test(n)){const a=ie(n,r.commands);if(a)return S(a.options.name,b(m({},a.options),{parent:r}),a.callback,t.slice(1))}return S(void 0,r,e,t)}function oe(r,e){if(!r)throw new Error("Command options are required");const{name:t}=r;if(r.name===void 0)throw new Error("Command name is required");if(!w.test(t))throw new Error(`Invalid command name ${JSON.stringify(t)}. Command names must be one word.`);return{options:r,callback:e}}exports.cli=se,exports.command=oe;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:!0});var q=require("type-flag"),N=require("tty"),C=require("terminal-columns");function A(t){return t&&typeof t=="object"&&"default"in t?t:{default:t}}var P=A(N),B=A(C);const S=t=>t.replace(/[-_ ](\w)/g,(e,r)=>r.toUpperCase()),I=t=>t.replace(/\B([A-Z])/g,"-$1").toLowerCase(),D={"> 80":[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"auto"}],"> 40":[{width:"auto",paddingLeft:2,paddingRight:8,preprocess:t=>t.trim()},{width:"100%",paddingLeft:2,paddingBottom:1}],"> 0":{stdoutColumns:1e3,columns:[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"content-width"}]}};function L(t){let e=!1;const n=Object.keys(t).sort((a,i)=>a.localeCompare(i)).map(a=>{const i=t[a],s="alias"in i;return s&&(e=!0),{name:a,flag:i,flagFormatted:`--${I(a)}`,aliasesEnabled:e,aliasFormatted:s?`-${i.alias}`:void 0}}).map(a=>(a.aliasesEnabled=e,[{type:"flagName",data:a},{type:"flagDescription",data:a}]));return{type:"table",data:{tableData:n,tableBreakpoints:D}}}const x=t=>!t||(t.version??(t.help?t.help.version:void 0)),O=t=>{const e="parent"in t&&t.parent?.name;return(e?`${e} `:"")+t.name};function R(t){const e=[];t.name&&e.push(O(t));const r=x(t)??("parent"in t&&x(t.parent));if(r&&e.push(`v${r}`),e.length!==0)return{id:"name",type:"text",data:`${e.join(" ")}
2
+ `}}function T(t){const{help:e}=t;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
3
+ `}}function F(t){const e=t.help||{};if("usage"in e)return e.usage?{id:"usage",type:"section",data:{title:"Usage:",body:Array.isArray(e.usage)?e.usage.join(`
4
+ `):e.usage}}:void 0;if(t.name){const r=[],n=[O(t)];if(t.flags&&Object.keys(t.flags).length>0&&n.push("[flags...]"),t.parameters&&t.parameters.length>0){const{parameters:a}=t,i=a.indexOf("--"),s=i>-1&&a.slice(i+1).some(o=>o.startsWith("<"));n.push(a.map(o=>o!=="--"?o:s?"--":"[--]").join(" "))}if(n.length>1&&r.push(n.join(" ")),"commands"in t&&t.commands?.length&&r.push(`${t.name} <command>`),r.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:r.join(`
5
+ `)}}}}function k(t){if(!("commands"in t)||!t.commands?.length)return;const e=t.commands.map(n=>[n.options.name,n.options.help?n.options.help.description:""]);return{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:e,tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function H(t){if(!(!t.flags||Object.keys(t.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:L(t.flags),indentBody:0}}}function M(t){const{help:e}=t;if(!e||!e.examples||e.examples.length===0)return;let{examples:r}=e;if(Array.isArray(r)&&(r=r.join(`
6
+ `)),r)return{id:"examples",type:"section",data:{title:"Examples:",body:r}}}function U(t){if(!("alias"in t)||!t.alias)return;const{alias:e}=t,r=Array.isArray(e)?e.join(", "):e;return{id:"aliases",type:"section",data:{title:"Aliases:",body:r}}}const J=t=>[R,T,F,k,H,M,U].map(e=>e(t)).filter(Boolean),V=P.default.WriteStream.prototype.hasColors();class W{text(e){return e}bold(e){return V?`\x1B[1m${e}\x1B[22m`:e.toLocaleUpperCase()}indentText({text:e,spaces:r}){return e.replace(/^/gm," ".repeat(r))}heading(e){return this.bold(e)}section({title:e,body:r,indentBody:n=2}){return`${(e?`${this.heading(e)}
7
+ `:"")+(r?this.indentText({text:this.render(r),spaces:n}):"")}
8
+ `}table({tableData:e,tableOptions:r,tableBreakpoints:n}){return B.default(e.map(a=>a.map(i=>this.render(i))),n?C.breakpoints(n):r)}flagParameter(e){return e===Boolean?"":e===String?"<string>":e===Number?"<number>":Array.isArray(e)?this.flagParameter(e[0]):"<value>"}flagOperator(e){return" "}flagName(e){const{flag:r,flagFormatted:n,aliasesEnabled:a,aliasFormatted:i}=e;let s="";if(i?s+=`${i}, `:a&&(s+=" "),s+=n,"placeholder"in r&&typeof r.placeholder=="string")s+=`${this.flagOperator(e)}${r.placeholder}`;else{const o=this.flagParameter("type"in r?r.type:r);o&&(s+=`${this.flagOperator(e)}${o}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){let r="description"in e?e.description??"":"";if("default"in e){let{default:n}=e;typeof n=="function"&&(n=n()),n&&(r+=` (default: ${this.flagDefault(n)})`)}return r}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(r=>this.render(r)).join(`
9
+ `);if("type"in e&&this[e.type]){const r=this[e.type];if(typeof r=="function")return r.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const y=/^[\w.-]+$/,{stringify:d}=JSON,Z=/[|\\{}()[\]^$+*?.]/;function w(t){const e=[];let r,n;for(const a of t){if(n)throw new Error(`Invalid parameter: Spread parameter ${d(n)} must be last`);const i=a[0],s=a[a.length-1];let o;if(i==="<"&&s===">"&&(o=!0,r))throw new Error(`Invalid parameter: Required parameter ${d(a)} cannot come after optional parameter ${d(r)}`);if(i==="["&&s==="]"&&(o=!1,r=a),o===void 0)throw new Error(`Invalid parameter: ${d(a)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let l=a.slice(1,-1);const f=l.slice(-3)==="...";f&&(n=a,l=l.slice(0,-3));const u=l.match(Z);if(u)throw new Error(`Invalid parameter: ${d(a)}. Invalid character found ${d(u[0])}`);e.push({name:l,required:o,spread:f})}return e}function b(t,e,r,n){for(let a=0;a<e.length;a+=1){const{name:i,required:s,spread:o}=e[a],l=S(i);if(l in t)throw new Error(`Invalid parameter: ${d(i)} is used more than once.`);const f=o?r.slice(a):r[a];if(o&&(a=e.length),s&&(!f||o&&f.length===0))return console.error(`Error: Missing required parameter ${d(i)}
10
+ `),n(),process.exit(1);t[l]=f}}function z(t){return t===void 0||t!==!1}function j(t,e,r,n){const a={...e.flags},i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,o=z(s);o&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const l=q.typeFlag(a,n,{ignore:e.ignoreArgv}),f=()=>{console.log(e.version)};if(i&&l.flags.version===!0)return f(),process.exit(0);const u=new W,_=o&&s?.render?s.render:c=>u.render(c),p=c=>{const m=J({...e,...c?{help:c}:{},flags:a});console.log(_(m,u))};if(o&&l.flags.help===!0)return p(),process.exit(0);if(e.parameters){let{parameters:c}=e,m=l._;const g=c.indexOf("--"),$=c.slice(g+1),h=Object.create(null);if(g>-1&&$.length>0){c=c.slice(0,g);const E=l._["--"];m=m.slice(0,-E.length||void 0),b(h,w(c),m,p),b(h,w($),E,p)}else b(h,w(c),m,p);Object.assign(l._,h)}const v={...l,showVersion:f,showHelp:p};return typeof r=="function"&&r(v),{command:t,...v}}function G(t,e){const r=new Map;for(const n of e){const a=[n.options.name],{alias:i}=n.options;i&&(Array.isArray(i)?a.push(...i):a.push(i));for(const s of a){if(r.has(s))throw new Error(`Duplicate command name found: ${d(s)}`);r.set(s,n)}}return r.get(t)}function K(t,e,r=process.argv.slice(2)){if(!t)throw new Error("Options is required");if("name"in t&&(!t.name||!y.test(t.name)))throw new Error(`Invalid script name: ${d(t.name)}`);const n=r[0];if(t.commands&&y.test(n)){const a=G(n,t.commands);if(a)return j(a.options.name,{...a.options,parent:t},a.callback,r.slice(1))}return j(void 0,t,e,r)}function Q(t,e){if(!t)throw new Error("Command options are required");const{name:r}=t;if(t.name===void 0)throw new Error("Command name is required");if(!y.test(r))throw new Error(`Invalid command name ${JSON.stringify(r)}. Command names must be one word.`);return{options:t,callback:e}}exports.cli=K,exports.command=Q;
package/dist/index.mjs CHANGED
@@ -1,10 +1,10 @@
1
- import S from"type-flag";import B from"tty";import I,{breakpoints as q}from"terminal-columns";const D=r=>r.replace(/[-_ ](\w)/g,(e,t)=>t.toUpperCase()),R=r=>r.replace(/\B([A-Z])/g,"-$1").toLowerCase(),L={"> 80":[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"auto"}],"> 40":[{width:"auto",paddingLeft:2,paddingRight:8,preprocess:r=>r.trim()},{width:"100%",paddingLeft:2,paddingBottom:1}],"> 0":{stdoutColumns:1e3,columns:[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"content-width"}]}};function T(r){let e=!1;const n=Object.keys(r).sort((a,i)=>a.localeCompare(i)).map(a=>{const i=r[a],s="alias"in i;return s&&(e=!0),{name:a,flag:i,flagFormatted:`--${R(a)}`,aliasesEnabled:e,aliasFormatted:s?`-${i.alias}`:void 0}}).map(a=>(a.aliasesEnabled=e,[{type:"flagName",data:a},{type:"flagDescription",data:a}]));return{type:"table",data:{tableData:n,tableBreakpoints:L}}}const P=r=>{var e;return!r||((e=r.version)!=null?e:r.help?r.help.version:void 0)},C=r=>{var e;const t="parent"in r&&((e=r.parent)==null?void 0:e.name);return(t?`${t} `:"")+r.name};function F(r){var e;const t=[];r.name&&t.push(C(r));const n=(e=P(r))!=null?e:"parent"in r&&P(r.parent);if(n&&t.push(`v${n}`),t.length!==0)return{id:"name",type:"text",data:`${t.join(" ")}
2
- `}}function H(r){const{help:e}=r;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
3
- `}}function U(r){var e;const t=r.help||{};if("usage"in t)return t.usage?{id:"usage",type:"section",data:{title:"Usage:",body:Array.isArray(t.usage)?t.usage.join(`
4
- `):t.usage}}:void 0;if(r.name){const n=[],a=[C(r)];if(r.flags&&Object.keys(r.flags).length>0&&a.push("[flags...]"),r.parameters&&r.parameters.length>0){const{parameters:i}=r,s=i.indexOf("--"),l=s>-1&&i.slice(s+1).some(o=>o.startsWith("<"));a.push(i.map(o=>o!=="--"?o:l?"--":"[--]").join(" "))}if(a.length>1&&n.push(a.join(" ")),"commands"in r&&((e=r.commands)==null?void 0:e.length)&&n.push(`${r.name} <command>`),n.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:n.join(`
5
- `)}}}}function V(r){var e;if(!("commands"in r)||!((e=r.commands)!=null&&e.length))return;const t=r.commands.map(a=>[a.options.name,a.options.help?a.options.help.description:""]);return{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:t,tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function J(r){if(!(!r.flags||Object.keys(r.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:T(r.flags),indentBody:0}}}function M(r){const{help:e}=r;if(!e||!e.examples||e.examples.length===0)return;let{examples:t}=e;if(Array.isArray(t)&&(t=t.join(`
6
- `)),t)return{id:"examples",type:"section",data:{title:"Examples:",body:t}}}function k(r){if(!("alias"in r)||!r.alias)return;const{alias:e}=r,t=Array.isArray(e)?e.join(", "):e;return{id:"aliases",type:"section",data:{title:"Aliases:",body:t}}}const W=r=>[F,H,U,V,J,M,k].map(e=>e(r)).filter(e=>Boolean(e)),Z=B.WriteStream.prototype.hasColors();class z{text(e){return e}bold(e){return Z?`\x1B[1m${e}\x1B[22m`:e.toLocaleUpperCase()}indentText({text:e,spaces:t}){return e.replace(/^/gm," ".repeat(t))}heading(e){return this.bold(e)}section({title:e,body:t,indentBody:n=2}){return`${(e?`${this.heading(e)}
7
- `:"")+(t?this.indentText({text:this.render(t),spaces:n}):"")}
8
- `}table({tableData:e,tableOptions:t,tableBreakpoints:n}){return I(e.map(a=>a.map(i=>this.render(i))),n?q(n):t)}flagParameter(e){return e===Boolean?"":e===String?"<string>":e===Number?"<number>":Array.isArray(e)?this.flagParameter(e[0]):"<value>"}flagOperator(e){return" "}flagName(e){const{flag:t,flagFormatted:n,aliasesEnabled:a,aliasFormatted:i}=e;let s="";if(i?s+=`${i}, `:a&&(s+=" "),s+=n,"placeholder"in t&&typeof t.placeholder=="string")s+=`${this.flagOperator(e)}${t.placeholder}`;else{const l=this.flagParameter("type"in t?t.type:t);l&&(s+=`${this.flagOperator(e)}${l}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){var t;let n="description"in e&&(t=e.description)!=null?t:"";if("default"in e){let{default:a}=e;typeof a=="function"&&(a=a()),a&&(n+=` (default: ${this.flagDefault(a)})`)}return n}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(t=>this.render(t)).join(`
9
- `);if("type"in e&&this[e.type]){const t=this[e.type];if(typeof t=="function")return t.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const w=/^[\w.-]+$/;var G=Object.defineProperty,K=Object.defineProperties,Q=Object.getOwnPropertyDescriptors,x=Object.getOwnPropertySymbols,X=Object.prototype.hasOwnProperty,Y=Object.prototype.propertyIsEnumerable,A=(r,e,t)=>e in r?G(r,e,{enumerable:!0,configurable:!0,writable:!0,value:t}):r[e]=t,p=(r,e)=>{for(var t in e||(e={}))X.call(e,t)&&A(r,t,e[t]);if(x)for(var t of x(e))Y.call(e,t)&&A(r,t,e[t]);return r},v=(r,e)=>K(r,Q(e));const{stringify:d}=JSON,ee=/[|\\{}()[\]^$+*?.]/;function b(r){const e=[];let t,n;for(const a of r){if(n)throw new Error(`Invalid parameter: Spread parameter ${d(n)} must be last`);const i=a[0],s=a[a.length-1];let l;if(i==="<"&&s===">"&&(l=!0,t))throw new Error(`Invalid parameter: Required parameter ${d(a)} cannot come after optional parameter ${d(t)}`);if(i==="["&&s==="]"&&(l=!1,t=a),l===void 0)throw new Error(`Invalid parameter: ${d(a)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let o=a.slice(1,-1);const f=o.slice(-3)==="...";f&&(n=a,o=o.slice(0,-3));const u=o.match(ee);if(u)throw new Error(`Invalid parameter: ${d(a)}. Invalid character found ${d(u[0])}`);e.push({name:o,required:l,spread:f})}return e}function $(r,e,t,n){for(let a=0;a<e.length;a+=1){const{name:i,required:s,spread:l}=e[a],o=D(i);if(o in r)throw new Error(`Invalid parameter: ${d(i)} is used more than once.`);const f=l?t.slice(a):t[a];if(l&&(a=e.length),s&&(!f||l&&f.length===0))return console.error(`Error: Missing required parameter ${d(i)}
10
- `),n(),process.exit(1);r[o]=f}}function re(r){return r===void 0||r!==!1}function j(r,e,t,n){const a=p({},e.flags),i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,l=re(s);l&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const o=S(a,n),f=()=>{console.log(e.version)};if(i&&o.flags.version===!0)return f(),process.exit(0);const u=new z,N=l&&(s==null?void 0:s.render)?s.render:c=>u.render(c),h=c=>{const m=W(v(p(p({},e),c?{help:c}:{}),{flags:a}));console.log(N(m,u))};if(l&&o.flags.help===!0)return h(),process.exit(0);if(e.parameters){let{parameters:c}=e,m=o._;const y=c.indexOf("--"),O=c.slice(y+1),g=Object.create(null);if(y>-1&&O.length>0){c=c.slice(0,y);const E=o._["--"];m=m.slice(0,-E.length||void 0),$(g,b(c),m,h),$(g,b(O),E,h)}else $(g,b(c),m,h);Object.assign(o._,g)}const _=v(p({},o),{showVersion:f,showHelp:h});return typeof t=="function"&&t(_),p({command:r},_)}function te(r,e){const t=new Map;for(const n of e){const a=[n.options.name],{alias:i}=n.options;i&&(Array.isArray(i)?a.push(...i):a.push(i));for(const s of a){if(t.has(s))throw new Error(`Duplicate command name found: ${d(s)}`);t.set(s,n)}}return t.get(r)}function ae(r,e,t=process.argv.slice(2)){if(!r)throw new Error("Options is required");if("name"in r&&(!r.name||!w.test(r.name)))throw new Error(`Invalid script name: ${d(r.name)}`);const n=t[0];if(r.commands&&w.test(n)){const a=te(n,r.commands);if(a)return j(a.options.name,v(p({},a.options),{parent:r}),a.callback,t.slice(1))}return j(void 0,r,e,t)}function ne(r,e){if(!r)throw new Error("Command options are required");const{name:t}=r;if(r.name===void 0)throw new Error("Command name is required");if(!w.test(t))throw new Error(`Invalid command name ${JSON.stringify(t)}. Command names must be one word.`);return{options:r,callback:e}}export{ae as cli,ne as command};
1
+ import{typeFlag as j}from"type-flag";import N from"tty";import B,{breakpoints as P}from"terminal-columns";const S=t=>t.replace(/[-_ ](\w)/g,(e,r)=>r.toUpperCase()),q=t=>t.replace(/\B([A-Z])/g,"-$1").toLowerCase(),I={"> 80":[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"auto"}],"> 40":[{width:"auto",paddingLeft:2,paddingRight:8,preprocess:t=>t.trim()},{width:"100%",paddingLeft:2,paddingBottom:1}],"> 0":{stdoutColumns:1e3,columns:[{width:"content-width",paddingLeft:2,paddingRight:8},{width:"content-width"}]}};function R(t){let e=!1;const n=Object.keys(t).sort((a,i)=>a.localeCompare(i)).map(a=>{const i=t[a],s="alias"in i;return s&&(e=!0),{name:a,flag:i,flagFormatted:`--${q(a)}`,aliasesEnabled:e,aliasFormatted:s?`-${i.alias}`:void 0}}).map(a=>(a.aliasesEnabled=e,[{type:"flagName",data:a},{type:"flagDescription",data:a}]));return{type:"table",data:{tableData:n,tableBreakpoints:I}}}const A=t=>!t||(t.version??(t.help?t.help.version:void 0)),C=t=>{const e="parent"in t&&t.parent?.name;return(e?`${e} `:"")+t.name};function D(t){const e=[];t.name&&e.push(C(t));const r=A(t)??("parent"in t&&A(t.parent));if(r&&e.push(`v${r}`),e.length!==0)return{id:"name",type:"text",data:`${e.join(" ")}
2
+ `}}function L(t){const{help:e}=t;if(!(!e||!e.description))return{id:"description",type:"text",data:`${e.description}
3
+ `}}function T(t){const e=t.help||{};if("usage"in e)return e.usage?{id:"usage",type:"section",data:{title:"Usage:",body:Array.isArray(e.usage)?e.usage.join(`
4
+ `):e.usage}}:void 0;if(t.name){const r=[],n=[C(t)];if(t.flags&&Object.keys(t.flags).length>0&&n.push("[flags...]"),t.parameters&&t.parameters.length>0){const{parameters:a}=t,i=a.indexOf("--"),s=i>-1&&a.slice(i+1).some(o=>o.startsWith("<"));n.push(a.map(o=>o!=="--"?o:s?"--":"[--]").join(" "))}if(n.length>1&&r.push(n.join(" ")),"commands"in t&&t.commands?.length&&r.push(`${t.name} <command>`),r.length>0)return{id:"usage",type:"section",data:{title:"Usage:",body:r.join(`
5
+ `)}}}}function _(t){if(!("commands"in t)||!t.commands?.length)return;const e=t.commands.map(n=>[n.options.name,n.options.help?n.options.help.description:""]);return{id:"commands",type:"section",data:{title:"Commands:",body:{type:"table",data:{tableData:e,tableOptions:[{width:"content-width",paddingLeft:2,paddingRight:8}]}},indentBody:0}}}function k(t){if(!(!t.flags||Object.keys(t.flags).length===0))return{id:"flags",type:"section",data:{title:"Flags:",body:R(t.flags),indentBody:0}}}function F(t){const{help:e}=t;if(!e||!e.examples||e.examples.length===0)return;let{examples:r}=e;if(Array.isArray(r)&&(r=r.join(`
6
+ `)),r)return{id:"examples",type:"section",data:{title:"Examples:",body:r}}}function H(t){if(!("alias"in t)||!t.alias)return;const{alias:e}=t,r=Array.isArray(e)?e.join(", "):e;return{id:"aliases",type:"section",data:{title:"Aliases:",body:r}}}const U=t=>[D,L,T,_,k,F,H].map(e=>e(t)).filter(Boolean),J=N.WriteStream.prototype.hasColors();class M{text(e){return e}bold(e){return J?`\x1B[1m${e}\x1B[22m`:e.toLocaleUpperCase()}indentText({text:e,spaces:r}){return e.replace(/^/gm," ".repeat(r))}heading(e){return this.bold(e)}section({title:e,body:r,indentBody:n=2}){return`${(e?`${this.heading(e)}
7
+ `:"")+(r?this.indentText({text:this.render(r),spaces:n}):"")}
8
+ `}table({tableData:e,tableOptions:r,tableBreakpoints:n}){return B(e.map(a=>a.map(i=>this.render(i))),n?P(n):r)}flagParameter(e){return e===Boolean?"":e===String?"<string>":e===Number?"<number>":Array.isArray(e)?this.flagParameter(e[0]):"<value>"}flagOperator(e){return" "}flagName(e){const{flag:r,flagFormatted:n,aliasesEnabled:a,aliasFormatted:i}=e;let s="";if(i?s+=`${i}, `:a&&(s+=" "),s+=n,"placeholder"in r&&typeof r.placeholder=="string")s+=`${this.flagOperator(e)}${r.placeholder}`;else{const o=this.flagParameter("type"in r?r.type:r);o&&(s+=`${this.flagOperator(e)}${o}`)}return s}flagDefault(e){return JSON.stringify(e)}flagDescription({flag:e}){let r="description"in e?e.description??"":"";if("default"in e){let{default:n}=e;typeof n=="function"&&(n=n()),n&&(r+=` (default: ${this.flagDefault(n)})`)}return r}render(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.map(r=>this.render(r)).join(`
9
+ `);if("type"in e&&this[e.type]){const r=this[e.type];if(typeof r=="function")return r.call(this,e.data)}throw new Error(`Invalid node type: ${JSON.stringify(e)}`)}}const y=/^[\w.-]+$/,{stringify:d}=JSON,V=/[|\\{}()[\]^$+*?.]/;function w(t){const e=[];let r,n;for(const a of t){if(n)throw new Error(`Invalid parameter: Spread parameter ${d(n)} must be last`);const i=a[0],s=a[a.length-1];let o;if(i==="<"&&s===">"&&(o=!0,r))throw new Error(`Invalid parameter: Required parameter ${d(a)} cannot come after optional parameter ${d(r)}`);if(i==="["&&s==="]"&&(o=!1,r=a),o===void 0)throw new Error(`Invalid parameter: ${d(a)}. Must be wrapped in <> (required parameter) or [] (optional parameter)`);let l=a.slice(1,-1);const f=l.slice(-3)==="...";f&&(n=a,l=l.slice(0,-3));const p=l.match(V);if(p)throw new Error(`Invalid parameter: ${d(a)}. Invalid character found ${d(p[0])}`);e.push({name:l,required:o,spread:f})}return e}function b(t,e,r,n){for(let a=0;a<e.length;a+=1){const{name:i,required:s,spread:o}=e[a],l=S(i);if(l in t)throw new Error(`Invalid parameter: ${d(i)} is used more than once.`);const f=o?r.slice(a):r[a];if(o&&(a=e.length),s&&(!f||o&&f.length===0))return console.error(`Error: Missing required parameter ${d(i)}
10
+ `),n(),process.exit(1);t[l]=f}}function W(t){return t===void 0||t!==!1}function x(t,e,r,n){const a={...e.flags},i=e.version;i&&(a.version={type:Boolean,description:"Show version"});const{help:s}=e,o=W(s);o&&!("help"in a)&&(a.help={type:Boolean,alias:"h",description:"Show help"});const l=j(a,n,{ignore:e.ignoreArgv}),f=()=>{console.log(e.version)};if(i&&l.flags.version===!0)return f(),process.exit(0);const p=new M,O=o&&s?.render?s.render:c=>p.render(c),u=c=>{const m=U({...e,...c?{help:c}:{},flags:a});console.log(O(m,p))};if(o&&l.flags.help===!0)return u(),process.exit(0);if(e.parameters){let{parameters:c}=e,m=l._;const g=c.indexOf("--"),v=c.slice(g+1),h=Object.create(null);if(g>-1&&v.length>0){c=c.slice(0,g);const E=l._["--"];m=m.slice(0,-E.length||void 0),b(h,w(c),m,u),b(h,w(v),E,u)}else b(h,w(c),m,u);Object.assign(l._,h)}const $={...l,showVersion:f,showHelp:u};return typeof r=="function"&&r($),{command:t,...$}}function Z(t,e){const r=new Map;for(const n of e){const a=[n.options.name],{alias:i}=n.options;i&&(Array.isArray(i)?a.push(...i):a.push(i));for(const s of a){if(r.has(s))throw new Error(`Duplicate command name found: ${d(s)}`);r.set(s,n)}}return r.get(t)}function z(t,e,r=process.argv.slice(2)){if(!t)throw new Error("Options is required");if("name"in t&&(!t.name||!y.test(t.name)))throw new Error(`Invalid script name: ${d(t.name)}`);const n=r[0];if(t.commands&&y.test(n)){const a=Z(n,t.commands);if(a)return x(a.options.name,{...a.options,parent:t},a.callback,r.slice(1))}return x(void 0,t,e,r)}function G(t,e){if(!t)throw new Error("Command options are required");const{name:r}=t;if(t.name===void 0)throw new Error("Command name is required");if(!y.test(r))throw new Error(`Invalid command name ${JSON.stringify(r)}. Command names must be one word.`);return{options:t,callback:e}}export{z as cli,G as command};
package/package.json CHANGED
@@ -1,77 +1,43 @@
1
1
  {
2
- "name": "cleye",
3
- "version": "1.2.0",
4
- "description": "The intuitive CLI development tool",
5
- "keywords": [
6
- "cli",
7
- "command line",
8
- "argv",
9
- "parameters",
10
- "flags",
11
- "node",
12
- "typescript"
13
- ],
14
- "license": "MIT",
15
- "repository": "privatenumber/cleye",
16
- "funding": "https://github.com/privatenumber/cleye?sponsor=1",
17
- "author": {
18
- "name": "Hiroki Osame",
19
- "email": "hiroki.osame@gmail.com"
20
- },
21
- "files": [
22
- "dist"
23
- ],
24
- "main": "./dist/index.js",
25
- "module": "./dist/index.mjs",
26
- "types": "./dist/index.d.ts",
27
- "exports": {
28
- "require": "./dist/index.js",
29
- "import": "./dist/index.mjs",
30
- "types": "./dist/index.d.ts"
31
- },
32
- "scripts": {
33
- "build": "pkgroll --minify",
34
- "lint": "eslint .",
35
- "pretest": "npm run build",
36
- "test": "CI=true jest",
37
- "test:dts": "tsd"
38
- },
39
- "husky": {
40
- "hooks": {
41
- "pre-commit": "lint-staged && npm test"
42
- }
43
- },
44
- "lint-staged": {
45
- "*.ts": [
46
- "eslint"
47
- ]
48
- },
49
- "dependencies": {
50
- "terminal-columns": "^1.4.1",
51
- "type-flag": "^2.1.0"
52
- },
53
- "devDependencies": {
54
- "@pvtnbr/eslint-config": "^0.19.1",
55
- "@types/jest": "^27.4.1",
56
- "@types/node": "^17.0.29",
57
- "colorette": "^2.0.16",
58
- "es-jest": "^1.5.0",
59
- "eslint": "^8.14.0",
60
- "esno": "^0.13.0",
61
- "husky": "^4.3.8",
62
- "jest": "^27.4.7",
63
- "lint-staged": "^12.1.2",
64
- "pkgroll": "^1.0.5",
65
- "tsd": "^0.20.0",
66
- "typescript": "^4.6.3"
67
- },
68
- "eslintConfig": {
69
- "extends": "@pvtnbr",
70
- "rules": {
71
- "unicorn/no-process-exit": "off"
72
- }
73
- },
74
- "tsd": {
75
- "directory": "tests"
76
- }
77
- }
2
+ "name": "cleye",
3
+ "version": "1.3.0",
4
+ "description": "The intuitive CLI development tool",
5
+ "keywords": [
6
+ "cli",
7
+ "command line",
8
+ "argv",
9
+ "parameters",
10
+ "flags",
11
+ "node",
12
+ "typescript"
13
+ ],
14
+ "license": "MIT",
15
+ "repository": "privatenumber/cleye",
16
+ "funding": "https://github.com/privatenumber/cleye?sponsor=1",
17
+ "author": {
18
+ "name": "Hiroki Osame",
19
+ "email": "hiroki.osame@gmail.com"
20
+ },
21
+ "files": [
22
+ "dist"
23
+ ],
24
+ "main": "./dist/index.js",
25
+ "module": "./dist/index.mjs",
26
+ "types": "./dist/index.d.ts",
27
+ "exports": {
28
+ "require": "./dist/index.js",
29
+ "import": "./dist/index.mjs",
30
+ "types": "./dist/index.d.ts"
31
+ },
32
+ "imports": {
33
+ "#cleye": {
34
+ "types": "./src/index.ts",
35
+ "development": "./src/index.ts",
36
+ "default": "./dist/index.js"
37
+ }
38
+ },
39
+ "dependencies": {
40
+ "terminal-columns": "^1.4.1",
41
+ "type-flag": "^3.0.0"
42
+ }
43
+ }