zod-args-parser 1.2.8 → 2.0.0-alpha.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 +9 -9
- package/lib/commonjs/parser/parse/parse.cjs +1 -1
- package/lib/commonjs/parser/parse/parse.cjs.map +1 -1
- package/lib/commonjs/parser/validate/validate-type.cjs.map +1 -1
- package/lib/commonjs/parser/validate/validate.cjs +1 -1
- package/lib/commonjs/parser/validate/validate.cjs.map +1 -1
- package/lib/module/parser/parse/parse.mjs +1 -1
- package/lib/module/parser/parse/parse.mjs.map +1 -1
- package/lib/module/parser/validate/validate-type.mjs.map +1 -1
- package/lib/module/parser/validate/validate.mjs +1 -1
- package/lib/module/parser/validate/validate.mjs.map +1 -1
- package/lib/typescript/parser/parse/parse-types.d.ts +2 -2
- package/lib/typescript/parser/parse/parse-types.d.ts.map +1 -1
- package/lib/typescript/parser/parse/parse.d.ts.map +1 -1
- package/lib/typescript/parser/validate/validate-type.d.ts +2 -1
- package/lib/typescript/parser/validate/validate-type.d.ts.map +1 -1
- package/lib/typescript/parser/validate/validate.d.ts +1 -0
- package/lib/typescript/parser/validate/validate.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/parser/parse/parse-types.ts +2 -2
- package/src/parser/parse/parse.ts +22 -17
- package/src/parser/validate/validate-type.ts +7 -8
- package/src/parser/validate/validate.ts +21 -16
package/README.md
CHANGED
|
@@ -85,7 +85,7 @@ const cliSchema = createCli({
|
|
|
85
85
|
|
|
86
86
|
// Execute this function when the CLI is run (no subcommands)
|
|
87
87
|
cliSchema.setAction(results => {
|
|
88
|
-
const { help, version, verbose } = results;
|
|
88
|
+
const { help, version, verbose } = results.options;
|
|
89
89
|
|
|
90
90
|
if (help) {
|
|
91
91
|
results.printCliHelp();
|
|
@@ -734,14 +734,14 @@ The context object is generated after parsing the CLI arguments and before valid
|
|
|
734
734
|
|
|
735
735
|
#### Results
|
|
736
736
|
|
|
737
|
-
| Name
|
|
738
|
-
|
|
|
739
|
-
| subcommand
|
|
740
|
-
| `[
|
|
741
|
-
| arguments
|
|
742
|
-
| positional
|
|
743
|
-
| printCliHelp
|
|
744
|
-
| printSubcommandHelp
|
|
737
|
+
| Name | Type | Description |
|
|
738
|
+
| ------------------- | -------------------------------------------------------- | ----------------------------------------------------------------------------------------- |
|
|
739
|
+
| subcommand | `string \| undefined` | The name of the executed subcommand. |
|
|
740
|
+
| options | `{ [OptionName: string]: unknown }` | Validated options for the CLI/subcommand. |
|
|
741
|
+
| arguments | `unknown[] \| undefined` | Validated arguments for the CLI/subcommand. |
|
|
742
|
+
| positional | `string[] \| undefined` | Positional array for the CLI/subcommand. |
|
|
743
|
+
| printCliHelp | `(style?: HelpMessageStyle) => void` | Prints the CLI help message. See [HelpMessageStyle](#helpmsgstyle) |
|
|
744
|
+
| printSubcommandHelp | `(subcommand: string, style?: HelpMessageStyle) => void` | Prints the help message for a specified subcommand. See [HelpMessageStyle](#helpmsgstyle) |
|
|
745
745
|
|
|
746
746
|
#### HelpMessageStyle
|
|
747
747
|
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.parse=parse;var _utilities=require("../../utilities.cjs");var _zodUtilities=require("../../zod-utilities.cjs");var _parserHelpers=require("./parser-helpers.cjs");function parse(argv,...parameters){const subcommandArray=parameters;const allSubcommands=new Set(subcommandArray.flatMap(c=>[c.name,...(c.aliases||[])]));argv=(0,_parserHelpers.decoupleFlags)(argv);const results={subcommand:undefined
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.parse=parse;var _utilities=require("../../utilities.cjs");var _zodUtilities=require("../../zod-utilities.cjs");var _parserHelpers=require("./parser-helpers.cjs");function parse(argv,...parameters){const subcommandArray=parameters;const allSubcommands=new Set(subcommandArray.flatMap(c=>[c.name,...(c.aliases||[])]));argv=(0,_parserHelpers.decoupleFlags)(argv);const results={subcommand:undefined};const getSubcommandObject=()=>(0,_parserHelpers.findSubcommand)(results.subcommand,subcommandArray);for(let index=0;index<argv.length;index++){const argument_=argv[index];if(index===0){results.subcommand=allSubcommands.has(argument_)?argument_:undefined;if(results.subcommand)continue}const argumentAndValue=argument_.split("=").filter(Boolean);const argumentWithEquals=argument_.includes("=");const argument=argumentAndValue[0];const argumentValue=argumentAndValue[1];if((0,_parserHelpers.isOptionArgument)(argument)){if((0,_parserHelpers.isFlagArgument)(argument)&&argumentWithEquals){throw new Error(`Flag arguments cannot be assigned using "=": "${argument_}"`,{cause:"zod-args-parser"})}const subcommandObject=getSubcommandObject();if(!subcommandObject){throw new Error(`Unknown subcommand: "${results.subcommand}"`,{cause:"zod-args-parser"})}if(!subcommandObject.options){if(!results.subcommand){throw new Error(`Error: options are not allowed here: "${argument}"`,{cause:"zod-args-parser"})}throw new Error(`Error: subcommand "${results.subcommand}" does not allow options: "${argument}"`,{cause:"zod-args-parser"})}const option=(0,_parserHelpers.findOption)(argument,subcommandObject.options);if(!option){throw new Error(`Unknown option: "${argument}"`,{cause:"zod-args-parser"})}if(results.options&&option.name in results.options){throw new Error(`Duplicated option: "${argument}"`,{cause:"zod-args-parser"})}const isTypeBoolean=(0,_zodUtilities.isBooleanSchema)(option.type);const nextArgument=argv[index+1];let optionValue=argumentWithEquals?argumentValue:nextArgument;if(isTypeBoolean&&!argumentWithEquals){optionValue="true"}if(optionValue===undefined){throw new Error(`Expected a value for "${argument}" but got nothing`,{cause:"zod-args-parser"})}if(!argumentWithEquals&&(0,_parserHelpers.isOptionArgument)(optionValue)){throw new Error(`Expected a value for "${argument}" but got an argument "${nextArgument}"`,{cause:"zod-args-parser"})}if(!results.options){results.options={}}results.options[option.name]={name:option.name,schema:option.type,flag:argument,rawValue:optionValue.toString(),source:"cli"};if(!argumentWithEquals&&!isTypeBoolean){index++}continue}const subcommandObject=getSubcommandObject();if(subcommandObject?.arguments){if(!results.arguments){results.arguments=[]}const currentArgumentCount=results.arguments.length;if(currentArgumentCount<subcommandObject.arguments.length){const argumentType=subcommandObject.arguments[currentArgumentCount].type;results.arguments.push({schema:argumentType,rawValue:argument_,source:"cli"});continue}}if(subcommandObject?.allowPositional){if(!results.positional){results.positional=[]}results.positional.push(argument_);continue}if(!results.subcommand){throw new Error(`Unexpected argument "${argument_}": positional arguments are not allowed here`,{cause:"zod-args-parser"})}throw new Error(`Unexpected argument "${argument_}": positional arguments are not allowed for subcommand "${results.subcommand}"`,{cause:"zod-args-parser"})}const subcommandObject=getSubcommandObject();if(!subcommandObject){throw new Error(`Unknown subcommand: "${results.subcommand}"`,{cause:"zod-args-parser"})}if(subcommandObject.options){if(!results.options){results.options={}}for(const option of subcommandObject.options){if(results.options&&option.name in results.options)continue;const optional=(0,_zodUtilities.isOptionalSchema)(option.type);const defaultValue=(0,_zodUtilities.schemaDefaultValue)(option.type);if(optional){if(defaultValue===undefined){continue}results.options[option.name]={name:option.name,schema:option.type,source:"default"};continue}throw new Error(`Missing required option: ${(0,_parserHelpers.transformOptionToArgument)(option.name)}`,{cause:"zod-args-parser"})}}if(subcommandObject.arguments){if(!results.arguments){results.arguments=[]}const currentArgumentCount=results.arguments.length??0;const subcommandArgumentCount=subcommandObject.arguments.length;if(currentArgumentCount<subcommandArgumentCount){for(let index=currentArgumentCount;index<subcommandArgumentCount;index++){const argumentType=subcommandObject.arguments[index].type;const optional=(0,_zodUtilities.isOptionalSchema)(argumentType);const defaultValue=(0,_zodUtilities.schemaDefaultValue)(argumentType);if(optional){if(defaultValue===undefined){continue}if(!results.arguments)results.arguments=[];results.arguments.push({schema:argumentType,source:"default"});continue}throw new Error(`the ${(0,_utilities.generateOrdinalSuffix)(index)} argument is required: "${subcommandObject.arguments[index].name}"`,{cause:"zod-args-parser"})}}}if(subcommandObject.allowPositional&&!results.positional){results.positional=[]}return results}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_utilities","require","_zodUtilities","_parserHelpers","parse","argv","parameters","subcommandArray","allSubcommands","Set","flatMap","c","name","aliases","decoupleFlags","results","subcommand","undefined","options","getSubcommandObject","findSubcommand","index","length","argument_","has","subcommandObject","allowPositional","positional","arguments","argumentAndValue","split","filter","Boolean","argumentWithEquals","includes","argument","argumentValue","isOptionArgument","isFlagArgument","Error","cause","option","findOption","isTypeBoolean","isBooleanSchema","type","nextArgument","optionValue","schema","flag","rawValue","toString","source","currentArgumentCount","argumentType","push","optional","isOptionalSchema","defaultValue","schemaDefaultValue","transformOptionToArgument","subcommandArgumentCount","generateOrdinalSuffix"],"sourceRoot":"../../../../src/parser/parse","sources":["parse.ts"],"sourcesContent":["import { generateOrdinalSuffix } from \"../../utilities.js\";\nimport { isBooleanSchema, isOptionalSchema, schemaDefaultValue } from \"../../zod-utilities.js\";\nimport {\n decoupleFlags,\n findOption,\n findSubcommand,\n isFlagArgument,\n isOptionArgument,\n transformOptionToArgument,\n} from \"./parser-helpers.js\";\n\nimport type { Cli, Subcommand } from \"../../types.js\";\nimport type { ParsedContext } from \"./parse-types.js\";\n\nexport function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {\n const subcommandArray = parameters as Subcommand[];\n const allSubcommands = new Set<string>(subcommandArray.flatMap(c => [c.name, ...(c.aliases || [])]));\n\n argv = decoupleFlags(argv); // decouple flags E.g. `-rf` -> `-r, -f`\n\n const results: ParsedContext = {\n subcommand: undefined,\n options: {},\n };\n\n /** - Get current subcommand object */\n const getSubcommandObject = () => findSubcommand(results.subcommand, subcommandArray);\n\n for (let index = 0; index < argv.length; index++) {\n const argument_ = argv[index];\n\n // * Subcommand check\n if (index === 0) {\n results.subcommand = allSubcommands.has(argument_) ? argument_ : undefined;\n\n // add positional and arguments arrays\n const subcommandObject = getSubcommandObject();\n if (subcommandObject && subcommandObject.allowPositional) {\n results.positional = [];\n }\n\n if (subcommandObject && subcommandObject.arguments?.length) {\n results.arguments = [];\n }\n\n // First argument is a subcommand. Skip to the next argument\n if (results.subcommand) continue;\n }\n\n // * Option check\n\n // Check for `--option=value` or `--option value`\n const argumentAndValue = argument_.split(\"=\").filter(Boolean);\n const argumentWithEquals = argument_.includes(\"=\");\n const argument = argumentAndValue[0];\n const argumentValue: string | undefined = argumentAndValue[1];\n\n if (isOptionArgument(argument)) {\n if (isFlagArgument(argument) && argumentWithEquals) {\n throw new Error(`Flag arguments cannot be assigned using \"=\": \"${argument_}\"`, { cause: \"zod-args-parser\" });\n }\n\n const subcommandObject = getSubcommandObject();\n if (!subcommandObject) {\n throw new Error(`Unknown subcommand: \"${results.subcommand}\"`, { cause: \"zod-args-parser\" });\n }\n\n if (!subcommandObject.options) {\n if (!results.subcommand) {\n throw new Error(`Error: options are not allowed here: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n throw new Error(`Error: subcommand \"${results.subcommand}\" does not allow options: \"${argument}\"`, {\n cause: \"zod-args-parser\",\n });\n }\n\n const option = findOption(argument, subcommandObject.options);\n if (!option) {\n throw new Error(`Unknown option: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n if (option.name in results.options) {\n throw new Error(`Duplicated option: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n const isTypeBoolean = isBooleanSchema(option.type);\n const nextArgument = argv[index + 1];\n\n let optionValue: string | boolean = argumentWithEquals ? argumentValue : nextArgument;\n\n // infer value for boolean options\n if (isTypeBoolean && !argumentWithEquals) {\n optionValue = \"true\";\n }\n\n if (optionValue === undefined) {\n throw new Error(`Expected a value for \"${argument}\" but got nothing`, { cause: \"zod-args-parser\" });\n }\n\n if (!argumentWithEquals && isOptionArgument(optionValue)) {\n throw new Error(`Expected a value for \"${argument}\" but got an argument \"${nextArgument}\"`, {\n cause: \"zod-args-parser\",\n });\n }\n\n results.options[option.name] = {\n name: option.name,\n schema: option.type,\n flag: argument,\n rawValue: optionValue.toString(),\n source: \"cli\",\n };\n\n // Skip to the next argument if it is the current option’s value.\n if (!argumentWithEquals && !isTypeBoolean) {\n index++;\n }\n\n continue;\n }\n\n const subcommandObject = getSubcommandObject();\n\n // * Arguments check\n if (subcommandObject?.arguments?.length) {\n if (!results.arguments) {\n results.arguments = [];\n }\n\n const currentArgumentCount = results.arguments.length;\n\n // Any extra arguments are possibly positional\n if (currentArgumentCount < subcommandObject.arguments.length) {\n const argumentType = subcommandObject.arguments[currentArgumentCount].type;\n results.arguments.push({\n schema: argumentType,\n rawValue: argument_,\n source: \"cli\",\n });\n continue;\n }\n }\n\n // * Positional check\n if (subcommandObject?.allowPositional) {\n if (!results.positional) {\n results.positional = [];\n }\n\n results.positional.push(argument_);\n continue;\n }\n\n // * Unexpected\n if (!results.subcommand) {\n throw new Error(`Unexpected argument \"${argument_}\": positional arguments are not allowed here`, {\n cause: \"zod-args-parser\",\n });\n }\n\n throw new Error(\n `Unexpected argument \"${argument_}\": positional arguments are not allowed for subcommand \"${results.subcommand}\"`,\n { cause: \"zod-args-parser\" },\n );\n }\n\n // * Check for missing options - set defaults - add `source`\n const subcommandObject = getSubcommandObject();\n if (!subcommandObject) {\n throw new Error(`Unknown subcommand: \"${results.subcommand}\"`, { cause: \"zod-args-parser\" });\n }\n\n // Options\n if (subcommandObject.options?.length) {\n for (const option of subcommandObject.options) {\n // option already exists\n if (option.name in results.options) continue;\n\n const optional = isOptionalSchema(option.type);\n const defaultValue = schemaDefaultValue(option.type);\n\n if (optional) {\n if (defaultValue === undefined) {\n continue;\n }\n\n results.options[option.name] = { name: option.name, schema: option.type, source: \"default\" };\n continue;\n }\n\n throw new Error(`Missing required option: ${transformOptionToArgument(option.name)}`, {\n cause: \"zod-args-parser\",\n });\n }\n }\n\n // Arguments\n if (subcommandObject.arguments?.length) {\n const currentArgumentCount = results.arguments?.length ?? 0;\n const subcommandArgumentCount = subcommandObject.arguments.length;\n\n // missing arguments\n if (currentArgumentCount < subcommandArgumentCount) {\n for (let index = currentArgumentCount; index < subcommandArgumentCount; index++) {\n const argumentType = subcommandObject.arguments[index].type;\n const optional = isOptionalSchema(argumentType);\n const defaultValue = schemaDefaultValue(argumentType);\n\n if (optional) {\n if (defaultValue === undefined) {\n continue;\n }\n\n if (!results.arguments) results.arguments = [];\n\n results.arguments.push({ schema: argumentType, source: \"default\" });\n continue;\n }\n\n throw new Error(\n `the ${generateOrdinalSuffix(index)} argument is required: \"${subcommandObject.arguments[index].name}\"`,\n { cause: \"zod-args-parser\" },\n );\n }\n }\n }\n\n return results;\n}\n"],"mappings":"0FAAA,IAAAA,UAAA,CAAAC,OAAA,wBACA,IAAAC,aAAA,CAAAD,OAAA,4BACA,IAAAE,cAAA,CAAAF,OAAA,yBAYO,QAAS,CAAAG,KAAKA,CAACC,IAAc,CAAE,GAAGC,UAAkC,CAAE,CAC3E,KAAM,CAAAC,eAAe,CAAGD,UAA0B,CAClD,KAAM,CAAAE,cAAc,CAAG,GAAI,CAAAC,GAAG,CAASF,eAAe,CAACG,OAAO,CAACC,CAAC,EAAI,CAACA,CAAC,CAACC,IAAI,CAAE,IAAID,CAAC,CAACE,OAAO,EAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAEpGR,IAAI,CAAG,GAAAS,4BAAa,EAACT,IAAI,CAAC,CAE1B,KAAM,CAAAU,OAAsB,CAAG,CAC7BC,UAAU,CAAEC,SAAS,CACrBC,OAAO,CAAE,CAAC,CACZ,CAAC,CAGD,KAAM,CAAAC,mBAAmB,CAAGA,CAAA,GAAM,GAAAC,6BAAc,EAACL,OAAO,CAACC,UAAU,CAAET,eAAe,CAAC,CAErF,IAAK,GAAI,CAAAc,KAAK,CAAG,CAAC,CAAEA,KAAK,CAAGhB,IAAI,CAACiB,MAAM,CAAED,KAAK,EAAE,CAAE,CAChD,KAAM,CAAAE,SAAS,CAAGlB,IAAI,CAACgB,KAAK,CAAC,CAG7B,GAAIA,KAAK,GAAK,CAAC,CAAE,CACfN,OAAO,CAACC,UAAU,CAAGR,cAAc,CAACgB,GAAG,CAACD,SAAS,CAAC,CAAGA,SAAS,CAAGN,SAAS,CAG1E,KAAM,CAAAQ,gBAAgB,CAAGN,mBAAmB,CAAC,CAAC,CAC9C,GAAIM,gBAAgB,EAAIA,gB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AAoB,CAAGQ,uBAAuB,CAAE,CAClD,IAAK,GAAI,CAAAxC,KAAK,CAAGgC,oBAAoB,CAAEhC,KAAK,CAAGwC,uBAAuB,CAAExC,KAAK,EAAE,CAAE,CAC/E,KAAM,CAAAiC,YAAY,CAAG7B,gBAAgB,CAACG,SAAS,CAACP,KAAK,CAAC,CAACwB,IAAI,CAC3D,KAAM,CAAAW,QAAQ,CAAG,GAAAC,8BAAgB,EAACH,YAAY,CAAC,CAC/C,KAAM,CAAAI,YAAY,CAAG,GAAAC,gCAAkB,EAACL,YAAY,CAAC,CAErD,GAAIE,QAAQ,CAAE,CACZ,GAAIE,YAAY,GAAKzC,SAAS,CAAE,CAC9B,QACF,CAEA,GAAI,CAACF,OAAO,CAACa,SAAS,CAAEb,OAAO,CAACa,SAAS,CAAG,EAAE,CAE9Cb,OAAO,CAACa,SAAS,CAAC2B,IAAI,CAAC,CAAEP,MAAM,CAAEM,YAAY,CAAEF,MAAM,CAAE,SAAU,CAAC,CAAC,CACnE,QACF,CAEA,KAAM,IAAI,CAAAb,KAAK,CACb,OAAO,GAAAuB,gCAAqB,EAACzC,KAAK,CAAC,2BAA2BI,gBAAgB,CAACG,SAAS,CAACP,KAAK,CAAC,CAACT,IAAI,GAAG,CACvG,CAAE4B,KAAK,CAAE,iBAAkB,CAC7B,CACF,CACF,CACF,CAEA,MAAO,CAAAzB,OACT","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["_utilities","require","_zodUtilities","_parserHelpers","parse","argv","parameters","subcommandArray","allSubcommands","Set","flatMap","c","name","aliases","decoupleFlags","results","subcommand","undefined","getSubcommandObject","findSubcommand","index","length","argument_","has","argumentAndValue","split","filter","Boolean","argumentWithEquals","includes","argument","argumentValue","isOptionArgument","isFlagArgument","Error","cause","subcommandObject","options","option","findOption","isTypeBoolean","isBooleanSchema","type","nextArgument","optionValue","schema","flag","rawValue","toString","source","arguments","currentArgumentCount","argumentType","push","allowPositional","positional","optional","isOptionalSchema","defaultValue","schemaDefaultValue","transformOptionToArgument","subcommandArgumentCount","generateOrdinalSuffix"],"sourceRoot":"../../../../src/parser/parse","sources":["parse.ts"],"sourcesContent":["import { generateOrdinalSuffix } from \"../../utilities.js\";\nimport { isBooleanSchema, isOptionalSchema, schemaDefaultValue } from \"../../zod-utilities.js\";\nimport {\n decoupleFlags,\n findOption,\n findSubcommand,\n isFlagArgument,\n isOptionArgument,\n transformOptionToArgument,\n} from \"./parser-helpers.js\";\n\nimport type { Cli, Subcommand } from \"../../types.js\";\nimport type { ParsedContext } from \"./parse-types.js\";\n\nexport function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {\n const subcommandArray = parameters as Subcommand[];\n const allSubcommands = new Set<string>(subcommandArray.flatMap(c => [c.name, ...(c.aliases || [])]));\n\n argv = decoupleFlags(argv); // decouple flags E.g. `-rf` -> `-r, -f`\n\n const results: ParsedContext = {\n subcommand: undefined,\n };\n\n /** - Get current subcommand object */\n const getSubcommandObject = () => findSubcommand(results.subcommand, subcommandArray);\n\n for (let index = 0; index < argv.length; index++) {\n const argument_ = argv[index];\n\n // * Subcommand check\n if (index === 0) {\n results.subcommand = allSubcommands.has(argument_) ? argument_ : undefined;\n\n // First argument is a subcommand. Skip to the next argument\n if (results.subcommand) continue;\n }\n\n // * Option check\n\n // Check for `--option=value` or `--option value`\n const argumentAndValue = argument_.split(\"=\").filter(Boolean);\n const argumentWithEquals = argument_.includes(\"=\");\n const argument = argumentAndValue[0];\n const argumentValue: string | undefined = argumentAndValue[1];\n\n if (isOptionArgument(argument)) {\n if (isFlagArgument(argument) && argumentWithEquals) {\n throw new Error(`Flag arguments cannot be assigned using \"=\": \"${argument_}\"`, { cause: \"zod-args-parser\" });\n }\n\n const subcommandObject = getSubcommandObject();\n if (!subcommandObject) {\n throw new Error(`Unknown subcommand: \"${results.subcommand}\"`, { cause: \"zod-args-parser\" });\n }\n\n if (!subcommandObject.options) {\n if (!results.subcommand) {\n throw new Error(`Error: options are not allowed here: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n throw new Error(`Error: subcommand \"${results.subcommand}\" does not allow options: \"${argument}\"`, {\n cause: \"zod-args-parser\",\n });\n }\n\n const option = findOption(argument, subcommandObject.options);\n if (!option) {\n throw new Error(`Unknown option: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n if (results.options && option.name in results.options) {\n throw new Error(`Duplicated option: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n const isTypeBoolean = isBooleanSchema(option.type);\n const nextArgument = argv[index + 1];\n\n let optionValue: string | boolean = argumentWithEquals ? argumentValue : nextArgument;\n\n // infer value for boolean options\n if (isTypeBoolean && !argumentWithEquals) {\n optionValue = \"true\";\n }\n\n if (optionValue === undefined) {\n throw new Error(`Expected a value for \"${argument}\" but got nothing`, { cause: \"zod-args-parser\" });\n }\n\n if (!argumentWithEquals && isOptionArgument(optionValue)) {\n throw new Error(`Expected a value for \"${argument}\" but got an argument \"${nextArgument}\"`, {\n cause: \"zod-args-parser\",\n });\n }\n\n if (!results.options) {\n results.options = {};\n }\n\n results.options[option.name] = {\n name: option.name,\n schema: option.type,\n flag: argument,\n rawValue: optionValue.toString(),\n source: \"cli\",\n };\n\n // Skip to the next argument if it is the current option’s value.\n if (!argumentWithEquals && !isTypeBoolean) {\n index++;\n }\n\n continue;\n }\n\n const subcommandObject = getSubcommandObject();\n\n // * Arguments check\n if (subcommandObject?.arguments) {\n if (!results.arguments) {\n results.arguments = [];\n }\n\n const currentArgumentCount = results.arguments.length;\n\n // Any extra arguments are possibly positional\n if (currentArgumentCount < subcommandObject.arguments.length) {\n const argumentType = subcommandObject.arguments[currentArgumentCount].type;\n results.arguments.push({\n schema: argumentType,\n rawValue: argument_,\n source: \"cli\",\n });\n continue;\n }\n }\n\n // * Positional check\n if (subcommandObject?.allowPositional) {\n if (!results.positional) {\n results.positional = [];\n }\n\n results.positional.push(argument_);\n continue;\n }\n\n // * Unexpected\n if (!results.subcommand) {\n throw new Error(`Unexpected argument \"${argument_}\": positional arguments are not allowed here`, {\n cause: \"zod-args-parser\",\n });\n }\n\n throw new Error(\n `Unexpected argument \"${argument_}\": positional arguments are not allowed for subcommand \"${results.subcommand}\"`,\n { cause: \"zod-args-parser\" },\n );\n }\n\n // * Check for missing options - set defaults - add `source`\n const subcommandObject = getSubcommandObject();\n if (!subcommandObject) {\n throw new Error(`Unknown subcommand: \"${results.subcommand}\"`, { cause: \"zod-args-parser\" });\n }\n\n // Options\n if (subcommandObject.options) {\n if (!results.options) {\n results.options = {};\n }\n\n for (const option of subcommandObject.options) {\n // option already exists\n if (results.options && option.name in results.options) continue;\n\n const optional = isOptionalSchema(option.type);\n const defaultValue = schemaDefaultValue(option.type);\n\n if (optional) {\n if (defaultValue === undefined) {\n continue;\n }\n\n results.options[option.name] = { name: option.name, schema: option.type, source: \"default\" };\n continue;\n }\n\n throw new Error(`Missing required option: ${transformOptionToArgument(option.name)}`, {\n cause: \"zod-args-parser\",\n });\n }\n }\n\n // Arguments\n if (subcommandObject.arguments) {\n if (!results.arguments) {\n results.arguments = [];\n }\n\n const currentArgumentCount = results.arguments.length ?? 0;\n const subcommandArgumentCount = subcommandObject.arguments.length;\n\n // missing arguments\n if (currentArgumentCount < subcommandArgumentCount) {\n for (let index = currentArgumentCount; index < subcommandArgumentCount; index++) {\n const argumentType = subcommandObject.arguments[index].type;\n const optional = isOptionalSchema(argumentType);\n const defaultValue = schemaDefaultValue(argumentType);\n\n if (optional) {\n if (defaultValue === undefined) {\n continue;\n }\n\n if (!results.arguments) results.arguments = [];\n\n results.arguments.push({ schema: argumentType, source: \"default\" });\n continue;\n }\n\n throw new Error(\n `the ${generateOrdinalSuffix(index)} argument is required: \"${subcommandObject.arguments[index].name}\"`,\n { cause: \"zod-args-parser\" },\n );\n }\n }\n }\n\n if (subcommandObject.allowPositional && !results.positional) {\n results.positional = [];\n }\n\n return results;\n}\n"],"mappings":"0FAAA,IAAAA,UAAA,CAAAC,OAAA,wBACA,IAAAC,aAAA,CAAAD,OAAA,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QAAQ,CAAG,GAAAC,8BAAgB,EAACL,YAAY,CAAC,CAC/C,KAAM,CAAAM,YAAY,CAAG,GAAAC,gCAAkB,EAACP,YAAY,CAAC,CAErD,GAAII,QAAQ,CAAE,CACZ,GAAIE,YAAY,GAAKzC,SAAS,CAAE,CAC9B,QACF,CAEA,GAAI,CAACF,OAAO,CAACmC,SAAS,CAAEnC,OAAO,CAACmC,SAAS,CAAG,EAAE,CAE9CnC,OAAO,CAACmC,SAAS,CAACG,IAAI,CAAC,CAAER,MAAM,CAAEO,YAAY,CAAEH,MAAM,CAAE,SAAU,CAAC,CAAC,CACnE,QACF,CAEA,KAAM,IAAI,CAAAf,KAAK,CACb,OAAO,GAAA4B,gCAAqB,EAAC1C,KAAK,CAAC,2BAA2BgB,gBAAgB,CAACc,SAAS,CAAC9B,KAAK,CAAC,CAACR,IAAI,GAAG,CACvG,CAAEuB,KAAK,CAAE,iBAAkB,CAC7B,CACF,CACF,CACF,CAEA,GAAIC,gBAAgB,CAACkB,eAAe,EAAI,CAACvC,OAAO,CAACwC,UAAU,CAAE,CAC3DxC,OAAO,CAACwC,UAAU,CAAG,EACvB,CAEA,MAAO,CAAAxC,OACT","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sourceRoot":"../../../../src/parser/validate","sources":["validate-type.ts"],"sourcesContent":["import type { Argument, Option, Prettify, Schema, Subcommand, ToOptional, ZodInferOutput } from \"../../types.js\";\nimport type { ParseResult } from \"../parse/parse-types.js\";\n\ntype OptionsArray2RecordType<T extends Option[] | undefined> = T extends Option[]\n ? ToOptional<{ [K in T[number][\"name\"]]: ZodInferOutput<Extract<T[number], { name: K }>[\"type\"]> }>\n : object;\n\ntype ArgumentsArray2ArrayType<T extends Argument[] | undefined> = T extends Argument[]\n ? { [K in keyof T]: T[K] extends { type: Schema } ? ZodInferOutput<T[K][\"type\"]> : never }\n : never;\n\nexport type ValidateResult<S extends Partial<Subcommand>[]> = {\n [K in keyof S]: Prettify
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../../../src/parser/validate","sources":["validate-type.ts"],"sourcesContent":["import type { Argument, Option, Prettify, Schema, Subcommand, ToOptional, ZodInferOutput } from \"../../types.js\";\nimport type { ParseResult } from \"../parse/parse-types.js\";\n\ntype OptionsArray2RecordType<T extends Option[] | undefined> = T extends Option[]\n ? ToOptional<{ [K in T[number][\"name\"]]: ZodInferOutput<Extract<T[number], { name: K }>[\"type\"]> }>\n : object;\n\ntype ArgumentsArray2ArrayType<T extends Argument[] | undefined> = T extends Argument[]\n ? { [K in keyof T]: T[K] extends { type: Schema } ? ZodInferOutput<T[K][\"type\"]> : never }\n : never;\n\nexport type ValidateResult<S extends Partial<Subcommand>[]> = {\n [K in keyof S]: Prettify<{\n subcommand: S[K][\"name\"] extends string ? S[K][\"name\"] : undefined;\n arguments: ArgumentsArray2ArrayType<S[K][\"arguments\"]>;\n positional: S[K][\"allowPositional\"] extends true ? string[] : never;\n options: S[K][\"options\"] extends Option[] ? OptionsArray2RecordType<S[K][\"options\"]> : never;\n ctx: ParseResult<S>;\n }>;\n}[number];\n"],"mappings":"","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.validate=validate;var _core=require("zod/v4/core");var _utilities=require("../../utilities.cjs");var _zodUtilities=require("../../zod-utilities.cjs");function validate(parsedData){const results={subcommand:parsedData.subcommand,positional:parsedData.positional,ctx:parsedData};for(const[optionName,{schema,rawValue,flag}]of Object.entries(parsedData.options)){let optionsValue=rawValue;if(flag&&rawValue&&(0,_zodUtilities.isBooleanSchema)(schema)){const booleanValue=(0,_utilities.stringToBoolean)(rawValue);if(typeof booleanValue==="boolean"){const isNegated=flag.startsWith("--no");optionsValue=isNegated?!booleanValue:booleanValue}}const safeParseResult=(0,_zodUtilities.safeParseSchema)(schema,optionsValue);if(!safeParseResult.success){throw new Error(`Invalid value "${rawValue}" for "${flag}": ${(0,_core.prettifyError)(safeParseResult.error)}`,{cause:"zod-args-parser"})}results[optionName]=safeParseResult.data}if(parsedData.arguments){if(!results.arguments)results.arguments=[];for(const{schema,rawValue}of parsedData.arguments){const argumentValue=rawValue&&(0,_zodUtilities.isBooleanSchema)(schema)?(0,_utilities.stringToBoolean)(rawValue):rawValue;const safeParseResult=(0,_zodUtilities.safeParseSchema)(schema,argumentValue);if(!safeParseResult.success){throw new Error(`The ${(0,_utilities.generateOrdinalSuffix)(results.arguments.length)} argument "${rawValue}" is invalid: ${(0,_core.prettifyError)(safeParseResult.error)}`,{cause:"zod-args-parser"})}results.arguments.push(safeParseResult.data)}}return results}
|
|
1
|
+
"use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.validate=validate;var _core=require("zod/v4/core");var _utilities=require("../../utilities.cjs");var _zodUtilities=require("../../zod-utilities.cjs");function validate(parsedData){const results={subcommand:parsedData.subcommand,positional:parsedData.positional,ctx:parsedData};if(parsedData.options){if(!results.options)results.options={};for(const[optionName,{schema,rawValue,flag}]of Object.entries(parsedData.options)){let optionsValue=rawValue;if(flag&&rawValue&&(0,_zodUtilities.isBooleanSchema)(schema)){const booleanValue=(0,_utilities.stringToBoolean)(rawValue);if(typeof booleanValue==="boolean"){const isNegated=flag.startsWith("--no");optionsValue=isNegated?!booleanValue:booleanValue}}const safeParseResult=(0,_zodUtilities.safeParseSchema)(schema,optionsValue);if(!safeParseResult.success){throw new Error(`Invalid value "${rawValue}" for "${flag}": ${(0,_core.prettifyError)(safeParseResult.error)}`,{cause:"zod-args-parser"})}results.options[optionName]=safeParseResult.data}}if(parsedData.arguments){if(!results.arguments)results.arguments=[];for(const{schema,rawValue}of parsedData.arguments){const argumentValue=rawValue&&(0,_zodUtilities.isBooleanSchema)(schema)?(0,_utilities.stringToBoolean)(rawValue):rawValue;const safeParseResult=(0,_zodUtilities.safeParseSchema)(schema,argumentValue);if(!safeParseResult.success){throw new Error(`The ${(0,_utilities.generateOrdinalSuffix)(results.arguments.length)} argument "${rawValue}" is invalid: ${(0,_core.prettifyError)(safeParseResult.error)}`,{cause:"zod-args-parser"})}results.arguments.push(safeParseResult.data)}}return results}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["_core","require","_utilities","_zodUtilities","validate","parsedData","results","subcommand","positional","ctx","optionName","schema","rawValue","flag","Object","entries","
|
|
1
|
+
{"version":3,"names":["_core","require","_utilities","_zodUtilities","validate","parsedData","results","subcommand","positional","ctx","options","optionName","schema","rawValue","flag","Object","entries","optionsValue","isBooleanSchema","booleanValue","stringToBoolean","isNegated","startsWith","safeParseResult","safeParseSchema","success","Error","prettifyError","error","cause","data","arguments","argumentValue","generateOrdinalSuffix","length","push"],"sourceRoot":"../../../../src/parser/validate","sources":["validate.ts"],"sourcesContent":["import { prettifyError } from \"zod/v4/core\";\n\nimport { generateOrdinalSuffix, stringToBoolean } from \"../../utilities.js\";\nimport { isBooleanSchema, safeParseSchema } from \"../../zod-utilities.js\";\n\nimport type { ParsedContext } from \"../parse/parse-types.js\";\n\n/** The return result object temporarily type. used inside the `parse` function */\ntype ResultsTemporaryType = Record<string, unknown> & {\n subcommand: string | undefined;\n positional?: string[];\n arguments?: unknown[];\n options?: Record<string, unknown>;\n ctx: ParsedContext;\n};\n\nexport function validate(parsedData: ParsedContext) {\n const results: ResultsTemporaryType = {\n subcommand: parsedData.subcommand,\n positional: parsedData.positional,\n ctx: parsedData,\n };\n\n // validate options\n if (parsedData.options) {\n if (!results.options) results.options = {};\n\n for (const [optionName, { schema, rawValue, flag }] of Object.entries(parsedData.options)) {\n let optionsValue: string | boolean | undefined = rawValue;\n\n // infer boolean value if possible\n if (flag && rawValue && isBooleanSchema(schema)) {\n const booleanValue = stringToBoolean(rawValue);\n if (typeof booleanValue === \"boolean\") {\n const isNegated = flag.startsWith(\"--no\");\n optionsValue = isNegated ? !booleanValue : booleanValue;\n }\n }\n\n const safeParseResult = safeParseSchema(schema, optionsValue);\n if (!safeParseResult.success) {\n throw new Error(`Invalid value \"${rawValue}\" for \"${flag}\": ${prettifyError(safeParseResult.error)}`, {\n cause: \"zod-args-parser\",\n });\n }\n\n results.options[optionName] = safeParseResult.data;\n }\n }\n\n // validate arguments\n if (parsedData.arguments) {\n if (!results.arguments) results.arguments = [];\n\n for (const { schema, rawValue } of parsedData.arguments) {\n const argumentValue = rawValue && isBooleanSchema(schema) ? stringToBoolean(rawValue) : rawValue;\n\n const safeParseResult = safeParseSchema(schema, argumentValue);\n if (!safeParseResult.success) {\n throw new Error(\n `The ${generateOrdinalSuffix(results.arguments.length)} argument \"${rawValue}\" is invalid: ${prettifyError(safeParseResult.error)}`,\n { cause: \"zod-args-parser\" },\n );\n }\n\n results.arguments.push(safeParseResult.data);\n }\n }\n\n return results;\n}\n"],"mappings":"gGAAA,IAAAA,KAAA,CAAAC,OAAA,gBAEA,IAAAC,UAAA,CAAAD,OAAA,wBACA,IAAAE,aAAA,CAAAF,OAAA,4BAaO,QAAS,CAAAG,QAAQA,CAACC,UAAyB,CAAE,CAClD,KAAM,CAAAC,OAA6B,CAAG,CACpCC,UAAU,CAAEF,UAAU,CAACE,UAAU,CACjCC,UAAU,CAAEH,UAAU,CAACG,UAAU,CACjCC,GAAG,CAAEJ,UACP,CAAC,CAGD,GAAIA,UAAU,CAACK,OAAO,CAAE,CACtB,GAAI,CAACJ,OAAO,CAACI,OAAO,CAAEJ,OAAO,CAACI,OAAO,CAAG,CAAC,CAAC,CAE1C,IAAK,KAAM,CAACC,UAAU,CAAE,CAAEC,MAAM,CAAEC,QAAQ,CAAEC,IAAK,CAAC,CAAC,EAAI,CAAAC,MAAM,CAACC,OAAO,CAACX,UAAU,CAACK,OAAO,CAAC,CAAE,CACzF,GAAI,CAAAO,YAA0C,CAAGJ,QAAQ,CAGzD,GAAIC,IAAI,EAAID,QAAQ,EAAI,GAAAK,6BAAe,EAACN,MAAM,CAAC,CAAE,CAC/C,KAAM,CAAAO,YAAY,CAAG,GAAAC,0BAAe,EAACP,QAAQ,CAAC,CAC9C,GAAI,M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AEAvB,OAAO,CAACyB,SAAS,CAACI,IAAI,CAACZ,eAAe,CAACO,IAAI,CAC7C,CACF,CAEA,MAAO,CAAAxB,OACT","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{generateOrdinalSuffix}from"../../utilities.mjs";import{isBooleanSchema,isOptionalSchema,schemaDefaultValue}from"../../zod-utilities.mjs";import{decoupleFlags,findOption,findSubcommand,isFlagArgument,isOptionArgument,transformOptionToArgument}from"./parser-helpers.mjs";export function parse(argv,...parameters){const subcommandArray=parameters;const allSubcommands=new Set(subcommandArray.flatMap(c=>[c.name,...(c.aliases||[])]));argv=decoupleFlags(argv);const results={subcommand:undefined
|
|
1
|
+
import{generateOrdinalSuffix}from"../../utilities.mjs";import{isBooleanSchema,isOptionalSchema,schemaDefaultValue}from"../../zod-utilities.mjs";import{decoupleFlags,findOption,findSubcommand,isFlagArgument,isOptionArgument,transformOptionToArgument}from"./parser-helpers.mjs";export function parse(argv,...parameters){const subcommandArray=parameters;const allSubcommands=new Set(subcommandArray.flatMap(c=>[c.name,...(c.aliases||[])]));argv=decoupleFlags(argv);const results={subcommand:undefined};const getSubcommandObject=()=>findSubcommand(results.subcommand,subcommandArray);for(let index=0;index<argv.length;index++){const argument_=argv[index];if(index===0){results.subcommand=allSubcommands.has(argument_)?argument_:undefined;if(results.subcommand)continue}const argumentAndValue=argument_.split("=").filter(Boolean);const argumentWithEquals=argument_.includes("=");const argument=argumentAndValue[0];const argumentValue=argumentAndValue[1];if(isOptionArgument(argument)){if(isFlagArgument(argument)&&argumentWithEquals){throw new Error(`Flag arguments cannot be assigned using "=": "${argument_}"`,{cause:"zod-args-parser"})}const subcommandObject=getSubcommandObject();if(!subcommandObject){throw new Error(`Unknown subcommand: "${results.subcommand}"`,{cause:"zod-args-parser"})}if(!subcommandObject.options){if(!results.subcommand){throw new Error(`Error: options are not allowed here: "${argument}"`,{cause:"zod-args-parser"})}throw new Error(`Error: subcommand "${results.subcommand}" does not allow options: "${argument}"`,{cause:"zod-args-parser"})}const option=findOption(argument,subcommandObject.options);if(!option){throw new Error(`Unknown option: "${argument}"`,{cause:"zod-args-parser"})}if(results.options&&option.name in results.options){throw new Error(`Duplicated option: "${argument}"`,{cause:"zod-args-parser"})}const isTypeBoolean=isBooleanSchema(option.type);const nextArgument=argv[index+1];let optionValue=argumentWithEquals?argumentValue:nextArgument;if(isTypeBoolean&&!argumentWithEquals){optionValue="true"}if(optionValue===undefined){throw new Error(`Expected a value for "${argument}" but got nothing`,{cause:"zod-args-parser"})}if(!argumentWithEquals&&isOptionArgument(optionValue)){throw new Error(`Expected a value for "${argument}" but got an argument "${nextArgument}"`,{cause:"zod-args-parser"})}if(!results.options){results.options={}}results.options[option.name]={name:option.name,schema:option.type,flag:argument,rawValue:optionValue.toString(),source:"cli"};if(!argumentWithEquals&&!isTypeBoolean){index++}continue}const subcommandObject=getSubcommandObject();if(subcommandObject?.arguments){if(!results.arguments){results.arguments=[]}const currentArgumentCount=results.arguments.length;if(currentArgumentCount<subcommandObject.arguments.length){const argumentType=subcommandObject.arguments[currentArgumentCount].type;results.arguments.push({schema:argumentType,rawValue:argument_,source:"cli"});continue}}if(subcommandObject?.allowPositional){if(!results.positional){results.positional=[]}results.positional.push(argument_);continue}if(!results.subcommand){throw new Error(`Unexpected argument "${argument_}": positional arguments are not allowed here`,{cause:"zod-args-parser"})}throw new Error(`Unexpected argument "${argument_}": positional arguments are not allowed for subcommand "${results.subcommand}"`,{cause:"zod-args-parser"})}const subcommandObject=getSubcommandObject();if(!subcommandObject){throw new Error(`Unknown subcommand: "${results.subcommand}"`,{cause:"zod-args-parser"})}if(subcommandObject.options){if(!results.options){results.options={}}for(const option of subcommandObject.options){if(results.options&&option.name in results.options)continue;const optional=isOptionalSchema(option.type);const defaultValue=schemaDefaultValue(option.type);if(optional){if(defaultValue===undefined){continue}results.options[option.name]={name:option.name,schema:option.type,source:"default"};continue}throw new Error(`Missing required option: ${transformOptionToArgument(option.name)}`,{cause:"zod-args-parser"})}}if(subcommandObject.arguments){if(!results.arguments){results.arguments=[]}const currentArgumentCount=results.arguments.length??0;const subcommandArgumentCount=subcommandObject.arguments.length;if(currentArgumentCount<subcommandArgumentCount){for(let index=currentArgumentCount;index<subcommandArgumentCount;index++){const argumentType=subcommandObject.arguments[index].type;const optional=isOptionalSchema(argumentType);const defaultValue=schemaDefaultValue(argumentType);if(optional){if(defaultValue===undefined){continue}if(!results.arguments)results.arguments=[];results.arguments.push({schema:argumentType,source:"default"});continue}throw new Error(`the ${generateOrdinalSuffix(index)} argument is required: "${subcommandObject.arguments[index].name}"`,{cause:"zod-args-parser"})}}}if(subcommandObject.allowPositional&&!results.positional){results.positional=[]}return results}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["generateOrdinalSuffix","isBooleanSchema","isOptionalSchema","schemaDefaultValue","decoupleFlags","findOption","findSubcommand","isFlagArgument","isOptionArgument","transformOptionToArgument","parse","argv","parameters","subcommandArray","allSubcommands","Set","flatMap","c","name","aliases","results","subcommand","undefined","options","getSubcommandObject","index","length","argument_","has","subcommandObject","allowPositional","positional","arguments","argumentAndValue","split","filter","Boolean","argumentWithEquals","includes","argument","argumentValue","Error","cause","option","isTypeBoolean","type","nextArgument","optionValue","schema","flag","rawValue","toString","source","currentArgumentCount","argumentType","push","optional","defaultValue","subcommandArgumentCount"],"sourceRoot":"../../../../src/parser/parse","sources":["parse.ts"],"sourcesContent":["import { generateOrdinalSuffix } from \"../../utilities.js\";\nimport { isBooleanSchema, isOptionalSchema, schemaDefaultValue } from \"../../zod-utilities.js\";\nimport {\n decoupleFlags,\n findOption,\n findSubcommand,\n isFlagArgument,\n isOptionArgument,\n transformOptionToArgument,\n} from \"./parser-helpers.js\";\n\nimport type { Cli, Subcommand } from \"../../types.js\";\nimport type { ParsedContext } from \"./parse-types.js\";\n\nexport function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {\n const subcommandArray = parameters as Subcommand[];\n const allSubcommands = new Set<string>(subcommandArray.flatMap(c => [c.name, ...(c.aliases || [])]));\n\n argv = decoupleFlags(argv); // decouple flags E.g. `-rf` -> `-r, -f`\n\n const results: ParsedContext = {\n subcommand: undefined,\n options: {},\n };\n\n /** - Get current subcommand object */\n const getSubcommandObject = () => findSubcommand(results.subcommand, subcommandArray);\n\n for (let index = 0; index < argv.length; index++) {\n const argument_ = argv[index];\n\n // * Subcommand check\n if (index === 0) {\n results.subcommand = allSubcommands.has(argument_) ? argument_ : undefined;\n\n // add positional and arguments arrays\n const subcommandObject = getSubcommandObject();\n if (subcommandObject && subcommandObject.allowPositional) {\n results.positional = [];\n }\n\n if (subcommandObject && subcommandObject.arguments?.length) {\n results.arguments = [];\n }\n\n // First argument is a subcommand. Skip to the next argument\n if (results.subcommand) continue;\n }\n\n // * Option check\n\n // Check for `--option=value` or `--option value`\n const argumentAndValue = argument_.split(\"=\").filter(Boolean);\n const argumentWithEquals = argument_.includes(\"=\");\n const argument = argumentAndValue[0];\n const argumentValue: string | undefined = argumentAndValue[1];\n\n if (isOptionArgument(argument)) {\n if (isFlagArgument(argument) && argumentWithEquals) {\n throw new Error(`Flag arguments cannot be assigned using \"=\": \"${argument_}\"`, { cause: \"zod-args-parser\" });\n }\n\n const subcommandObject = getSubcommandObject();\n if (!subcommandObject) {\n throw new Error(`Unknown subcommand: \"${results.subcommand}\"`, { cause: \"zod-args-parser\" });\n }\n\n if (!subcommandObject.options) {\n if (!results.subcommand) {\n throw new Error(`Error: options are not allowed here: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n throw new Error(`Error: subcommand \"${results.subcommand}\" does not allow options: \"${argument}\"`, {\n cause: \"zod-args-parser\",\n });\n }\n\n const option = findOption(argument, subcommandObject.options);\n if (!option) {\n throw new Error(`Unknown option: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n if (option.name in results.options) {\n throw new Error(`Duplicated option: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n const isTypeBoolean = isBooleanSchema(option.type);\n const nextArgument = argv[index + 1];\n\n let optionValue: string | boolean = argumentWithEquals ? argumentValue : nextArgument;\n\n // infer value for boolean options\n if (isTypeBoolean && !argumentWithEquals) {\n optionValue = \"true\";\n }\n\n if (optionValue === undefined) {\n throw new Error(`Expected a value for \"${argument}\" but got nothing`, { cause: \"zod-args-parser\" });\n }\n\n if (!argumentWithEquals && isOptionArgument(optionValue)) {\n throw new Error(`Expected a value for \"${argument}\" but got an argument \"${nextArgument}\"`, {\n cause: \"zod-args-parser\",\n });\n }\n\n results.options[option.name] = {\n name: option.name,\n schema: option.type,\n flag: argument,\n rawValue: optionValue.toString(),\n source: \"cli\",\n };\n\n // Skip to the next argument if it is the current option’s value.\n if (!argumentWithEquals && !isTypeBoolean) {\n index++;\n }\n\n continue;\n }\n\n const subcommandObject = getSubcommandObject();\n\n // * Arguments check\n if (subcommandObject?.arguments?.length) {\n if (!results.arguments) {\n results.arguments = [];\n }\n\n const currentArgumentCount = results.arguments.length;\n\n // Any extra arguments are possibly positional\n if (currentArgumentCount < subcommandObject.arguments.length) {\n const argumentType = subcommandObject.arguments[currentArgumentCount].type;\n results.arguments.push({\n schema: argumentType,\n rawValue: argument_,\n source: \"cli\",\n });\n continue;\n }\n }\n\n // * Positional check\n if (subcommandObject?.allowPositional) {\n if (!results.positional) {\n results.positional = [];\n }\n\n results.positional.push(argument_);\n continue;\n }\n\n // * Unexpected\n if (!results.subcommand) {\n throw new Error(`Unexpected argument \"${argument_}\": positional arguments are not allowed here`, {\n cause: \"zod-args-parser\",\n });\n }\n\n throw new Error(\n `Unexpected argument \"${argument_}\": positional arguments are not allowed for subcommand \"${results.subcommand}\"`,\n { cause: \"zod-args-parser\" },\n );\n }\n\n // * Check for missing options - set defaults - add `source`\n const subcommandObject = getSubcommandObject();\n if (!subcommandObject) {\n throw new Error(`Unknown subcommand: \"${results.subcommand}\"`, { cause: \"zod-args-parser\" });\n }\n\n // Options\n if (subcommandObject.options?.length) {\n for (const option of subcommandObject.options) {\n // option already exists\n if (option.name in results.options) continue;\n\n const optional = isOptionalSchema(option.type);\n const defaultValue = schemaDefaultValue(option.type);\n\n if (optional) {\n if (defaultValue === undefined) {\n continue;\n }\n\n results.options[option.name] = { name: option.name, schema: option.type, source: \"default\" };\n continue;\n }\n\n throw new Error(`Missing required option: ${transformOptionToArgument(option.name)}`, {\n cause: \"zod-args-parser\",\n });\n }\n }\n\n // Arguments\n if (subcommandObject.arguments?.length) {\n const currentArgumentCount = results.arguments?.length ?? 0;\n const subcommandArgumentCount = subcommandObject.arguments.length;\n\n // missing arguments\n if (currentArgumentCount < subcommandArgumentCount) {\n for (let index = currentArgumentCount; index < subcommandArgumentCount; index++) {\n const argumentType = subcommandObject.arguments[index].type;\n const optional = isOptionalSchema(argumentType);\n const defaultValue = schemaDefaultValue(argumentType);\n\n if (optional) {\n if (defaultValue === undefined) {\n continue;\n }\n\n if (!results.arguments) results.arguments = [];\n\n results.arguments.push({ schema: argumentType, source: \"default\" });\n continue;\n }\n\n throw new Error(\n `the ${generateOrdinalSuffix(index)} argument is required: \"${subcommandObject.arguments[index].name}\"`,\n { cause: \"zod-args-parser\" },\n );\n }\n }\n }\n\n return results;\n}\n"],"mappings":"AAAA,OAASA,qBAAqB,KAAQ,qBAAoB,CAC1D,OAASC,eAAe,CAAEC,gBAAgB,CAAEC,kBAAkB,KAAQ,yBAAwB,CAC9F,OACEC,aAAa,CACbC,UAAU,CACVC,cAAc,CACdC,cAAc,CACdC,gBAAgB,CAChBC,yBAAyB,KACpB,sBAAqB,CAK5B,MAAO,SAAS,CAAAC,KAAKA,CAACC,IAAc,CAAE,GAAGC,UAAkC,CAAE,CAC3E,KAAM,CAAAC,eAAe,CAAGD,UAA0B,CAClD,KAAM,CAAAE,cAAc,CAAG,GAAI,CAAAC,GAAG,CAASF,eAAe,CAACG,OAAO,CAACC,CAAC,EAAI,CAACA,CAAC,CAACC,IAAI,CAAE,IAAID,CAAC,CAACE,OAAO,EAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CA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,qBAAqB,CAACyB,KAAK,CAAC,2BAA2BI,gBAAgB,CAACG,SAAS,CAACP,KAAK,CAAC,CAACP,IAAI,GAAG,CACvG,CAAEwB,KAAK,CAAE,iBAAkB,CAC7B,CACF,CACF,CACF,CAEA,MAAO,CAAAtB,OACT","ignoreList":[]}
|
|
1
|
+
{"version":3,"names":["generateOrdinalSuffix","isBooleanSchema","isOptionalSchema","schemaDefaultValue","decoupleFlags","findOption","findSubcommand","isFlagArgument","isOptionArgument","transformOptionToArgument","parse","argv","parameters","subcommandArray","allSubcommands","Set","flatMap","c","name","aliases","results","subcommand","undefined","getSubcommandObject","index","length","argument_","has","argumentAndValue","split","filter","Boolean","argumentWithEquals","includes","argument","argumentValue","Error","cause","subcommandObject","options","option","isTypeBoolean","type","nextArgument","optionValue","schema","flag","rawValue","toString","source","arguments","currentArgumentCount","argumentType","push","allowPositional","positional","optional","defaultValue","subcommandArgumentCount"],"sourceRoot":"../../../../src/parser/parse","sources":["parse.ts"],"sourcesContent":["import { generateOrdinalSuffix } from \"../../utilities.js\";\nimport { isBooleanSchema, isOptionalSchema, schemaDefaultValue } from \"../../zod-utilities.js\";\nimport {\n decoupleFlags,\n findOption,\n findSubcommand,\n isFlagArgument,\n isOptionArgument,\n transformOptionToArgument,\n} from \"./parser-helpers.js\";\n\nimport type { Cli, Subcommand } from \"../../types.js\";\nimport type { ParsedContext } from \"./parse-types.js\";\n\nexport function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {\n const subcommandArray = parameters as Subcommand[];\n const allSubcommands = new Set<string>(subcommandArray.flatMap(c => [c.name, ...(c.aliases || [])]));\n\n argv = decoupleFlags(argv); // decouple flags E.g. `-rf` -> `-r, -f`\n\n const results: ParsedContext = {\n subcommand: undefined,\n };\n\n /** - Get current subcommand object */\n const getSubcommandObject = () => findSubcommand(results.subcommand, subcommandArray);\n\n for (let index = 0; index < argv.length; index++) {\n const argument_ = argv[index];\n\n // * Subcommand check\n if (index === 0) {\n results.subcommand = allSubcommands.has(argument_) ? argument_ : undefined;\n\n // First argument is a subcommand. Skip to the next argument\n if (results.subcommand) continue;\n }\n\n // * Option check\n\n // Check for `--option=value` or `--option value`\n const argumentAndValue = argument_.split(\"=\").filter(Boolean);\n const argumentWithEquals = argument_.includes(\"=\");\n const argument = argumentAndValue[0];\n const argumentValue: string | undefined = argumentAndValue[1];\n\n if (isOptionArgument(argument)) {\n if (isFlagArgument(argument) && argumentWithEquals) {\n throw new Error(`Flag arguments cannot be assigned using \"=\": \"${argument_}\"`, { cause: \"zod-args-parser\" });\n }\n\n const subcommandObject = getSubcommandObject();\n if (!subcommandObject) {\n throw new Error(`Unknown subcommand: \"${results.subcommand}\"`, { cause: \"zod-args-parser\" });\n }\n\n if (!subcommandObject.options) {\n if (!results.subcommand) {\n throw new Error(`Error: options are not allowed here: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n throw new Error(`Error: subcommand \"${results.subcommand}\" does not allow options: \"${argument}\"`, {\n cause: \"zod-args-parser\",\n });\n }\n\n const option = findOption(argument, subcommandObject.options);\n if (!option) {\n throw new Error(`Unknown option: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n if (results.options && option.name in results.options) {\n throw new Error(`Duplicated option: \"${argument}\"`, { cause: \"zod-args-parser\" });\n }\n\n const isTypeBoolean = isBooleanSchema(option.type);\n const nextArgument = argv[index + 1];\n\n let optionValue: string | boolean = argumentWithEquals ? argumentValue : nextArgument;\n\n // infer value for boolean options\n if (isTypeBoolean && !argumentWithEquals) {\n optionValue = \"true\";\n }\n\n if (optionValue === undefined) {\n throw new Error(`Expected a value for \"${argument}\" but got nothing`, { cause: \"zod-args-parser\" });\n }\n\n if (!argumentWithEquals && isOptionArgument(optionValue)) {\n throw new Error(`Expected a value for \"${argument}\" but got an argument \"${nextArgument}\"`, {\n cause: \"zod-args-parser\",\n });\n }\n\n if (!results.options) {\n results.options = {};\n }\n\n results.options[option.name] = {\n name: option.name,\n schema: option.type,\n flag: argument,\n rawValue: optionValue.toString(),\n source: \"cli\",\n };\n\n // Skip to the next argument if it is the current option’s value.\n if (!argumentWithEquals && !isTypeBoolean) {\n index++;\n }\n\n continue;\n }\n\n const subcommandObject = getSubcommandObject();\n\n // * Arguments check\n if (subcommandObject?.arguments) {\n if (!results.arguments) {\n results.arguments = [];\n }\n\n const currentArgumentCount = results.arguments.length;\n\n // Any extra arguments are possibly positional\n if (currentArgumentCount < subcommandObject.arguments.length) {\n const argumentType = subcommandObject.arguments[currentArgumentCount].type;\n results.arguments.push({\n schema: argumentType,\n rawValue: argument_,\n source: \"cli\",\n });\n continue;\n }\n }\n\n // * Positional check\n if (subcommandObject?.allowPositional) {\n if (!results.positional) {\n results.positional = [];\n }\n\n results.positional.push(argument_);\n continue;\n }\n\n // * Unexpected\n if (!results.subcommand) {\n throw new Error(`Unexpected argument \"${argument_}\": positional arguments are not allowed here`, {\n cause: \"zod-args-parser\",\n });\n }\n\n throw new Error(\n `Unexpected argument \"${argument_}\": positional arguments are not allowed for subcommand \"${results.subcommand}\"`,\n { cause: \"zod-args-parser\" },\n );\n }\n\n // * Check for missing options - set defaults - add `source`\n const subcommandObject = getSubcommandObject();\n if (!subcommandObject) {\n throw new Error(`Unknown subcommand: \"${results.subcommand}\"`, { cause: \"zod-args-parser\" });\n }\n\n // Options\n if (subcommandObject.options) {\n if (!results.options) {\n results.options = {};\n }\n\n for (const option of subcommandObject.options) {\n // option already exists\n if (results.options && option.name in results.options) continue;\n\n const optional = isOptionalSchema(option.type);\n const defaultValue = schemaDefaultValue(option.type);\n\n if (optional) {\n if (defaultValue === undefined) {\n continue;\n }\n\n results.options[option.name] = { name: option.name, schema: option.type, source: \"default\" };\n continue;\n }\n\n throw new Error(`Missing required option: ${transformOptionToArgument(option.name)}`, {\n cause: \"zod-args-parser\",\n });\n }\n }\n\n // Arguments\n if (subcommandObject.arguments) {\n if (!results.arguments) {\n results.arguments = [];\n }\n\n const currentArgumentCount = results.arguments.length ?? 0;\n const subcommandArgumentCount = subcommandObject.arguments.length;\n\n // missing arguments\n if (currentArgumentCount < subcommandArgumentCount) {\n for (let index = currentArgumentCount; index < subcommandArgumentCount; index++) {\n const argumentType = subcommandObject.arguments[index].type;\n const optional = isOptionalSchema(argumentType);\n const defaultValue = schemaDefaultValue(argumentType);\n\n if (optional) {\n if (defaultValue === undefined) {\n continue;\n }\n\n if (!results.arguments) results.arguments = [];\n\n results.arguments.push({ schema: argumentType, source: \"default\" });\n continue;\n }\n\n throw new Error(\n `the ${generateOrdinalSuffix(index)} argument is required: \"${subcommandObject.arguments[index].name}\"`,\n { cause: \"zod-args-parser\" },\n );\n }\n }\n }\n\n if (subcommandObject.allowPositional && !results.positional) {\n results.positional = [];\n }\n\n return results;\n}\n"],"mappings":"AAAA,OAASA,qBAAqB,KAAQ,qBAAoB,CAC1D,OAASC,eAAe,CAAEC,gBAAgB,CAAEC,kBAAkB,KAAQ,yBAAwB,CAC9F,OACEC,aAAa,CACbC,UAAU,CACVC,cAAc,CACdC,cAAc,CACdC,gBAAgB,CAChBC,yBAAyB,KACpB,sBAAqB,CAK5B,MAAO,SAAS,CAAAC,KAAKA,CAACC,IAAc,CAAE,GAAGC,UAAkC,CAAE,CAC3E,KAAM,CAAAC,eAAe,CAAGD,UAA0B,CAClD,KAAM,CAAAE,cAAc,CAAG,GAAI,CAAAC,GAAG,CAASF,eAAe,CAACG,OAAO,CAACC,CAAC,EAAI,CAACA,CAAC,CAACC,IAAI,CAAE,IAAID,CAAC,CAACE,OAAO,EAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAEpGR,IAAI,CAAGP,aAAa,CAACO,IAAI,CAAC,CAE1B,KAAM,CAAAS,OAAsB,CAAG,CAC7BC,UAAU,CAAEC,SACd,CAAC,CAGD,KAAM,CAAAC,mBAAmB,CAAGA,CAAA,GAAMjB,cAAc,CAACc,OAAO,CAACC,UAAU,CAAER,eAAe,CAAC,CAErF,IAAK,GAAI,CAAAW,KAAK,CAAG,CAAC,CAAEA,KAAK,CAAGb,IA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,CAAG,EACvB,CAEA,MAAO,CAAAnC,OACT","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":[],"sourceRoot":"../../../../src/parser/validate","sources":["validate-type.ts"],"sourcesContent":["import type { Argument, Option, Prettify, Schema, Subcommand, ToOptional, ZodInferOutput } from \"../../types.js\";\nimport type { ParseResult } from \"../parse/parse-types.js\";\n\ntype OptionsArray2RecordType<T extends Option[] | undefined> = T extends Option[]\n ? ToOptional<{ [K in T[number][\"name\"]]: ZodInferOutput<Extract<T[number], { name: K }>[\"type\"]> }>\n : object;\n\ntype ArgumentsArray2ArrayType<T extends Argument[] | undefined> = T extends Argument[]\n ? { [K in keyof T]: T[K] extends { type: Schema } ? ZodInferOutput<T[K][\"type\"]> : never }\n : never;\n\nexport type ValidateResult<S extends Partial<Subcommand>[]> = {\n [K in keyof S]: Prettify
|
|
1
|
+
{"version":3,"names":[],"sourceRoot":"../../../../src/parser/validate","sources":["validate-type.ts"],"sourcesContent":["import type { Argument, Option, Prettify, Schema, Subcommand, ToOptional, ZodInferOutput } from \"../../types.js\";\nimport type { ParseResult } from \"../parse/parse-types.js\";\n\ntype OptionsArray2RecordType<T extends Option[] | undefined> = T extends Option[]\n ? ToOptional<{ [K in T[number][\"name\"]]: ZodInferOutput<Extract<T[number], { name: K }>[\"type\"]> }>\n : object;\n\ntype ArgumentsArray2ArrayType<T extends Argument[] | undefined> = T extends Argument[]\n ? { [K in keyof T]: T[K] extends { type: Schema } ? ZodInferOutput<T[K][\"type\"]> : never }\n : never;\n\nexport type ValidateResult<S extends Partial<Subcommand>[]> = {\n [K in keyof S]: Prettify<{\n subcommand: S[K][\"name\"] extends string ? S[K][\"name\"] : undefined;\n arguments: ArgumentsArray2ArrayType<S[K][\"arguments\"]>;\n positional: S[K][\"allowPositional\"] extends true ? string[] : never;\n options: S[K][\"options\"] extends Option[] ? OptionsArray2RecordType<S[K][\"options\"]> : never;\n ctx: ParseResult<S>;\n }>;\n}[number];\n"],"mappings":"","ignoreList":[]}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
import{prettifyError}from"zod/v4/core";import{generateOrdinalSuffix,stringToBoolean}from"../../utilities.mjs";import{isBooleanSchema,safeParseSchema}from"../../zod-utilities.mjs";export function validate(parsedData){const results={subcommand:parsedData.subcommand,positional:parsedData.positional,ctx:parsedData};for(const[optionName,{schema,rawValue,flag}]of Object.entries(parsedData.options)){let optionsValue=rawValue;if(flag&&rawValue&&isBooleanSchema(schema)){const booleanValue=stringToBoolean(rawValue);if(typeof booleanValue==="boolean"){const isNegated=flag.startsWith("--no");optionsValue=isNegated?!booleanValue:booleanValue}}const safeParseResult=safeParseSchema(schema,optionsValue);if(!safeParseResult.success){throw new Error(`Invalid value "${rawValue}" for "${flag}": ${prettifyError(safeParseResult.error)}`,{cause:"zod-args-parser"})}results[optionName]=safeParseResult.data}if(parsedData.arguments){if(!results.arguments)results.arguments=[];for(const{schema,rawValue}of parsedData.arguments){const argumentValue=rawValue&&isBooleanSchema(schema)?stringToBoolean(rawValue):rawValue;const safeParseResult=safeParseSchema(schema,argumentValue);if(!safeParseResult.success){throw new Error(`The ${generateOrdinalSuffix(results.arguments.length)} argument "${rawValue}" is invalid: ${prettifyError(safeParseResult.error)}`,{cause:"zod-args-parser"})}results.arguments.push(safeParseResult.data)}}return results}
|
|
1
|
+
import{prettifyError}from"zod/v4/core";import{generateOrdinalSuffix,stringToBoolean}from"../../utilities.mjs";import{isBooleanSchema,safeParseSchema}from"../../zod-utilities.mjs";export function validate(parsedData){const results={subcommand:parsedData.subcommand,positional:parsedData.positional,ctx:parsedData};if(parsedData.options){if(!results.options)results.options={};for(const[optionName,{schema,rawValue,flag}]of Object.entries(parsedData.options)){let optionsValue=rawValue;if(flag&&rawValue&&isBooleanSchema(schema)){const booleanValue=stringToBoolean(rawValue);if(typeof booleanValue==="boolean"){const isNegated=flag.startsWith("--no");optionsValue=isNegated?!booleanValue:booleanValue}}const safeParseResult=safeParseSchema(schema,optionsValue);if(!safeParseResult.success){throw new Error(`Invalid value "${rawValue}" for "${flag}": ${prettifyError(safeParseResult.error)}`,{cause:"zod-args-parser"})}results.options[optionName]=safeParseResult.data}}if(parsedData.arguments){if(!results.arguments)results.arguments=[];for(const{schema,rawValue}of parsedData.arguments){const argumentValue=rawValue&&isBooleanSchema(schema)?stringToBoolean(rawValue):rawValue;const safeParseResult=safeParseSchema(schema,argumentValue);if(!safeParseResult.success){throw new Error(`The ${generateOrdinalSuffix(results.arguments.length)} argument "${rawValue}" is invalid: ${prettifyError(safeParseResult.error)}`,{cause:"zod-args-parser"})}results.arguments.push(safeParseResult.data)}}return results}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"names":["prettifyError","generateOrdinalSuffix","stringToBoolean","isBooleanSchema","safeParseSchema","validate","parsedData","results","subcommand","positional","ctx","optionName","schema","rawValue","flag","Object","entries","
|
|
1
|
+
{"version":3,"names":["prettifyError","generateOrdinalSuffix","stringToBoolean","isBooleanSchema","safeParseSchema","validate","parsedData","results","subcommand","positional","ctx","options","optionName","schema","rawValue","flag","Object","entries","optionsValue","booleanValue","isNegated","startsWith","safeParseResult","success","Error","error","cause","data","arguments","argumentValue","length","push"],"sourceRoot":"../../../../src/parser/validate","sources":["validate.ts"],"sourcesContent":["import { prettifyError } from \"zod/v4/core\";\n\nimport { generateOrdinalSuffix, stringToBoolean } from \"../../utilities.js\";\nimport { isBooleanSchema, safeParseSchema } from \"../../zod-utilities.js\";\n\nimport type { ParsedContext } from \"../parse/parse-types.js\";\n\n/** The return result object temporarily type. used inside the `parse` function */\ntype ResultsTemporaryType = Record<string, unknown> & {\n subcommand: string | undefined;\n positional?: string[];\n arguments?: unknown[];\n options?: Record<string, unknown>;\n ctx: ParsedContext;\n};\n\nexport function validate(parsedData: ParsedContext) {\n const results: ResultsTemporaryType = {\n subcommand: parsedData.subcommand,\n positional: parsedData.positional,\n ctx: parsedData,\n };\n\n // validate options\n if (parsedData.options) {\n if (!results.options) results.options = {};\n\n for (const [optionName, { schema, rawValue, flag }] of Object.entries(parsedData.options)) {\n let optionsValue: string | boolean | undefined = rawValue;\n\n // infer boolean value if possible\n if (flag && rawValue && isBooleanSchema(schema)) {\n const booleanValue = stringToBoolean(rawValue);\n if (typeof booleanValue === \"boolean\") {\n const isNegated = flag.startsWith(\"--no\");\n optionsValue = isNegated ? !booleanValue : booleanValue;\n }\n }\n\n const safeParseResult = safeParseSchema(schema, optionsValue);\n if (!safeParseResult.success) {\n throw new Error(`Invalid value \"${rawValue}\" for \"${flag}\": ${prettifyError(safeParseResult.error)}`, {\n cause: \"zod-args-parser\",\n });\n }\n\n results.options[optionName] = safeParseResult.data;\n }\n }\n\n // validate arguments\n if (parsedData.arguments) {\n if (!results.arguments) results.arguments = [];\n\n for (const { schema, rawValue } of parsedData.arguments) {\n const argumentValue = rawValue && isBooleanSchema(schema) ? stringToBoolean(rawValue) : rawValue;\n\n const safeParseResult = safeParseSchema(schema, argumentValue);\n if (!safeParseResult.success) {\n throw new Error(\n `The ${generateOrdinalSuffix(results.arguments.length)} argument \"${rawValue}\" is invalid: ${prettifyError(safeParseResult.error)}`,\n { cause: \"zod-args-parser\" },\n );\n }\n\n results.arguments.push(safeParseResult.data);\n }\n }\n\n return results;\n}\n"],"mappings":"AAAA,OAASA,aAAa,KAAQ,aAAa,CAE3C,OAASC,qBAAqB,CAAEC,eAAe,KAAQ,qBAAoB,CAC3E,OAASC,eAAe,CAAEC,eAAe,KAAQ,yBAAwB,CAazE,MAAO,SAAS,CAAAC,QAAQA,CAACC,UAAyB,CAAE,CAClD,KAAM,CAAAC,OAA6B,CAAG,CACpCC,UAAU,CAAEF,UAAU,CAACE,UAAU,CACjCC,UAAU,CAAEH,UAAU,CAACG,UAAU,CACjCC,GAAG,CAAEJ,UACP,CAAC,CAGD,GAAIA,UAAU,CAACK,OAAO,CAAE,CACtB,GAAI,CAACJ,OAAO,CAACI,OAAO,CAAEJ,OAAO,CAACI,OAAO,CAAG,CAAC,CAAC,CAE1C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AAM,CAAEgB,aAAa,CAAC,CAC9D,GAAI,CAACP,eAAe,CAACC,OAAO,CAAE,CAC5B,KAAM,IAAI,CAAAC,KAAK,CACb,OAAOvB,qBAAqB,CAACM,OAAO,CAACqB,SAAS,CAACE,MAAM,CAAC,cAAchB,QAAQ,iBAAiBd,aAAa,CAACsB,eAAe,CAACG,KAAK,CAAC,EAAE,CACnI,CAAEC,KAAK,CAAE,iBAAkB,CAC7B,CACF,CAEAnB,OAAO,CAACqB,SAAS,CAACG,IAAI,CAACT,eAAe,CAACK,IAAI,CAC7C,CACF,CAEA,MAAO,CAAApB,OACT","ignoreList":[]}
|
|
@@ -71,13 +71,13 @@ export type ParseResult<S extends Partial<Subcommand>[]> = {
|
|
|
71
71
|
[K in keyof S]: Prettify<{
|
|
72
72
|
subcommand: S[K]["name"] extends string ? S[K]["name"] : undefined;
|
|
73
73
|
positional: S[K]["allowPositional"] extends true ? string[] : never;
|
|
74
|
-
options: OptionsArray2Record<S[K]["options"]
|
|
74
|
+
options: S[K]["options"] extends Option[] ? OptionsArray2Record<S[K]["options"]> : never;
|
|
75
75
|
arguments: ArgumentsArray2ArrayType<S[K]["arguments"]>;
|
|
76
76
|
}>;
|
|
77
77
|
}[number];
|
|
78
78
|
export type ParsedContext = {
|
|
79
79
|
subcommand: string | undefined;
|
|
80
|
-
options
|
|
80
|
+
options?: Record<string, ParsedOption>;
|
|
81
81
|
arguments?: ParsedArgument[];
|
|
82
82
|
positional?: string[];
|
|
83
83
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"parse-types.d.ts","sourceRoot":"","sources":["../../../../src/parser/parse/parse-types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AAErF,KAAK,YAAY,CAAC,CAAC,SAAS,MAAM,GAAG,MAAM,EAAE,CAAC,SAAS,MAAM,GAAG,MAA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AAEtD,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,UAAU,EAAE,CAAC,GAAG,EAAE,GAAG,UAAU,EAAE,CAAC,
|
|
1
|
+
{"version":3,"file":"parse.d.ts","sourceRoot":"","sources":["../../../../src/parser/parse/parse.ts"],"names":[],"mappings":"AAWA,OAAO,KAAK,EAAE,GAAG,EAAE,UAAU,EAAE,MAAM,gBAAgB,CAAC;AACtD,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAEtD,wBAAgB,KAAK,CAAC,IAAI,EAAE,MAAM,EAAE,EAAE,GAAG,UAAU,EAAE,CAAC,GAAG,EAAE,GAAG,UAAU,EAAE,CAAC,iBA4N1E"}
|
|
@@ -15,8 +15,9 @@ export type ValidateResult<S extends Partial<Subcommand>[]> = {
|
|
|
15
15
|
subcommand: S[K]["name"] extends string ? S[K]["name"] : undefined;
|
|
16
16
|
arguments: ArgumentsArray2ArrayType<S[K]["arguments"]>;
|
|
17
17
|
positional: S[K]["allowPositional"] extends true ? string[] : never;
|
|
18
|
+
options: S[K]["options"] extends Option[] ? OptionsArray2RecordType<S[K]["options"]> : never;
|
|
18
19
|
ctx: ParseResult<S>;
|
|
19
|
-
}
|
|
20
|
+
}>;
|
|
20
21
|
}[number];
|
|
21
22
|
export {};
|
|
22
23
|
//# sourceMappingURL=validate-type.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate-type.d.ts","sourceRoot":"","sources":["../../../../src/parser/validate/validate-type.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,QAAQ,EAAE,MAAM,EAAE,QAAQ,EAAE,MAAM,EAAE,UAAU,EAAE,UAAU,EAAE,cAAc,EAAE,MAAM,gBAAgB,CAAC;AACjH,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,yBAAyB,CAAC;AAE3D,KAAK,uBAAuB,CAAC,CAAC,SAAS,MAAM,EAAE,GAAG,SAAS,IAAI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+
options?: Record<string, unknown>;
|
|
7
8
|
ctx: ParsedContext;
|
|
8
9
|
};
|
|
9
10
|
export declare function validate(parsedData: ParsedContext): ResultsTemporaryType;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../../src/parser/validate/validate.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAE7D,kFAAkF;AAClF,KAAK,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IACpD,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC;IACtB,GAAG,EAAE,aAAa,CAAC;CACpB,CAAC;AAEF,wBAAgB,QAAQ,CAAC,UAAU,EAAE,aAAa,
|
|
1
|
+
{"version":3,"file":"validate.d.ts","sourceRoot":"","sources":["../../../../src/parser/validate/validate.ts"],"names":[],"mappings":"AAKA,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,yBAAyB,CAAC;AAE7D,kFAAkF;AAClF,KAAK,oBAAoB,GAAG,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,GAAG;IACpD,UAAU,EAAE,MAAM,GAAG,SAAS,CAAC;IAC/B,UAAU,CAAC,EAAE,MAAM,EAAE,CAAC;IACtB,SAAS,CAAC,EAAE,OAAO,EAAE,CAAC;IACtB,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAC;IAClC,GAAG,EAAE,aAAa,CAAC;CACpB,CAAC;AAEF,wBAAgB,QAAQ,CAAC,UAAU,EAAE,aAAa,wBAsDjD"}
|
package/package.json
CHANGED
|
@@ -76,14 +76,14 @@ export type ParseResult<S extends Partial<Subcommand>[]> = {
|
|
|
76
76
|
[K in keyof S]: Prettify<{
|
|
77
77
|
subcommand: S[K]["name"] extends string ? S[K]["name"] : undefined;
|
|
78
78
|
positional: S[K]["allowPositional"] extends true ? string[] : never;
|
|
79
|
-
options: OptionsArray2Record<S[K]["options"]
|
|
79
|
+
options: S[K]["options"] extends Option[] ? OptionsArray2Record<S[K]["options"]> : never;
|
|
80
80
|
arguments: ArgumentsArray2ArrayType<S[K]["arguments"]>;
|
|
81
81
|
}>;
|
|
82
82
|
}[number];
|
|
83
83
|
|
|
84
84
|
export type ParsedContext = {
|
|
85
85
|
subcommand: string | undefined;
|
|
86
|
-
options
|
|
86
|
+
options?: Record<string, ParsedOption>;
|
|
87
87
|
arguments?: ParsedArgument[];
|
|
88
88
|
positional?: string[];
|
|
89
89
|
};
|
|
@@ -20,7 +20,6 @@ export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
|
20
20
|
|
|
21
21
|
const results: ParsedContext = {
|
|
22
22
|
subcommand: undefined,
|
|
23
|
-
options: {},
|
|
24
23
|
};
|
|
25
24
|
|
|
26
25
|
/** - Get current subcommand object */
|
|
@@ -33,16 +32,6 @@ export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
|
33
32
|
if (index === 0) {
|
|
34
33
|
results.subcommand = allSubcommands.has(argument_) ? argument_ : undefined;
|
|
35
34
|
|
|
36
|
-
// add positional and arguments arrays
|
|
37
|
-
const subcommandObject = getSubcommandObject();
|
|
38
|
-
if (subcommandObject && subcommandObject.allowPositional) {
|
|
39
|
-
results.positional = [];
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
if (subcommandObject && subcommandObject.arguments?.length) {
|
|
43
|
-
results.arguments = [];
|
|
44
|
-
}
|
|
45
|
-
|
|
46
35
|
// First argument is a subcommand. Skip to the next argument
|
|
47
36
|
if (results.subcommand) continue;
|
|
48
37
|
}
|
|
@@ -80,7 +69,7 @@ export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
|
80
69
|
throw new Error(`Unknown option: "${argument}"`, { cause: "zod-args-parser" });
|
|
81
70
|
}
|
|
82
71
|
|
|
83
|
-
if (option.name in results.options) {
|
|
72
|
+
if (results.options && option.name in results.options) {
|
|
84
73
|
throw new Error(`Duplicated option: "${argument}"`, { cause: "zod-args-parser" });
|
|
85
74
|
}
|
|
86
75
|
|
|
@@ -104,6 +93,10 @@ export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
|
104
93
|
});
|
|
105
94
|
}
|
|
106
95
|
|
|
96
|
+
if (!results.options) {
|
|
97
|
+
results.options = {};
|
|
98
|
+
}
|
|
99
|
+
|
|
107
100
|
results.options[option.name] = {
|
|
108
101
|
name: option.name,
|
|
109
102
|
schema: option.type,
|
|
@@ -123,7 +116,7 @@ export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
|
123
116
|
const subcommandObject = getSubcommandObject();
|
|
124
117
|
|
|
125
118
|
// * Arguments check
|
|
126
|
-
if (subcommandObject?.arguments
|
|
119
|
+
if (subcommandObject?.arguments) {
|
|
127
120
|
if (!results.arguments) {
|
|
128
121
|
results.arguments = [];
|
|
129
122
|
}
|
|
@@ -172,10 +165,14 @@ export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
|
172
165
|
}
|
|
173
166
|
|
|
174
167
|
// Options
|
|
175
|
-
if (subcommandObject.options
|
|
168
|
+
if (subcommandObject.options) {
|
|
169
|
+
if (!results.options) {
|
|
170
|
+
results.options = {};
|
|
171
|
+
}
|
|
172
|
+
|
|
176
173
|
for (const option of subcommandObject.options) {
|
|
177
174
|
// option already exists
|
|
178
|
-
if (option.name in results.options) continue;
|
|
175
|
+
if (results.options && option.name in results.options) continue;
|
|
179
176
|
|
|
180
177
|
const optional = isOptionalSchema(option.type);
|
|
181
178
|
const defaultValue = schemaDefaultValue(option.type);
|
|
@@ -196,8 +193,12 @@ export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
|
196
193
|
}
|
|
197
194
|
|
|
198
195
|
// Arguments
|
|
199
|
-
if (subcommandObject.arguments
|
|
200
|
-
|
|
196
|
+
if (subcommandObject.arguments) {
|
|
197
|
+
if (!results.arguments) {
|
|
198
|
+
results.arguments = [];
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
const currentArgumentCount = results.arguments.length ?? 0;
|
|
201
202
|
const subcommandArgumentCount = subcommandObject.arguments.length;
|
|
202
203
|
|
|
203
204
|
// missing arguments
|
|
@@ -226,5 +227,9 @@ export function parse(argv: string[], ...parameters: [Cli, ...Subcommand[]]) {
|
|
|
226
227
|
}
|
|
227
228
|
}
|
|
228
229
|
|
|
230
|
+
if (subcommandObject.allowPositional && !results.positional) {
|
|
231
|
+
results.positional = [];
|
|
232
|
+
}
|
|
233
|
+
|
|
229
234
|
return results;
|
|
230
235
|
}
|
|
@@ -10,12 +10,11 @@ type ArgumentsArray2ArrayType<T extends Argument[] | undefined> = T extends Argu
|
|
|
10
10
|
: never;
|
|
11
11
|
|
|
12
12
|
export type ValidateResult<S extends Partial<Subcommand>[]> = {
|
|
13
|
-
[K in keyof S]: Prettify<
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
>;
|
|
13
|
+
[K in keyof S]: Prettify<{
|
|
14
|
+
subcommand: S[K]["name"] extends string ? S[K]["name"] : undefined;
|
|
15
|
+
arguments: ArgumentsArray2ArrayType<S[K]["arguments"]>;
|
|
16
|
+
positional: S[K]["allowPositional"] extends true ? string[] : never;
|
|
17
|
+
options: S[K]["options"] extends Option[] ? OptionsArray2RecordType<S[K]["options"]> : never;
|
|
18
|
+
ctx: ParseResult<S>;
|
|
19
|
+
}>;
|
|
21
20
|
}[number];
|
|
@@ -10,6 +10,7 @@ type ResultsTemporaryType = Record<string, unknown> & {
|
|
|
10
10
|
subcommand: string | undefined;
|
|
11
11
|
positional?: string[];
|
|
12
12
|
arguments?: unknown[];
|
|
13
|
+
options?: Record<string, unknown>;
|
|
13
14
|
ctx: ParsedContext;
|
|
14
15
|
};
|
|
15
16
|
|
|
@@ -21,26 +22,30 @@ export function validate(parsedData: ParsedContext) {
|
|
|
21
22
|
};
|
|
22
23
|
|
|
23
24
|
// validate options
|
|
24
|
-
|
|
25
|
-
|
|
25
|
+
if (parsedData.options) {
|
|
26
|
+
if (!results.options) results.options = {};
|
|
26
27
|
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
28
|
+
for (const [optionName, { schema, rawValue, flag }] of Object.entries(parsedData.options)) {
|
|
29
|
+
let optionsValue: string | boolean | undefined = rawValue;
|
|
30
|
+
|
|
31
|
+
// infer boolean value if possible
|
|
32
|
+
if (flag && rawValue && isBooleanSchema(schema)) {
|
|
33
|
+
const booleanValue = stringToBoolean(rawValue);
|
|
34
|
+
if (typeof booleanValue === "boolean") {
|
|
35
|
+
const isNegated = flag.startsWith("--no");
|
|
36
|
+
optionsValue = isNegated ? !booleanValue : booleanValue;
|
|
37
|
+
}
|
|
33
38
|
}
|
|
34
|
-
}
|
|
35
39
|
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
40
|
+
const safeParseResult = safeParseSchema(schema, optionsValue);
|
|
41
|
+
if (!safeParseResult.success) {
|
|
42
|
+
throw new Error(`Invalid value "${rawValue}" for "${flag}": ${prettifyError(safeParseResult.error)}`, {
|
|
43
|
+
cause: "zod-args-parser",
|
|
44
|
+
});
|
|
45
|
+
}
|
|
42
46
|
|
|
43
|
-
|
|
47
|
+
results.options[optionName] = safeParseResult.data;
|
|
48
|
+
}
|
|
44
49
|
}
|
|
45
50
|
|
|
46
51
|
// validate arguments
|