@clerc/parser 1.1.1 → 1.2.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/dist/index.d.mts +46 -1
- package/dist/index.mjs +1 -1
- package/package.json +2 -2
package/dist/index.d.mts
CHANGED
|
@@ -172,4 +172,49 @@ declare function createParser<T extends FlagsDefinition>(options?: ParserOptions
|
|
|
172
172
|
};
|
|
173
173
|
declare const parse: <T extends FlagsDefinition>(args: string[], options?: ParserOptions<T>) => ParsedResult<InferFlags<T>>;
|
|
174
174
|
//#endregion
|
|
175
|
-
|
|
175
|
+
//#region src/utils.d.ts
|
|
176
|
+
/**
|
|
177
|
+
* Infers the implicit default value for a flag type based on its type
|
|
178
|
+
* constructor. This is useful for help output to show the default values of
|
|
179
|
+
* types that have built-in defaults.
|
|
180
|
+
*
|
|
181
|
+
* - Boolean: false
|
|
182
|
+
* - [Boolean] (Counter): 0
|
|
183
|
+
* - [T] (Array): []
|
|
184
|
+
* - Object: {}
|
|
185
|
+
* - Others: undefined (no implicit default)
|
|
186
|
+
*
|
|
187
|
+
* @param type The type value (constructor or array of constructor)
|
|
188
|
+
* @returns The inferred default value, or undefined if no implicit default
|
|
189
|
+
*/
|
|
190
|
+
declare function inferDefault(type: TypeValue): unknown;
|
|
191
|
+
/**
|
|
192
|
+
* Default value coercion for Object type. Converts "true" / "" to true, "false"
|
|
193
|
+
* to false, other values remain unchanged.
|
|
194
|
+
*
|
|
195
|
+
* @param value The raw string value from CLI
|
|
196
|
+
* @returns Coerced value (boolean or string)
|
|
197
|
+
*/
|
|
198
|
+
declare function coerceObjectValue(value: string): string | boolean;
|
|
199
|
+
/**
|
|
200
|
+
* Sets a value at a nested path in an object, creating intermediate objects as
|
|
201
|
+
* needed. Does NOT apply type conversion - value is set as-is. Overwrites
|
|
202
|
+
* existing values.
|
|
203
|
+
*
|
|
204
|
+
* @param obj The target object
|
|
205
|
+
* @param path Dot-separated path (e.g., "foo.bar.baz")
|
|
206
|
+
* @param value The value to set (used as-is, no type conversion)
|
|
207
|
+
*/
|
|
208
|
+
declare function setDotValues(obj: any, path: string, value: any): void;
|
|
209
|
+
/**
|
|
210
|
+
* Similar to setDotValues but handles duplicate keys by converting to arrays.
|
|
211
|
+
* Does NOT apply type conversion - value is set as-is. Useful for flags that
|
|
212
|
+
* can be specified multiple times.
|
|
213
|
+
*
|
|
214
|
+
* @param obj The target object
|
|
215
|
+
* @param path Dot-separated path (e.g., "foo.bar")
|
|
216
|
+
* @param value The value to set or append (used as-is, no type conversion)
|
|
217
|
+
*/
|
|
218
|
+
declare function appendDotValues(obj: any, path: string, value: any): void;
|
|
219
|
+
//#endregion
|
|
220
|
+
export { BaseFlagOptions, DOUBLE_DASH, FlagDefaultValue, FlagDefaultValueFunction, FlagDefinitionValue, FlagOptions, FlagsDefinition, IgnoreFunction, InferFlags, InvalidSchemaError, KNOWN_FLAG, ObjectInputType, PARAMETER, ParsedResult, ParserOptions, RawInputType, TypeFunction, TypeValue, UNKNOWN_FLAG, appendDotValues, coerceObjectValue, createParser, inferDefault, parse, setDotValues };
|
package/dist/index.mjs
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{camelCase as e,
|
|
1
|
+
import{camelCase as e,hasOwn as t,looseIsArray as n,resolveValue as r}from"@clerc/utils";var i=class extends Error{constructor(e){super(`Invalid schema: ${e}`),this.name=`InvalidSchemaError`}};const a=`known-flag`,o=`unknown-flag`,s=`parameter`;function c(e,t,n,r,i,c){let l=0,u=!1,d=e.length,f={current:``,index:0,hasNext:!1,next:``,shouldIgnore:e=>i?i(n(e)?r(e)?a:o:s,e):!1,eat:()=>{if(l+1>=d)return;let t=e[l+1];if(f.shouldIgnore(t)){f.exit();return}return l++,p(),e[l]},exit:(n=!0)=>{u||=(n&&t.ignored.push(...e.slice(l+1)),!0)}};function p(){f.current=e[l],f.index=l,f.hasNext=l+1<d,f.next=f.hasNext?e[l+1]:``}for(l=0;l<d&&!u;l++)p(),c(f)}const l={delimiters:[`=`,`:`]},u=(e={})=>({...l,...e}),d=e=>typeof e==`function`||n(e)?{type:e}:e;function f(t,n){let r=new Map,a=new Map,o=e=>t.some(t=>e.includes(t))||e.includes(` `)||e.includes(`.`);function s(e,t){let n=`Flag "${e}"`;if(Array.isArray(t.type)&&t.type.length>1)throw new i(`${n} has an invalid type array. Only single-element arrays are allowed to denote multiple occurrences.`);if(e.length<2)throw new i(`${n} name must be at least 2 characters long.`);let r=[e];if(t.short){if(t.short.length!==1)throw new i(`${n} short flag must be exactly 1 character long.`);r.push(t.short)}if(r.some(o))throw new i(`${n} contains reserved characters, which are used as delimiters.`);if(t.required&&t.default!==void 0)throw new i(`${n} cannot be both required and have a default value.`)}for(let[t,i]of Object.entries(n)){let n=d(i);s(t,n),r.set(t,n),a.set(t,t),a.set(e(t),t),n.short&&a.set(n.short,t)}return{configs:r,aliases:a}}const p=(e,t)=>Array.isArray(e)&&e[0]===t;function m(e){if(n(e))return p(e,Boolean)?0:[];if(e===Boolean)return!1;if(e===Object)return{}}const h=e=>e>=65&&e<=90||e>=97&&e<=122,g=e=>e>=48&&e<=57;function _(e,t,r,i){let{type:a}=i;n(a)?p(a,Boolean)?e[t]=(e[t]??0)+1:(e[t]??=[]).push(a[0](r)):e[t]=a(r)}function v(e){return e===`true`||e===``?!0:e===`false`?!1:e}function y(e,t,n){let r=t.split(`.`),i=e;for(let e=0;e<r.length-1;e++){let t=r[e];i[t]??={},i=i[t]}let a=r[r.length-1];i[a]=n}function b(e,n,r){let i=n.split(`.`),a=e;for(let e=0;e<i.length-1;e++){let n=i[e];if(t(a,n)&&(typeof a[n]!=`object`||a[n]===null))return;a[n]??={},a=a[n]}let o=i[i.length-1];if(t(a,o)){let e=a[o];Array.isArray(e)?e.push(r):a[o]=[e,r]}else a[o]=r}function x(e,t){let n=-1,r=0;for(let i of t){let t=e.indexOf(i);t!==-1&&(n===-1||t<n)&&(n=t,r=i.length)}return n===-1?{rawName:e,rawValue:void 0,hasSep:!1}:{rawName:e.slice(0,n),rawValue:e.slice(n+r),hasSep:!0}}const S=`--`;function C(t={}){let{flags:n={},delimiters:i,ignore:a}=u(t),{configs:o,aliases:s}=f(i,n);function l(t,n=!1){let r=t.indexOf(`.`),i=r===-1?t:t.slice(0,r),a=s.get(i);if(!a&&(a=s.get(e(i)),!a))return;let c=o.get(a);if(!(!n&&c.short===i))return{key:a,config:c,path:r===-1?void 0:t.slice(r+1)}}function d(e){if(!e.startsWith(`no`))return;let t=e[2]===`-`?e.slice(3):e.length>2&&e.charCodeAt(2)>=65&&e.charCodeAt(2)<=90?e[2].toLowerCase()+e.slice(3):``;if(t){let e=l(t);if(e?.config.type===Boolean&&e.config.negatable!==!1)return e}}function y(e){if(e.charCodeAt(0)!==45)return!1;let t=e.length;if(t<2)return!1;let n=e.charCodeAt(1);if(h(n))return!0;if(g(n)){let t=n!==45;return!!l(t?e[1]:e.slice(2),t)}return n===45&&t>2?h(e.charCodeAt(2)):!1}function S(e){if(e.charCodeAt(1)===45){let{rawName:t}=x(e.slice(2),i);return!!(l(t,!1)||d(t))}let t=e.slice(1);for(let e of t)if(!l(e,!0))return!1;return!0}return{parse:t=>{let n={parameters:[],doubleDash:[],flags:{},raw:t,unknown:{},ignored:[],missingRequiredFlags:[]};c(t,n,y,S,a,({current:r,eat:a,exit:o,hasNext:s,index:c,next:u,shouldIgnore:f})=>{if(r===`--`){n.doubleDash.push(...t.slice(c+1)),o(!1);return}if(f(r)){n.ignored.push(r),o();return}if(!y(r)){n.parameters.push(r);return}let m=!r.startsWith(`--`),h=r.slice(m?1:2);if(m){let e=h.length;for(let t=0;t<e;t++){let r=h[t],i=l(r,!0);if(!i){n.unknown[r]=!0;continue}let{key:o,config:c}=i,d=c.type;if(d===Boolean||p(d,Boolean))_(n.flags,o,`true`,c);else if(t+1<e){_(n.flags,o,h.slice(t+1),c);break}else{let e=s&&!y(u)?a()??``:``;_(n.flags,o,e,c)}}}else{let{rawName:t,rawValue:r,hasSep:o}=x(h,i),c=l(t),f=!1;if(!c){let e=d(t);e&&(c=e,f=!0)}if(!c){let i=e(t);if(o)n.unknown[i]=r;else if(s&&!y(u)){let e=a();n.unknown[i]=e??!0}else n.unknown[i]=!0;return}let{key:p,config:m,path:g}=c;if(g){if(m.type===Object){n.flags[p]??={};let e=o?r:s&&!y(u)?a()??``:``;b(n.flags[p],g,v(e))}}else if(m.type===Boolean){let e=o?r!==`false`:!0;n.flags[p]=f?!e:e}else{let e=o?r:s&&!y(u)?a()??``:``;_(n.flags,p,e,m)}}});for(let[e,t]of o.entries())n.flags[e]===void 0&&(t.default===void 0?t.required?n.missingRequiredFlags.push(e):n.flags[e]=m(t.type):n.flags[e]=r(t.default));return n}}}const w=(e,t={})=>C(t).parse(e);export{S as DOUBLE_DASH,i as InvalidSchemaError,a as KNOWN_FLAG,s as PARAMETER,o as UNKNOWN_FLAG,b as appendDotValues,v as coerceObjectValue,C as createParser,m as inferDefault,w as parse,y as setDotValues};
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@clerc/parser",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.2.0",
|
|
4
4
|
"author": "Ray <i@mk1.io> (https://github.com/so1ve)",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Clerc parser",
|
|
@@ -40,7 +40,7 @@
|
|
|
40
40
|
"access": "public"
|
|
41
41
|
},
|
|
42
42
|
"dependencies": {
|
|
43
|
-
"@clerc/utils": "1.
|
|
43
|
+
"@clerc/utils": "1.2.0"
|
|
44
44
|
},
|
|
45
45
|
"devDependencies": {
|
|
46
46
|
"@types/minimist": "^1.2.5",
|