zod-args-parser 1.0.2 → 1.0.4

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/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2024 Ahmed Alabsi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
package/README.md CHANGED
@@ -6,6 +6,7 @@ A strictly typed command-line arguments parser powered by Zod.
6
6
 
7
7
  - **Strict typing for subcommands, options, and arguments**.
8
8
  - **Flag coupling support**: e.g., `-rf` to combine `-r` and `-f` flags.
9
+ - **Negative flag support**: e.g., `--no-verbose` to negate `--verbose`.
9
10
  - **Flexible option value formatting**: Supports both `--input-dir path` and `--input-dir=path` styles.
10
11
  - **Help message generation**: Built-in methods to generate help text for the CLI and each subcommand.
11
12
 
@@ -18,28 +19,57 @@ npm install zod-args-parser
18
19
  ## Usage
19
20
 
20
21
  ```ts
21
- import { createCli, createSubcommand, safeParse } from "zod-args-parser";
22
+ import { z } from "zod";
23
+ import { createCli, createSubcommand, createOptions, safeParse } from "zod-args-parser";
24
+
25
+ // Share same options between subcommands
26
+ const sharedOptions = createOptions([
27
+ {
28
+ name: "verbose",
29
+ description: "Verbose mode",
30
+ type: z.boolean().optional(),
31
+ },
32
+ ]);
22
33
 
23
34
  // Create a CLI schema
24
35
  // This will be used when no subcommands are provided
25
- const cliProgram = createCli({
36
+ const cliSchema = createCli({
26
37
  cliName: "my-cli",
27
38
  description: "A description for my CLI",
28
39
  example: "example of how to use my cli\nmy-cli --help",
29
40
  options: [
30
41
  {
31
42
  name: "help",
32
- description: "Show this help message",
33
43
  aliases: ["h"],
34
- type: z.boolean(),
44
+ type: z.boolean().optional().describe("Show this help message"),
35
45
  },
46
+ {
47
+ name: "version",
48
+ aliases: ["v"],
49
+ description: "Show version",
50
+ type: z.boolean().optional(),
51
+ },
52
+ ...sharedOptions,
36
53
  ],
37
54
  });
38
55
 
39
- // Execute this function when the CLI is run
40
- cliProgram.setAction(results => {
41
- const { help } = results;
42
- if (help) results.printCliHelp();
56
+ // Execute this function when the CLI is run (no subcommands)
57
+ cliSchema.setAction(results => {
58
+ const { help, version, verbose } = results;
59
+
60
+ if (help) {
61
+ results.printCliHelp();
62
+ return;
63
+ }
64
+
65
+ if (version) {
66
+ console.log("v1.0.0");
67
+ return;
68
+ }
69
+
70
+ if (verbose) {
71
+ console.log("Verbose mode enabled");
72
+ }
43
73
  });
44
74
 
45
75
  // Create a subcommand schema
@@ -65,7 +95,7 @@ helpCommandSchema.setAction(results => {
65
95
 
66
96
  const results = safeParse(
67
97
  process.argv.slice(2),
68
- cliProgram,
98
+ cliSchema,
69
99
  helpCommandSchema,
70
100
  // Add more subcommands
71
101
  );
@@ -108,9 +138,7 @@ type Arguments = InferArgumentsType<typeof subcommand>;
108
138
 
109
139
  ## API
110
140
 
111
- ### `createSubcommand(input: Subcommand)`
112
-
113
- Defines a subcommand with associated options and arguments.
141
+ ### `Subcommand`
114
142
 
115
143
  - `name: string`
116
144
  The name of the subcommand.
@@ -121,6 +149,9 @@ Defines a subcommand with associated options and arguments.
121
149
  - `description?: string`
122
150
  A description of the subcommand for the help message.
123
151
 
152
+ - `usage?: string`
153
+ The usage of the subcommand for the help message.
154
+
124
155
  - `placeholder?: string`
125
156
  A placeholder displayed in the help message alongside the subcommand name.
126
157
 
@@ -135,7 +166,7 @@ Defines a subcommand with associated options and arguments.
135
166
  An array of options for the subcommand.
136
167
 
137
168
  - `name: string`
138
- The name of the option. Should be a valid variable name in JavaScript.
169
+ The name of the option. camelCase is recommended. E.g. `inputDir` -> `--input-dir`
139
170
 
140
171
  - `aliases?: string[]`
141
172
  An array of aliases for the option.
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.help=void 0;var _chalk=_interopRequireDefault(require("chalk"));var _utils=require("./utils.js");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e};}const colors={title:_chalk.default.bold.blue,description:_chalk.default.white,default:_chalk.default.dim.italic,optional:_chalk.default.dim.italic,exampleTitle:_chalk.default.yellow,example:_chalk.default.dim.italic,command:_chalk.default.yellow,option:_chalk.default.cyan,argument:_chalk.default.green,placeholder:_chalk.default.hex("#FF9800"),punctuation:_chalk.default.white.dim};function printCliHelp(params){let printOptions=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};printOptions.colors??=true;const noColors=new Proxy(colors,{get:()=>{return function(){for(var _len=arguments.length,str=new Array(_len),_key=0;_key<_len;_key++){str[_key]=arguments[_key];}return str.join(" ");};}});const c=printOptions.colors?colors:noColors;if(printOptions.customColors){Object.assign(c,printOptions.customColors);}const isFirstParamCli="cliName"in params[0];const cliOptions=isFirstParamCli?params.shift():{};const subcommands=params;const printTitle=title=>{(0,_utils.print)(c.title(` ${title.toUpperCase()} `));};const cliName=cliOptions.cliName??"";const usage=cliOptions.usage??(0,_utils.concat)(c.punctuation("$"),cliName,subcommands.length?c.command("[command]"):"",cliOptions.options?.length?c.option("[options]"):"",cliOptions.arguments?.length||cliOptions.allowPositional?c.argument("<arguments>"):"");printTitle("Usage");(0,_utils.println)();(0,_utils.println)((0,_utils.indent)(2),usage,(0,_utils.ln)(1));if(cliOptions.description){printTitle("Description");(0,_utils.println)();(0,_utils.println)((0,_utils.indent)(2),c.description(cliOptions.description),(0,_utils.ln)(1));}let longest=0;const[optionsToPrint,longestOptionIndent]=prepareOptionsToPrint(cliOptions.options);if(longestOptionIndent>longest)longest=longestOptionIndent;const[commandsToPrint,longestSubcommandIndent]=prepareCommandsToPrint(subcommands);if(longestSubcommandIndent>longest)longest=longestSubcommandIndent;const[argsToPrint,longestArgIndent]=prepareArgumentsToPrint(cliOptions.arguments);if(longestArgIndent>longest)longest=longestArgIndent;printPreparedOptions(optionsToPrint,c,longest);printPreparedCommands(commandsToPrint,c,longest);printPreparedArguments(argsToPrint,c,longest);if(cliOptions.example){printTitle("Example");(0,_utils.println)();const normalizeExample=cliOptions.example.replace(/\n/g,"\n"+(0,_utils.indent)(3));(0,_utils.println)((0,_utils.indent)(2),c.example(normalizeExample),(0,_utils.ln)(1));}}function printSubcommandHelp(subcommand){let printOptions=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};let cliName=arguments.length>2&&arguments[2]!==undefined?arguments[2]:"";printOptions.colors??=true;const noColors=new Proxy(colors,{get:()=>{return function(){for(var _len2=arguments.length,str=new Array(_len2),_key2=0;_key2<_len2;_key2++){str[_key2]=arguments[_key2];}return str.join(" ");};}});const c=printOptions.colors?colors:noColors;if(printOptions.customColors){Object.assign(c,printOptions.customColors);}const printTitle=title=>{(0,_utils.print)(c.title(` ${title.toUpperCase()} `));};const usage=(0,_utils.concat)(c.punctuation("$"),cliName,c.command(subcommand.name),subcommand.options?.length?c.option("[options]"):"",subcommand.arguments?.length||subcommand.allowPositional?c.argument("<arguments>"):"");printTitle("Usage");(0,_utils.println)();(0,_utils.println)((0,_utils.indent)(2),usage,(0,_utils.ln)(1));if(subcommand.description){printTitle("Description");(0,_utils.println)();const normalizeDesc=subcommand.description.replace(/\n/g,"\n"+(0,_utils.indent)(3));(0,_utils.println)((0,_utils.indent)(2),c.description(normalizeDesc),(0,_utils.ln)(1));}let longest=0;const[optionsToPrint,longestOptionIndent]=prepareOptionsToPrint(subcommand.options);if(longestOptionIndent>longest)longest=longestOptionIndent;const[argsToPrint,longestArgIndent]=prepareArgumentsToPrint(subcommand.arguments);if(longestArgIndent>longest)longest=longestArgIndent;printPreparedOptions(optionsToPrint,c,longest);printPreparedArguments(argsToPrint,c,longest);if(subcommand.example){printTitle("Example");(0,_utils.println)();const normalizeExample=subcommand.example.replace(/\n/g,"\n"+(0,_utils.indent)(3));(0,_utils.println)((0,_utils.indent)(2),c.example(normalizeExample),(0,_utils.ln)(1));}}function prepareOptionsToPrint(options){if(!options||!options.length)return[[],0];const optionsToPrint=[];let longest=0;for(const option of options){const nameWithAliases=option.aliases?[...option.aliases,option.name]:[option.name];const names=Array.from(new Set(nameWithAliases.map(name=>(0,_utils.transformOptionToArg)(name)))).join(", ");const defaultValue=(0,_utils.getDefaultValueFromSchema)(option.type);const placeholder=option.placeholder??" ";optionsToPrint.push({names,placeholder,description:option.description??option.type.description??"",default:typeof defaultValue!=="undefined"?`(default: ${JSON.stringify(defaultValue)})`:"",optional:option.type.isOptional()?"[optional]":"",example:option.example??""});const optLength=names.length+placeholder.length;if(optLength>longest)longest=optLength;}return[optionsToPrint,longest];}function prepareCommandsToPrint(subcommands){if(!subcommands||!subcommands.length)return[[],0];const commandsToPrint=[];let longest=0;for(const subcommand of subcommands){const{name,aliases,description}=subcommand;const names=Array.from(new Set([...(aliases??[]),name])).join(", ");const placeholder=subcommand.placeholder??(subcommand.options?"[options]":subcommand.allowPositional?"<args>":" ");commandsToPrint.push({names,placeholder,description:description??""});const cmdLength=names.length+placeholder.length;if(cmdLength>longest)longest=cmdLength;}return[commandsToPrint,longest];}function prepareArgumentsToPrint(args){if(!args||!args.length)return[[],0];const argsToPrint=[];let longest=0;for(const arg of args){const defaultValue=(0,_utils.getDefaultValueFromSchema)(arg.type);argsToPrint.push({names:arg.name,description:arg.description??"",default:typeof defaultValue!=="undefined"?`(default: ${JSON.stringify(defaultValue)})`:"",optional:arg.type.isOptional()?"[optional]":"",example:arg.example??""});const cmdLength=arg.name.length;if(cmdLength>longest)longest=cmdLength;}return[argsToPrint,longest];}function printPreparedOptions(optionsToPrint,c,longest){if(!optionsToPrint.length)return;(0,_utils.print)(c.title(" OPTIONS "));(0,_utils.println)();for(const{names,placeholder,description,example,optional,default:def}of optionsToPrint){const optLength=names.length+(placeholder?.length??0);const spacing=longest+1-optLength;const normalizeDesc=description.replace(/\n/g,"\n"+(0,_utils.indent)(longest+7)+c.punctuation("└"));const coloredNames=names.split(/(,)/).map(name=>name===","?c.punctuation(name):c.option(name)).join("");(0,_utils.println)((0,_utils.indent)(2),coloredNames,c.placeholder(placeholder),(0,_utils.indent)(spacing),c.description(normalizeDesc),def?c.default(def):c.optional(optional));if(example){const normalizeExample=example.replace(/\n/g,"\n"+(0,_utils.indent)(longest+17));(0,_utils.println)((0,_utils.indent)(longest+6),c.punctuation("└")+c.exampleTitle("Example:"),c.example(normalizeExample));}}(0,_utils.println)();}function printPreparedCommands(commandsToPrint,c,longest){if(!commandsToPrint.length)return;(0,_utils.print)(c.title(" COMMANDS "));(0,_utils.println)();for(const{names,placeholder,description}of commandsToPrint){const optLength=names.length+(placeholder?.length??0);const spacing=longest+1-optLength;const normalizeDesc=description.replace(/\n/g,"\n"+(0,_utils.indent)(longest+7));const coloredNames=names.split(/(,)/).map(name=>name===","?c.punctuation(name):c.command(name)).join("");(0,_utils.println)((0,_utils.indent)(2),coloredNames,c.placeholder(placeholder),(0,_utils.indent)(spacing),c.description(normalizeDesc));}(0,_utils.println)();}function printPreparedArguments(argsToPrint,c,longest){if(!argsToPrint.length)return;(0,_utils.print)(c.title(" ARGUMENTS "));(0,_utils.println)();for(const{names,description,example,optional,default:def}of argsToPrint){const spacing=longest+2-names.length;const normalizeDesc=description.replace(/\n/g,"\n"+(0,_utils.indent)(longest+6)+c.punctuation("└"));(0,_utils.println)((0,_utils.indent)(2),c.argument(names),(0,_utils.indent)(spacing),c.description(normalizeDesc),def?c.default(def):c.optional(optional));if(example){const normalizeExample=example.replace(/\n/g,"\n"+(0,_utils.indent)(longest+16));(0,_utils.println)((0,_utils.indent)(longest+5),c.punctuation("└")+c.exampleTitle("Example:"),c.example(normalizeExample));}}(0,_utils.println)();}const help=exports.help={printCliHelp,printSubcommandHelp};
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.help=void 0;var _chalk=_interopRequireDefault(require("chalk"));var _utils=require("./utils.js");function _interopRequireDefault(e){return e&&e.__esModule?e:{default:e};}const colors={title:_chalk.default.bold.blue,description:_chalk.default.white,default:_chalk.default.dim.italic,optional:_chalk.default.dim.italic,exampleTitle:_chalk.default.yellow,example:_chalk.default.dim,command:_chalk.default.yellow,option:_chalk.default.cyan,argument:_chalk.default.green,placeholder:_chalk.default.hex("#FF9800"),punctuation:_chalk.default.white.dim};function printCliHelp(params){let printOptions=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};printOptions.colors??=true;const noColors=new Proxy(colors,{get:()=>{return function(){for(var _len=arguments.length,str=new Array(_len),_key=0;_key<_len;_key++){str[_key]=arguments[_key];}return str.join(" ");};}});const c=printOptions.colors?colors:noColors;if(printOptions.customColors){Object.assign(c,printOptions.customColors);}const isFirstParamCli="cliName"in params[0];const cliOptions=isFirstParamCli?params.shift():{};const subcommands=params;const printTitle=title=>{(0,_utils.print)(c.title(` ${title.toUpperCase()} `));};const cliName=cliOptions.cliName??"";const usage=cliOptions.usage??(0,_utils.concat)(c.punctuation("$"),cliName,subcommands.length?c.command("[command]"):"",cliOptions.options?.length?c.option("[options]"):"",cliOptions.arguments?.length||cliOptions.allowPositional?c.argument("<arguments>"):"");printTitle("Usage");(0,_utils.println)();(0,_utils.println)((0,_utils.indent)(2),usage,(0,_utils.ln)(1));if(cliOptions.description){printTitle("Description");(0,_utils.println)();(0,_utils.println)((0,_utils.indent)(2),c.description(cliOptions.description),(0,_utils.ln)(1));}let longest=0;const[optionsToPrint,longestOptionIndent]=prepareOptionsToPrint(cliOptions.options);if(longestOptionIndent>longest)longest=longestOptionIndent;const[commandsToPrint,longestSubcommandIndent]=prepareCommandsToPrint(subcommands);if(longestSubcommandIndent>longest)longest=longestSubcommandIndent;const[argsToPrint,longestArgIndent]=prepareArgumentsToPrint(cliOptions.arguments);if(longestArgIndent>longest)longest=longestArgIndent;printPreparedOptions(optionsToPrint,c,longest);printPreparedCommands(commandsToPrint,c,longest);printPreparedArguments(argsToPrint,c,longest);if(cliOptions.example){printTitle("Example");(0,_utils.println)();const normalizeExample=cliOptions.example.replace(/\n/g,"\n"+(0,_utils.indent)(3));(0,_utils.println)((0,_utils.indent)(2),c.example(normalizeExample),(0,_utils.ln)(1));}}function printSubcommandHelp(subcommand){let printOptions=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};let cliName=arguments.length>2&&arguments[2]!==undefined?arguments[2]:"";printOptions.colors??=true;const noColors=new Proxy(colors,{get:()=>{return function(){for(var _len2=arguments.length,str=new Array(_len2),_key2=0;_key2<_len2;_key2++){str[_key2]=arguments[_key2];}return str.join(" ");};}});const c=printOptions.colors?colors:noColors;if(printOptions.customColors){Object.assign(c,printOptions.customColors);}const printTitle=title=>{(0,_utils.print)(c.title(` ${title.toUpperCase()} `));};const usage=subcommand.usage??(0,_utils.concat)(c.punctuation("$"),cliName,c.command(subcommand.name),subcommand.options?.length?c.option("[options]"):"",subcommand.arguments?.length||subcommand.allowPositional?c.argument("<arguments>"):"");printTitle("Usage");(0,_utils.println)();(0,_utils.println)((0,_utils.indent)(2),usage,(0,_utils.ln)(1));if(subcommand.description){printTitle("Description");(0,_utils.println)();const normalizeDesc=subcommand.description.replace(/\n/g,"\n"+(0,_utils.indent)(3));(0,_utils.println)((0,_utils.indent)(2),c.description(normalizeDesc),(0,_utils.ln)(1));}let longest=0;const[optionsToPrint,longestOptionIndent]=prepareOptionsToPrint(subcommand.options);if(longestOptionIndent>longest)longest=longestOptionIndent;const[argsToPrint,longestArgIndent]=prepareArgumentsToPrint(subcommand.arguments);if(longestArgIndent>longest)longest=longestArgIndent;printPreparedOptions(optionsToPrint,c,longest);printPreparedArguments(argsToPrint,c,longest);if(subcommand.example){printTitle("Example");(0,_utils.println)();const normalizeExample=subcommand.example.replace(/\n/g,"\n"+(0,_utils.indent)(3));(0,_utils.println)((0,_utils.indent)(2),c.example(normalizeExample),(0,_utils.ln)(1));}}function prepareOptionsToPrint(options){if(!options||!options.length)return[[],0];const optionsToPrint=[];let longest=0;for(const option of options){const nameWithAliases=option.aliases?[...option.aliases,option.name]:[option.name];const names=Array.from(new Set(nameWithAliases.map(name=>(0,_utils.transformOptionToArg)(name)))).join(", ");const defaultValue=(0,_utils.getDefaultValueFromSchema)(option.type);const placeholder=option.placeholder??" ";optionsToPrint.push({names,placeholder,description:option.description??option.type.description??"",default:typeof defaultValue!=="undefined"?`(default: ${JSON.stringify(defaultValue)})`:"",optional:option.type.isOptional()?"[optional]":"",example:option.example??""});const optLength=names.length+placeholder.length;if(optLength>longest)longest=optLength;}return[optionsToPrint,longest];}function prepareCommandsToPrint(subcommands){if(!subcommands||!subcommands.length)return[[],0];const commandsToPrint=[];let longest=0;for(const subcommand of subcommands){const{name,aliases,description}=subcommand;const names=Array.from(new Set([...(aliases??[]),name])).join(", ");const placeholder=subcommand.placeholder??(subcommand.options?"[options]":subcommand.allowPositional?"<args>":" ");commandsToPrint.push({names,placeholder,description:description??""});const cmdLength=names.length+placeholder.length;if(cmdLength>longest)longest=cmdLength;}return[commandsToPrint,longest];}function prepareArgumentsToPrint(args){if(!args||!args.length)return[[],0];const argsToPrint=[];let longest=0;for(const arg of args){const defaultValue=(0,_utils.getDefaultValueFromSchema)(arg.type);argsToPrint.push({names:arg.name,description:arg.description??"",default:typeof defaultValue!=="undefined"?`(default: ${JSON.stringify(defaultValue)})`:"",optional:arg.type.isOptional()?"[optional]":"",example:arg.example??""});const cmdLength=arg.name.length;if(cmdLength>longest)longest=cmdLength;}return[argsToPrint,longest];}function printPreparedOptions(optionsToPrint,c,longest){if(!optionsToPrint.length)return;(0,_utils.print)(c.title(" OPTIONS "));(0,_utils.println)();for(const{names,placeholder,description,example,optional,default:def}of optionsToPrint){const optLength=names.length+(placeholder?.length??0);const spacing=longest+1-optLength;const normalizeDesc=description.replace(/\n/g,"\n"+(0,_utils.indent)(longest+7)+c.punctuation("└"));const coloredNames=names.split(/(,)/).map(name=>name===","?c.punctuation(name):c.option(name)).join("");(0,_utils.println)((0,_utils.indent)(2),coloredNames,c.placeholder(placeholder),(0,_utils.indent)(spacing),c.description(normalizeDesc),def?c.default(def):c.optional(optional));if(example){const normalizeExample=example.replace(/\n/g,"\n"+(0,_utils.indent)(longest+17));(0,_utils.println)((0,_utils.indent)(longest+6),c.punctuation("└")+c.exampleTitle("Example:"),c.example(normalizeExample));}}(0,_utils.println)();}function printPreparedCommands(commandsToPrint,c,longest){if(!commandsToPrint.length)return;(0,_utils.print)(c.title(" COMMANDS "));(0,_utils.println)();for(const{names,placeholder,description}of commandsToPrint){const optLength=names.length+(placeholder?.length??0);const spacing=longest+1-optLength;const normalizeDesc=description.replace(/\n/g,"\n"+(0,_utils.indent)(longest+7)+c.punctuation("└"));const coloredNames=names.split(/(,)/).map(name=>name===","?c.punctuation(name):c.command(name)).join("");(0,_utils.println)((0,_utils.indent)(2),coloredNames,c.placeholder(placeholder),(0,_utils.indent)(spacing),c.description(normalizeDesc));}(0,_utils.println)();}function printPreparedArguments(argsToPrint,c,longest){if(!argsToPrint.length)return;(0,_utils.print)(c.title(" ARGUMENTS "));(0,_utils.println)();for(const{names,description,example,optional,default:def}of argsToPrint){const spacing=longest+2-names.length;const normalizeDesc=description.replace(/\n/g,"\n"+(0,_utils.indent)(longest+6)+c.punctuation("└"));(0,_utils.println)((0,_utils.indent)(2),c.argument(names),(0,_utils.indent)(spacing),c.description(normalizeDesc),def?c.default(def):c.optional(optional));if(example){const normalizeExample=example.replace(/\n/g,"\n"+(0,_utils.indent)(longest+16));(0,_utils.println)((0,_utils.indent)(longest+5),c.punctuation("└")+c.exampleTitle("Example:"),c.example(normalizeExample));}}(0,_utils.println)();}const help=exports.help={printCliHelp,printSubcommandHelp};
@@ -1 +1 @@
1
- {"version":3,"names":["_chalk","_interopRequireDefault","require","_utils","e","__esModule","default","colors","title","chalk","bold","blue","description","white","dim","italic","optional","exampleTitle","yellow","example","command","option","cyan","argument","green","placeholder","hex","punctuation","printCliHelp","params","printOptions","arguments","length","undefined","noColors","Proxy","get","_len","str","Array","_key","join","c","customColors","Object","assign","isFirstParamCli","cliOptions","shift","subcommands","printTitle","print","toUpperCase","cliName","usage","concat","options","allowPositional","println","indent","ln","longest","optionsToPrint","longestOptionIndent","prepareOptionsToPrint","commandsToPrint","longestSubcommandIndent","prepareCommandsToPrint","argsToPrint","longestArgIndent","prepareArgumentsToPrint","printPreparedOptions","printPreparedCommands","printPreparedArguments","normalizeExample","replace","printSubcommandHelp","subcommand","_len2","_key2","name","normalizeDesc","nameWithAliases","aliases","names","from","Set","map","transformOptionToArg","defaultValue","getDefaultValueFromSchema","type","push","JSON","stringify","isOptional","optLength","cmdLength","args","arg","def","spacing","coloredNames","split","help","exports"],"sourceRoot":"../../src","sources":["help.ts"],"sourcesContent":["import chalk from \"chalk\";\nimport { concat, getDefaultValueFromSchema, indent, ln, print, println, transformOptionToArg } from \"./utils.js\";\n\nimport type { Argument, Cli, Option, PrintHelpOpt, Subcommand } from \"./types.js\";\n\n/** Colors */\nconst colors: NonNullable<Required<PrintHelpOpt[\"customColors\"]>> = {\n title: chalk.bold.blue,\n description: chalk.white,\n default: chalk.dim.italic,\n optional: chalk.dim.italic,\n exampleTitle: chalk.yellow,\n example: chalk.dim.italic,\n command: chalk.yellow,\n option: chalk.cyan,\n argument: chalk.green,\n placeholder: chalk.hex(\"#FF9800\"),\n punctuation: chalk.white.dim,\n};\n\ntype PreparedToPrint = {\n names: string;\n description: string;\n placeholder?: string;\n example?: string;\n default?: string;\n optional?: string;\n};\n\nfunction printCliHelp(params: [Cli, ...Subcommand[]], printOptions: PrintHelpOpt = {}) {\n printOptions.colors ??= true;\n\n const noColors = new Proxy(colors, {\n get: () => {\n return (...str: string[]) => str.join(\" \");\n },\n });\n\n const c = printOptions.colors ? colors : noColors;\n\n if (printOptions.customColors) {\n Object.assign(c, printOptions.customColors);\n }\n\n const isFirstParamCli = \"cliName\" in params[0];\n const cliOptions = (isFirstParamCli ? params.shift() : {}) as Cli;\n const subcommands = params as Subcommand[];\n\n /** Print a styled title */\n const printTitle = (title: string) => {\n print(c.title(` ${title.toUpperCase()} `));\n };\n\n // Print CLI usage\n const cliName = cliOptions.cliName ?? \"\";\n const usage =\n cliOptions.usage ??\n concat(\n c.punctuation(\"$\"),\n cliName,\n subcommands.length ? c.command(\"[command]\") : \"\",\n cliOptions.options?.length ? c.option(\"[options]\") : \"\",\n cliOptions.arguments?.length || cliOptions.allowPositional ? c.argument(\"<arguments>\") : \"\",\n );\n printTitle(\"Usage\");\n println();\n println(indent(2), usage, ln(1));\n\n // Print CLI description\n if (cliOptions.description) {\n printTitle(\"Description\");\n println();\n println(indent(2), c.description(cliOptions.description), ln(1));\n }\n\n let longest = 0;\n\n // Prepare CLI options\n const [optionsToPrint, longestOptionIndent] = prepareOptionsToPrint(cliOptions.options);\n if (longestOptionIndent > longest) longest = longestOptionIndent;\n\n // Prepare CLI commands\n const [commandsToPrint, longestSubcommandIndent] = prepareCommandsToPrint(subcommands);\n if (longestSubcommandIndent > longest) longest = longestSubcommandIndent;\n\n // Prepare CLI arguments\n const [argsToPrint, longestArgIndent] = prepareArgumentsToPrint(cliOptions.arguments);\n if (longestArgIndent > longest) longest = longestArgIndent;\n\n // Print CLI options\n printPreparedOptions(optionsToPrint, c, longest);\n\n // Print CLI commands\n printPreparedCommands(commandsToPrint, c, longest);\n\n // Print CLI arguments\n printPreparedArguments(argsToPrint, c, longest);\n\n // Print CLI example\n if (cliOptions.example) {\n printTitle(\"Example\");\n println();\n const normalizeExample = cliOptions.example.replace(/\\n/g, \"\\n\" + indent(3));\n println(indent(2), c.example(normalizeExample), ln(1));\n }\n}\n\nfunction printSubcommandHelp(subcommand: Subcommand, printOptions: PrintHelpOpt = {}, cliName = \"\") {\n printOptions.colors ??= true;\n\n const noColors = new Proxy(colors, {\n get: () => {\n return (...str: string[]) => str.join(\" \");\n },\n });\n\n const c = printOptions.colors ? colors : noColors;\n\n if (printOptions.customColors) {\n Object.assign(c, printOptions.customColors);\n }\n\n /** Print a styled title */\n const printTitle = (title: string) => {\n print(c.title(` ${title.toUpperCase()} `));\n };\n\n // Print command usage\n const usage = concat(\n c.punctuation(\"$\"),\n cliName,\n c.command(subcommand.name),\n subcommand.options?.length ? c.option(\"[options]\") : \"\",\n subcommand.arguments?.length || subcommand.allowPositional ? c.argument(\"<arguments>\") : \"\",\n );\n printTitle(\"Usage\");\n println();\n println(indent(2), usage, ln(1));\n\n // Print command description\n if (subcommand.description) {\n printTitle(\"Description\");\n println();\n const normalizeDesc = subcommand.description.replace(/\\n/g, \"\\n\" + indent(3));\n println(indent(2), c.description(normalizeDesc), ln(1));\n }\n\n let longest = 0;\n\n // Prepare command options\n const [optionsToPrint, longestOptionIndent] = prepareOptionsToPrint(subcommand.options);\n if (longestOptionIndent > longest) longest = longestOptionIndent;\n\n // Prepare command arguments\n const [argsToPrint, longestArgIndent] = prepareArgumentsToPrint(subcommand.arguments);\n if (longestArgIndent > longest) longest = longestArgIndent;\n\n // Print command options\n printPreparedOptions(optionsToPrint, c, longest);\n\n // Print command arguments\n printPreparedArguments(argsToPrint, c, longest);\n\n // Print command example\n if (subcommand.example) {\n printTitle(\"Example\");\n println();\n const normalizeExample = subcommand.example.replace(/\\n/g, \"\\n\" + indent(3));\n println(indent(2), c.example(normalizeExample), ln(1));\n }\n}\n\n// * Prepare\nfunction prepareOptionsToPrint(options: Option[] | undefined): [PreparedToPrint[], number] {\n if (!options || !options.length) return [[], 0];\n\n const optionsToPrint: PreparedToPrint[] = [];\n let longest = 0;\n\n for (const option of options) {\n const nameWithAliases = option.aliases ? [...option.aliases, option.name] : [option.name];\n const names = Array.from(new Set(nameWithAliases.map(name => transformOptionToArg(name)))).join(\", \");\n\n const defaultValue = getDefaultValueFromSchema(option.type);\n\n const placeholder = option.placeholder ?? \" \";\n optionsToPrint.push({\n names,\n placeholder,\n description: option.description ?? option.type.description ?? \"\",\n default: typeof defaultValue !== \"undefined\" ? `(default: ${JSON.stringify(defaultValue)})` : \"\",\n optional: option.type.isOptional() ? \"[optional]\" : \"\",\n example: option.example ?? \"\",\n });\n\n const optLength = names.length + placeholder.length;\n\n if (optLength > longest) longest = optLength;\n }\n\n return [optionsToPrint, longest];\n}\n\nfunction prepareCommandsToPrint(subcommands: Subcommand[] | undefined): [PreparedToPrint[], number] {\n if (!subcommands || !subcommands.length) return [[], 0];\n\n const commandsToPrint: PreparedToPrint[] = [];\n let longest = 0;\n\n for (const subcommand of subcommands) {\n const { name, aliases, description } = subcommand;\n const names = Array.from(new Set([...(aliases ?? []), name])).join(\", \");\n\n const placeholder =\n subcommand.placeholder ?? (subcommand.options ? \"[options]\" : subcommand.allowPositional ? \"<args>\" : \" \");\n\n commandsToPrint.push({ names, placeholder, description: description ?? \"\" });\n\n const cmdLength = names.length + placeholder.length;\n if (cmdLength > longest) longest = cmdLength;\n }\n\n return [commandsToPrint, longest];\n}\n\nfunction prepareArgumentsToPrint(args: Argument[] | undefined): [PreparedToPrint[], number] {\n if (!args || !args.length) return [[], 0];\n\n const argsToPrint: PreparedToPrint[] = [];\n let longest = 0;\n\n for (const arg of args) {\n const defaultValue = getDefaultValueFromSchema(arg.type);\n\n argsToPrint.push({\n names: arg.name,\n description: arg.description ?? \"\",\n default: typeof defaultValue !== \"undefined\" ? `(default: ${JSON.stringify(defaultValue)})` : \"\",\n optional: arg.type.isOptional() ? \"[optional]\" : \"\",\n example: arg.example ?? \"\",\n });\n\n const cmdLength = arg.name.length;\n if (cmdLength > longest) longest = cmdLength;\n }\n\n return [argsToPrint, longest];\n}\n\n// * Print\nfunction printPreparedOptions(optionsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {\n if (!optionsToPrint.length) return;\n\n print(c.title(\" OPTIONS \"));\n\n println();\n\n for (const { names, placeholder, description, example, optional, default: def } of optionsToPrint) {\n const optLength = names.length + (placeholder?.length ?? 0);\n const spacing = longest + 1 - optLength;\n const normalizeDesc = description.replace(/\\n/g, \"\\n\" + indent(longest + 7) + c.punctuation(\"└\"));\n\n const coloredNames = names\n .split(/(,)/)\n .map(name => (name === \",\" ? c.punctuation(name) : c.option(name)))\n .join(\"\");\n\n println(\n indent(2),\n coloredNames,\n c.placeholder(placeholder),\n indent(spacing),\n c.description(normalizeDesc),\n def ? c.default(def) : c.optional(optional),\n );\n\n if (example) {\n const normalizeExample = example.replace(/\\n/g, \"\\n\" + indent(longest + 17));\n println(indent(longest + 6), c.punctuation(\"└\") + c.exampleTitle(\"Example:\"), c.example(normalizeExample));\n }\n }\n\n println();\n}\n\nfunction printPreparedCommands(commandsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {\n if (!commandsToPrint.length) return;\n\n print(c.title(\" COMMANDS \"));\n\n println();\n\n for (const { names, placeholder, description } of commandsToPrint) {\n const optLength = names.length + (placeholder?.length ?? 0);\n const spacing = longest + 1 - optLength;\n const normalizeDesc = description.replace(/\\n/g, \"\\n\" + indent(longest + 7));\n\n const coloredNames = names\n .split(/(,)/)\n .map(name => (name === \",\" ? c.punctuation(name) : c.command(name)))\n .join(\"\");\n\n println(indent(2), coloredNames, c.placeholder(placeholder), indent(spacing), c.description(normalizeDesc));\n }\n\n println();\n}\n\nfunction printPreparedArguments(argsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {\n if (!argsToPrint.length) return;\n\n print(c.title(\" ARGUMENTS \"));\n\n println();\n\n for (const { names, description, example, optional, default: def } of argsToPrint) {\n const spacing = longest + 2 - names.length;\n const normalizeDesc = description.replace(/\\n/g, \"\\n\" + indent(longest + 6) + c.punctuation(\"└\"));\n\n println(\n indent(2),\n c.argument(names),\n indent(spacing),\n c.description(normalizeDesc),\n def ? c.default(def) : c.optional(optional),\n );\n\n if (example) {\n const normalizeExample = example.replace(/\\n/g, \"\\n\" + indent(longest + 16));\n println(indent(longest + 5), c.punctuation(\"└\") + c.exampleTitle(\"Example:\"), c.example(normalizeExample));\n }\n }\n\n println();\n}\n\nexport const help = {\n printCliHelp,\n printSubcommandHelp,\n};\n"],"mappings":"0FAAA,IAAAA,MAAA,CAAAC,sBAAA,CAAAC,OAAA,WACA,IAAAC,MAAA,CAAAD,OAAA,eAAiH,SAAAD,uBAAAG,CAAA,SAAAA,CAAA,EAAAA,CAAA,CAAAC,UAAA,CAAAD,CAAA,EAAAE,OAAA,CAAAF,CAAA,GAKjH,KAAM,CAAAG,MAA2D,CAAG,CAClEC,KAAK,CAAEC,cAAK,CAACC,IAAI,CAACC,IAAI,CACtBC,WAAW,CAAEH,cAAK,CAACI,KAAK,CACxBP,OAAO,CAAEG,cAAK,CAACK,GAAG,CAACC,MAAM,CACzBC,QAAQ,CAAEP,cAAK,CAACK,GAAG,CAACC,MAAM,CAC1BE,YAAY,CAAER,cAAK,CAACS,MAAM,CAC1BC,OAAO,CAAEV,cAAK,CAACK,GAAG,CAACC,MAAM,CACzBK,OAAO,CAAEX,cAAK,CAACS,MAAM,CACrBG,MAAM,CAAEZ,cAAK,CAACa,IAAI,CAClBC,QAAQ,CAAEd,cAAK,CAACe,KAAK,CACrBC,WAAW,CAAEhB,cAAK,CAACiB,GAAG,CAAC,SAAS,CAAC,CACjCC,WAAW,CAAElB,cAAK,CAACI,KAAK,CAACC,GAC3B,CAAC,CAWD,QAAS,CAAAc,YAAYA,CAACC,MAA8B,CAAmC,IAAjC,CAAAC,YAA0B,CAAAC,SAAA,CAAAC,MAAA,IAAAD,SAAA,MAAAE,SAAA,CAAAF,SAAA,IAAG,CAAC,CAAC,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,cAAO,EAAC,CAAC,CACX,CAEO,KAAM,CAAA8C,IAAI,CAAAC,OAAA,CAAAD,IAAA,CAAG,CAClB5E,YAAY,CACZgD,mBACF,CAAC","ignoreList":[]}
1
+ {"version":3,"names":["_chalk","_interopRequireDefault","require","_utils","e","__esModule","default","colors","title","chalk","bold","blue","description","white","dim","italic","optional","exampleTitle","yellow","example","command","option","cyan","argument","green","placeholder","hex","punctuation","printCliHelp","params","printOptions","arguments","length","undefined","noColors","Proxy","get","_len","str","Array","_key","join","c","customColors","Object","assign","isFirstParamCli","cliOptions","shift","subcommands","printTitle","print","toUpperCase","cliName","usage","concat","options","allowPositional","println","indent","ln","longest","optionsToPrint","longestOptionIndent","prepareOptionsToPrint","commandsToPrint","longestSubcommandIndent","prepareCommandsToPrint","argsToPrint","longestArgIndent","prepareArgumentsToPrint","printPreparedOptions","printPreparedCommands","printPreparedArguments","normalizeExample","replace","printSubcommandHelp","subcommand","_len2","_key2","name","normalizeDesc","nameWithAliases","aliases","names","from","Set","map","transformOptionToArg","defaultValue","getDefaultValueFromSchema","type","push","JSON","stringify","isOptional","optLength","cmdLength","args","arg","def","spacing","coloredNames","split","help","exports"],"sourceRoot":"../../src","sources":["help.ts"],"sourcesContent":["import chalk from \"chalk\";\nimport { concat, getDefaultValueFromSchema, indent, ln, print, println, transformOptionToArg } from \"./utils.js\";\n\nimport type { Argument, Cli, Option, PrintHelpOpt, Subcommand } from \"./types.js\";\n\nconst colors: NonNullable<Required<PrintHelpOpt[\"customColors\"]>> = {\n title: chalk.bold.blue,\n description: chalk.white,\n default: chalk.dim.italic,\n optional: chalk.dim.italic,\n exampleTitle: chalk.yellow,\n example: chalk.dim,\n command: chalk.yellow,\n option: chalk.cyan,\n argument: chalk.green,\n placeholder: chalk.hex(\"#FF9800\"),\n punctuation: chalk.white.dim,\n};\n\ntype PreparedToPrint = {\n names: string;\n description: string;\n placeholder?: string;\n example?: string;\n default?: string;\n optional?: string;\n};\n\nfunction printCliHelp(params: [Cli, ...Subcommand[]], printOptions: PrintHelpOpt = {}) {\n printOptions.colors ??= true;\n\n const noColors = new Proxy(colors, {\n get: () => {\n return (...str: string[]) => str.join(\" \");\n },\n });\n\n const c = printOptions.colors ? colors : noColors;\n\n if (printOptions.customColors) {\n Object.assign(c, printOptions.customColors);\n }\n\n const isFirstParamCli = \"cliName\" in params[0];\n const cliOptions = (isFirstParamCli ? params.shift() : {}) as Cli;\n const subcommands = params as Subcommand[];\n\n /** Print a styled title */\n const printTitle = (title: string) => {\n print(c.title(` ${title.toUpperCase()} `));\n };\n\n // Print CLI usage\n const cliName = cliOptions.cliName ?? \"\";\n const usage =\n cliOptions.usage ??\n concat(\n c.punctuation(\"$\"),\n cliName,\n subcommands.length ? c.command(\"[command]\") : \"\",\n cliOptions.options?.length ? c.option(\"[options]\") : \"\",\n cliOptions.arguments?.length || cliOptions.allowPositional ? c.argument(\"<arguments>\") : \"\",\n );\n printTitle(\"Usage\");\n println();\n println(indent(2), usage, ln(1));\n\n // Print CLI description\n if (cliOptions.description) {\n printTitle(\"Description\");\n println();\n println(indent(2), c.description(cliOptions.description), ln(1));\n }\n\n let longest = 0;\n\n // Prepare CLI options\n const [optionsToPrint, longestOptionIndent] = prepareOptionsToPrint(cliOptions.options);\n if (longestOptionIndent > longest) longest = longestOptionIndent;\n\n // Prepare CLI commands\n const [commandsToPrint, longestSubcommandIndent] = prepareCommandsToPrint(subcommands);\n if (longestSubcommandIndent > longest) longest = longestSubcommandIndent;\n\n // Prepare CLI arguments\n const [argsToPrint, longestArgIndent] = prepareArgumentsToPrint(cliOptions.arguments);\n if (longestArgIndent > longest) longest = longestArgIndent;\n\n // Print CLI options\n printPreparedOptions(optionsToPrint, c, longest);\n\n // Print CLI commands\n printPreparedCommands(commandsToPrint, c, longest);\n\n // Print CLI arguments\n printPreparedArguments(argsToPrint, c, longest);\n\n // Print CLI example\n if (cliOptions.example) {\n printTitle(\"Example\");\n println();\n const normalizeExample = cliOptions.example.replace(/\\n/g, \"\\n\" + indent(3));\n println(indent(2), c.example(normalizeExample), ln(1));\n }\n}\n\nfunction printSubcommandHelp(subcommand: Subcommand, printOptions: PrintHelpOpt = {}, cliName = \"\") {\n printOptions.colors ??= true;\n\n const noColors = new Proxy(colors, {\n get: () => {\n return (...str: string[]) => str.join(\" \");\n },\n });\n\n const c = printOptions.colors ? colors : noColors;\n\n if (printOptions.customColors) {\n Object.assign(c, printOptions.customColors);\n }\n\n /** Print a styled title */\n const printTitle = (title: string) => {\n print(c.title(` ${title.toUpperCase()} `));\n };\n\n // Print command usage\n const usage =\n subcommand.usage ??\n concat(\n c.punctuation(\"$\"),\n cliName,\n c.command(subcommand.name),\n subcommand.options?.length ? c.option(\"[options]\") : \"\",\n subcommand.arguments?.length || subcommand.allowPositional ? c.argument(\"<arguments>\") : \"\",\n );\n printTitle(\"Usage\");\n println();\n println(indent(2), usage, ln(1));\n\n // Print command description\n if (subcommand.description) {\n printTitle(\"Description\");\n println();\n const normalizeDesc = subcommand.description.replace(/\\n/g, \"\\n\" + indent(3));\n println(indent(2), c.description(normalizeDesc), ln(1));\n }\n\n let longest = 0;\n\n // Prepare command options\n const [optionsToPrint, longestOptionIndent] = prepareOptionsToPrint(subcommand.options);\n if (longestOptionIndent > longest) longest = longestOptionIndent;\n\n // Prepare command arguments\n const [argsToPrint, longestArgIndent] = prepareArgumentsToPrint(subcommand.arguments);\n if (longestArgIndent > longest) longest = longestArgIndent;\n\n // Print command options\n printPreparedOptions(optionsToPrint, c, longest);\n\n // Print command arguments\n printPreparedArguments(argsToPrint, c, longest);\n\n // Print command example\n if (subcommand.example) {\n printTitle(\"Example\");\n println();\n const normalizeExample = subcommand.example.replace(/\\n/g, \"\\n\" + indent(3));\n println(indent(2), c.example(normalizeExample), ln(1));\n }\n}\n\n// * Prepare\nfunction prepareOptionsToPrint(options: Option[] | undefined): [PreparedToPrint[], number] {\n if (!options || !options.length) return [[], 0];\n\n const optionsToPrint: PreparedToPrint[] = [];\n let longest = 0;\n\n for (const option of options) {\n const nameWithAliases = option.aliases ? [...option.aliases, option.name] : [option.name];\n const names = Array.from(new Set(nameWithAliases.map(name => transformOptionToArg(name)))).join(\", \");\n\n const defaultValue = getDefaultValueFromSchema(option.type);\n\n const placeholder = option.placeholder ?? \" \";\n optionsToPrint.push({\n names,\n placeholder,\n description: option.description ?? option.type.description ?? \"\",\n default: typeof defaultValue !== \"undefined\" ? `(default: ${JSON.stringify(defaultValue)})` : \"\",\n optional: option.type.isOptional() ? \"[optional]\" : \"\",\n example: option.example ?? \"\",\n });\n\n const optLength = names.length + placeholder.length;\n\n if (optLength > longest) longest = optLength;\n }\n\n return [optionsToPrint, longest];\n}\n\nfunction prepareCommandsToPrint(subcommands: Subcommand[] | undefined): [PreparedToPrint[], number] {\n if (!subcommands || !subcommands.length) return [[], 0];\n\n const commandsToPrint: PreparedToPrint[] = [];\n let longest = 0;\n\n for (const subcommand of subcommands) {\n const { name, aliases, description } = subcommand;\n const names = Array.from(new Set([...(aliases ?? []), name])).join(\", \");\n\n const placeholder =\n subcommand.placeholder ?? (subcommand.options ? \"[options]\" : subcommand.allowPositional ? \"<args>\" : \" \");\n\n commandsToPrint.push({ names, placeholder, description: description ?? \"\" });\n\n const cmdLength = names.length + placeholder.length;\n if (cmdLength > longest) longest = cmdLength;\n }\n\n return [commandsToPrint, longest];\n}\n\nfunction prepareArgumentsToPrint(args: Argument[] | undefined): [PreparedToPrint[], number] {\n if (!args || !args.length) return [[], 0];\n\n const argsToPrint: PreparedToPrint[] = [];\n let longest = 0;\n\n for (const arg of args) {\n const defaultValue = getDefaultValueFromSchema(arg.type);\n\n argsToPrint.push({\n names: arg.name,\n description: arg.description ?? \"\",\n default: typeof defaultValue !== \"undefined\" ? `(default: ${JSON.stringify(defaultValue)})` : \"\",\n optional: arg.type.isOptional() ? \"[optional]\" : \"\",\n example: arg.example ?? \"\",\n });\n\n const cmdLength = arg.name.length;\n if (cmdLength > longest) longest = cmdLength;\n }\n\n return [argsToPrint, longest];\n}\n\n// * Print\nfunction printPreparedOptions(optionsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {\n if (!optionsToPrint.length) return;\n\n print(c.title(\" OPTIONS \"));\n\n println();\n\n for (const { names, placeholder, description, example, optional, default: def } of optionsToPrint) {\n const optLength = names.length + (placeholder?.length ?? 0);\n const spacing = longest + 1 - optLength;\n const normalizeDesc = description.replace(/\\n/g, \"\\n\" + indent(longest + 7) + c.punctuation(\"└\"));\n\n const coloredNames = names\n .split(/(,)/)\n .map(name => (name === \",\" ? c.punctuation(name) : c.option(name)))\n .join(\"\");\n\n println(\n indent(2),\n coloredNames,\n c.placeholder(placeholder),\n indent(spacing),\n c.description(normalizeDesc),\n def ? c.default(def) : c.optional(optional),\n );\n\n if (example) {\n const normalizeExample = example.replace(/\\n/g, \"\\n\" + indent(longest + 17));\n println(indent(longest + 6), c.punctuation(\"└\") + c.exampleTitle(\"Example:\"), c.example(normalizeExample));\n }\n }\n\n println();\n}\n\nfunction printPreparedCommands(commandsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {\n if (!commandsToPrint.length) return;\n\n print(c.title(\" COMMANDS \"));\n\n println();\n\n for (const { names, placeholder, description } of commandsToPrint) {\n const optLength = names.length + (placeholder?.length ?? 0);\n const spacing = longest + 1 - optLength;\n const normalizeDesc = description.replace(/\\n/g, \"\\n\" + indent(longest + 7) + c.punctuation(\"└\"));\n\n const coloredNames = names\n .split(/(,)/)\n .map(name => (name === \",\" ? c.punctuation(name) : c.command(name)))\n .join(\"\");\n\n println(indent(2), coloredNames, c.placeholder(placeholder), indent(spacing), c.description(normalizeDesc));\n }\n\n println();\n}\n\nfunction printPreparedArguments(argsToPrint: PreparedToPrint[], c: typeof colors, longest: number) {\n if (!argsToPrint.length) return;\n\n print(c.title(\" ARGUMENTS \"));\n\n println();\n\n for (const { names, description, example, optional, default: def } of argsToPrint) {\n const spacing = longest + 2 - names.length;\n const normalizeDesc = description.replace(/\\n/g, \"\\n\" + indent(longest + 6) + c.punctuation(\"└\"));\n\n println(\n indent(2),\n c.argument(names),\n indent(spacing),\n c.description(normalizeDesc),\n def ? c.default(def) : c.optional(optional),\n );\n\n if (example) {\n const normalizeExample = example.replace(/\\n/g, \"\\n\" + indent(longest + 16));\n println(indent(longest + 5), c.punctuation(\"└\") + c.exampleTitle(\"Example:\"), c.example(normalizeExample));\n }\n }\n\n println();\n}\n\nexport const help = {\n printCliHelp,\n printSubcommandHelp,\n};\n"],"mappings":"0FAAA,IAAAA,MAAA,CAAAC,sBAAA,CAAAC,OAAA,WACA,IAAAC,MAAA,CAAAD,OAAA,eAAiH,SAAAD,uBAAAG,CAAA,SAAAA,CAAA,EAAAA,CAAA,CAAAC,UAAA,CAAAD,CAAA,EAAAE,OAAA,CAAAF,CAAA,GAIjH,KAAM,CAAAG,MAA2D,CAAG,CAClEC,KAAK,CAAEC,cAAK,CAACC,IAAI,CAACC,IAAI,CACtBC,WAAW,CAAEH,cAAK,CAACI,KAAK,CACxBP,OAAO,CAAEG,cAAK,CAACK,GAAG,CAACC,MAAM,CACzBC,QAAQ,CAAEP,cAAK,CAACK,GAAG,CAACC,MAAM,CAC1BE,YAAY,CAAER,cAAK,CAACS,MAAM,CAC1BC,OAAO,CAAEV,cAAK,CAACK,GAAG,CAClBM,OAAO,CAAEX,cAAK,CAACS,MAAM,CACrBG,MAAM,CAAEZ,cAAK,CAACa,IAAI,CAClBC,QAAQ,CAAEd,cAAK,CAACe,KAAK,CACrBC,WAAW,CAAEhB,cAAK,CAACiB,GAAG,CAAC,SAAS,CAAC,CACjCC,WAAW,CAAElB,cAAK,CAACI,KAAK,CAACC,GAC3B,CAAC,CAWD,QAAS,CAAAc,YAAYA,CAACC,MAA8B,CAAmC,IAAjC,CAAAC,YAA0B,CAAAC,SAAA,CAAAC,MAAA,IAAAD,SAAA,MAAAE,SAAA,CAAAF,SAAA,IAAG,CAAC,CAAC,CACnFD,YAAY,CAACvB,MAAM,GAAK,IAAI,CAE5B,KAAM,CAAA2B,QAAQ,CAAG,GAAI,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AC,CAAC,CACX,CAEO,KAAM,CAAA8C,IAAI,CAAAC,OAAA,CAAAD,IAAA,CAAG,CAClB5E,YAAY,CACZgD,mBACF,CAAC","ignoreList":[]}
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createCli=createCli;exports.createSubcommand=createSubcommand;Object.defineProperty(exports,"parse",{enumerable:true,get:function(){return _parser.parse;}});exports.printSubcommandHelp=exports.printCliHelp=void 0;Object.defineProperty(exports,"safeParse",{enumerable:true,get:function(){return _parser.safeParse;}});var _help=require("./help.js");var _parser=require("./parser.js");function createSubcommand(input){return Object.assign(input,{setAction:action=>input.action=action});}function createCli(input){return Object.assign(input,{setAction:action=>input.action=action});}const{printCliHelp,printSubcommandHelp}=_help.help;exports.printSubcommandHelp=printSubcommandHelp;exports.printCliHelp=printCliHelp;
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.createCli=createCli;exports.createOptions=createOptions;exports.createSubcommand=createSubcommand;Object.defineProperty(exports,"parse",{enumerable:true,get:function(){return _parser.parse;}});exports.printSubcommandHelp=exports.printCliHelp=void 0;Object.defineProperty(exports,"safeParse",{enumerable:true,get:function(){return _parser.safeParse;}});var _help=require("./help.js");var _parser=require("./parser.js");function createSubcommand(input){return Object.assign(input,{setAction:action=>input.action=action});}function createCli(input){return Object.assign(input,{setAction:action=>input.action=action});}function createOptions(options){return options;}const{printCliHelp,printSubcommandHelp}=_help.help;exports.printSubcommandHelp=printSubcommandHelp;exports.printCliHelp=printCliHelp;
@@ -1 +1 @@
1
- {"version":3,"names":["_help","require","_parser","createSubcommand","input","Object","assign","setAction","action","createCli","printCliHelp","printSubcommandHelp","help","exports"],"sourceRoot":"../../src","sources":["index.ts"],"sourcesContent":["import { help } from \"./help.js\";\nimport { parse, safeParse } from \"./parser.js\";\n\nimport type { ActionFn, Cli, Prettify, Subcommand, UnSafeParseResult } from \"./types.js\";\n\nfunction createSubcommand<const T extends Subcommand>(input: T & Subcommand): Prettify<T & ActionFn<T>> {\n return Object.assign(input, {\n setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),\n });\n}\n\nfunction createCli<const T extends Cli>(input: T & Cli): Prettify<T & ActionFn<T>> {\n return Object.assign(input, {\n setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),\n });\n}\n\nconst { printCliHelp, printSubcommandHelp } = help;\n\nexport { createCli, createSubcommand, parse, printCliHelp, printSubcommandHelp, safeParse };\n\nexport type * from \"./types.js\";\n"],"mappings":"0YAAA,IAAAA,KAAA,CAAAC,OAAA,cACA,IAAAC,OAAA,CAAAD,OAAA,gBAIA,QAAS,CAAAE,gBAAgBA,CAA6BC,KAAqB,CAA6B,CACtG,MAAO,CAAAC,MAAM,CAACC,MAAM,CAACF,KAAK,CAAE,CAC1BG,SAAS,CAAGC,MAA6C,EAAMJ,KAAK,CAACI,MAAM,CAAGA,MAChF,CAAC,CAAC,CACJ,CAEA,QAAS,CAAAC,SAASA,CAAsBL,KAAc,CAA6B,CACjF,MAAO,CAAAC,MAAM,CAACC,MAAM,CAACF,KAAK,CAAE,CAC1BG,SAAS,CAAGC,MAA6C,EAAMJ,KAAK,CAACI,MAAM,CAAGA,MAChF,CAAC,CAAC,CACJ,CAEA,KAAM,CAAEE,YAAY,CAAEC,mBAAoB,CAAC,CAAGC,UAAI,CAACC,OAAA,CAAAF,mBAAA,CAAAA,mBAAA,CAAAE,OAAA,CAAAH,YAAA,CAAAA,YAAA","ignoreList":[]}
1
+ {"version":3,"names":["_help","require","_parser","createSubcommand","input","Object","assign","setAction","action","createCli","createOptions","options","printCliHelp","printSubcommandHelp","help","exports"],"sourceRoot":"../../src","sources":["index.ts"],"sourcesContent":["import { help } from \"./help.js\";\nimport { parse, safeParse } from \"./parser.js\";\n\nimport type {\n ActionFn,\n CheckDuplicatedOptions,\n Cli,\n Option,\n Prettify,\n Subcommand,\n UnSafeParseResult,\n} from \"./types.js\";\n\nfunction createSubcommand<const T extends Subcommand>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>> {\n return Object.assign(input, {\n setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),\n });\n}\n\nfunction createCli<const T extends Cli>(input: CheckDuplicatedOptions<T>): Prettify<T & ActionFn<T>> {\n return Object.assign(input, {\n setAction: (action: (res: UnSafeParseResult<[T]>) => void) => (input.action = action),\n });\n}\n\nfunction createOptions<const T extends [Option, ...Option[]]>(options: T): T {\n return options;\n}\n\nconst { printCliHelp, printSubcommandHelp } = help;\n\nexport { createCli, createSubcommand, createOptions, parse, printCliHelp, printSubcommandHelp, safeParse };\n\nexport type * from \"./types.js\";\n"],"mappings":"8aAAA,IAAAA,KAAA,CAAAC,OAAA,cACA,IAAAC,OAAA,CAAAD,OAAA,gBAYA,QAAS,CAAAE,gBAAgBA,CAA6BC,KAAgC,CAA6B,CACjH,MAAO,CAAAC,MAAM,CAACC,MAAM,CAACF,KAAK,CAAE,CAC1BG,SAAS,CAAGC,MAA6C,EAAMJ,KAAK,CAACI,MAAM,CAAGA,MAChF,CAAC,CAAC,CACJ,CAEA,QAAS,CAAAC,SAASA,CAAsBL,KAAgC,CAA6B,CACnG,MAAO,CAAAC,MAAM,CAACC,MAAM,CAACF,KAAK,CAAE,CAC1BG,SAAS,CAAGC,MAA6C,EAAMJ,KAAK,CAACI,MAAM,CAAGA,MAChF,CAAC,CAAC,CACJ,CAEA,QAAS,CAAAE,aAAaA,CAAwCC,OAAU,CAAK,CAC3E,MAAO,CAAAA,OAAO,CAChB,CAEA,KAAM,CAAEC,YAAY,CAAEC,mBAAoB,CAAC,CAAGC,UAAI,CAACC,OAAA,CAAAF,mBAAA,CAAAA,mBAAA,CAAAE,OAAA,CAAAH,YAAA,CAAAA,YAAA","ignoreList":[]}
@@ -1 +1 @@
1
- "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.parse=parse;exports.safeParse=safeParse;var _help=require("./help.js");var _utils=require("./utils.js");function parse(argsv){for(var _len=arguments.length,params=new Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){params[_key-1]=arguments[_key];}const cliOptions="cliName"in params[0]?params[0]:{};const subcommandArr=params;const allSubcommands=new Set(subcommandArr.flatMap(c=>[c.name,...(c.aliases||[])]));argsv=(0,_utils.decoupleFlags)(argsv);const results={subcommand:undefined,printCliHelp(opt){_help.help.printCliHelp(params,opt);},printSubcommandHelp(subcommandStr,opt){const subcommand=subcommandArr.find(c=>c.name===subcommandStr);if(!subcommand)return console.error(`Cannot print help for subcommand "${subcommandStr}" as it does not exist`);_help.help.printSubcommandHelp(subcommand,opt,cliOptions.cliName);}};const addRawArg=(optionName,rawArg)=>{if(!results._info)results._info={};if(!results._info[optionName])results._info[optionName]=Object.create({});results._info[optionName].rawArg=rawArg;};const addRawValue=(optionName,rawValue)=>{if(!results._info)results._info={};if(!results._info[optionName])results._info[optionName]=Object.create({});results._info[optionName].rawValue=rawValue;};const addSource=(optionName,source)=>{if(!results._info)results._info={};if(!results._info[optionName])results._info[optionName]=Object.create({});results._info[optionName].source=source;};const fillOption=(optionName,value)=>{if(!results._info)results._info={};if(!results._info[optionName])results._info[optionName]=Object.create({});Object.assign(results._info[optionName],value);};for(let i=0;i<argsv.length;i++){const arg=argsv[i];if(i===0){results.subcommand=allSubcommands.has(arg)?arg:undefined;const subcommandProps=subcommandArr.find(c=>c.name===results.subcommand);if(subcommandProps?.allowPositional)results.positional=[];if(subcommandProps?.arguments?.length)results.arguments=[];if(results.subcommand)continue;}const argAndValue=arg.split("=").filter(Boolean);const argWithEquals=arg.includes("=");const argument=argAndValue[0];const argValue=argAndValue[1];if((0,_utils.isOptionArg)(argument)){if((0,_utils.isFlagArg)(argument)&&argWithEquals){throw new Error(`Flag arguments cannot be assigned using "=": "${arg}"`);}const subcommandProps=subcommandArr.find(c=>c.name===results.subcommand);if(!subcommandProps)throw new Error(`Unknown subcommand: "${results.subcommand}"`);if(!subcommandProps.options){const msg=!results.subcommand?"options are not allowed here":`subcommand "${results.subcommand}" does not allow options`;throw new Error(`Error: ${msg}: "${argument}"`);}const optionName=(0,_utils.transformArg)(argument);if(optionName in results)throw new Error(`Duplicate option: "${argument}"`);const isNegative=argument.startsWith("--no-");const option=subcommandProps.options.find(o=>{if(o.name===optionName)return true;if(isNegative&&(0,_utils.noName)(o.name)===optionName)return true;if(!o.aliases)return false;if(o.aliases.includes(optionName))return true;if(isNegative&&o.aliases.map(_utils.noName).includes(optionName))return true;return false;});if(!option){throw new Error(`Unknown option: "${argument}"`);}const isTypeBoolean=(0,_utils.isBooleanSchema)(option.type);const nextArg=argsv[i+1];let optionValue=argWithEquals?argValue:nextArg;if(isTypeBoolean){if(argWithEquals){const parsedBoolean=(0,_utils.stringToBoolean)(argValue);optionValue=isNegative?!parsedBoolean:parsedBoolean;}else{optionValue=!isNegative;}}if(typeof optionValue==="undefined"){throw new Error(`Expected a value for "${argument}" but got nothing`);}if(!argWithEquals&&(0,_utils.isOptionArg)(optionValue)){throw new Error(`Expected a value for "${argument}" but got an argument "${nextArg}"`);}const res=option.type.safeParse(optionValue);if(!res.success){throw new Error(`Invalid value "${optionValue}" for "${argument}": ${res.error.errors[0].message}`);}results[option.name]=res.data;addRawArg(option.name,argument);const rawVal=argWithEquals?argValue:isTypeBoolean?"":nextArg;addRawValue(option.name,rawVal);fillOption(option.name,option);if(!argWithEquals&&!isTypeBoolean)i++;continue;}const subcommandProps=subcommandArr.find(c=>c.name===results.subcommand);if(subcommandProps?.arguments?.length){if(!results.arguments)results.arguments=[];const currentArgCount=results.arguments.length;if(currentArgCount<subcommandProps.arguments.length){const argType=subcommandProps.arguments[currentArgCount].type;let argValue=arg;const isTypeBoolean=(0,_utils.isBooleanSchema)(argType);if(isTypeBoolean)argValue=(0,_utils.stringToBoolean)(argValue);const res=argType.safeParse(argValue);if(!res.success){throw new Error(`The ${(0,_utils.getOrdinalPlacement)(currentArgCount)} argument "${arg}" is invalid: ${res.error.errors[0].message}`);}results.arguments.push(res.data);continue;}}if(subcommandProps?.allowPositional){if(!results.positional)results.positional=[];results.positional.push(arg);continue;}const msg=!results.subcommand?"here":`for subcommand "${results.subcommand}"`;throw new Error(`Unexpected argument "${arg}": positional arguments are not allowed ${msg}`);}const subcommandProps=subcommandArr.find(c=>c.name===results.subcommand);if(subcommandProps?.options?.length){for(const option of subcommandProps.options){if(option.name in results){addSource(option.name,"cli");fillOption(option.name,option);continue;}if(option.type.isOptional()){const hasDefault=typeof option.type._def.defaultValue==="function";if(!hasDefault)continue;results[option.name]=option.type._def.defaultValue();addSource(option.name,"default");fillOption(option.name,option);continue;}throw new Error(`Missing required option: ${(0,_utils.transformOptionToArg)(option.name)}`);}}if(subcommandProps?.arguments?.length){const currentArgCount=results.arguments?.length??0;const subcommandArgCount=subcommandProps.arguments.length;if(currentArgCount<subcommandArgCount){for(let i=currentArgCount;i<subcommandArgCount;i++){const argumentType=subcommandProps.arguments[i].type;const hasDefault=typeof argumentType._def.defaultValue==="function";if(hasDefault&&results.arguments){results.arguments.push(argumentType._def.defaultValue());continue;}if(argumentType.isOptional())continue;throw new Error(`the ${(0,_utils.getOrdinalPlacement)(i)} argument is required: "${subcommandProps.arguments[i].name}"`);}}}if(subcommandProps?.action){subcommandProps.action(results);}return results;}function safeParse(argsv){for(var _len2=arguments.length,params=new Array(_len2>1?_len2-1:0),_key2=1;_key2<_len2;_key2++){params[_key2-1]=arguments[_key2];}const cliOptions="cliName"in params[0]?params[0]:{};const subcommandArr=params;const printCliHelp=opt=>_help.help.printCliHelp(params,opt);const printSubcommandHelp=(subcommandStr,opt)=>{const subcommand=subcommandArr.find(c=>c.name===subcommandStr);if(!subcommand)return console.error(`Cannot print help for subcommand "${subcommandStr}" as it does not exist`);_help.help.printSubcommandHelp(subcommand,opt,cliOptions.cliName);};try{const data=parse(argsv,...params);delete data.printCliHelp;delete data.printSubcommandHelp;return{success:true,data:data,printCliHelp,printSubcommandHelp};}catch(e){return{success:false,error:e,printCliHelp,printSubcommandHelp};}}
1
+ "use strict";Object.defineProperty(exports,"__esModule",{value:true});exports.parse=parse;exports.safeParse=safeParse;var _help=require("./help.js");var _utils=require("./utils.js");function parse(argsv){for(var _len=arguments.length,params=new Array(_len>1?_len-1:0),_key=1;_key<_len;_key++){params[_key-1]=arguments[_key];}const cliOptions="cliName"in params[0]?params[0]:{};const subcommandArr=params;const allSubcommands=new Set(subcommandArr.flatMap(c=>[c.name,...(c.aliases||[])]));argsv=(0,_utils.decoupleFlags)(argsv);const results={subcommand:undefined,printCliHelp(opt){_help.help.printCliHelp(params,opt);},printSubcommandHelp(subcommandStr,opt){const subcommand=subcommandArr.find(c=>{if(c.name===subcommandStr)return true;if(!subcommandStr)return false;if(!c.aliases?.length)return false;return c.aliases.includes(subcommandStr);});if(!subcommand)return console.error(`Cannot print help for subcommand "${subcommandStr}" as it does not exist`);_help.help.printSubcommandHelp(subcommand,opt,cliOptions.cliName);}};const GetSubcommandProps=function(){let cmd=arguments.length>0&&arguments[0]!==undefined?arguments[0]:results.subcommand;return subcommandArr.find(c=>{if(c.name===cmd)return true;if(!cmd)return false;if(!c.aliases?.length)return false;return c.aliases.includes(cmd);});};const addRawArg=(optionName,rawArg)=>{if(!results._info)results._info={};if(!results._info[optionName])results._info[optionName]=Object.create({});results._info[optionName].rawArg=rawArg;};const addRawValue=(optionName,rawValue)=>{if(!results._info)results._info={};if(!results._info[optionName])results._info[optionName]=Object.create({});results._info[optionName].rawValue=rawValue;};const addSource=(optionName,source)=>{if(!results._info)results._info={};if(!results._info[optionName])results._info[optionName]=Object.create({});results._info[optionName].source=source;};const fillOption=(optionName,value)=>{if(!results._info)results._info={};if(!results._info[optionName])results._info[optionName]=Object.create({});Object.assign(results._info[optionName],value);};for(let i=0;i<argsv.length;i++){const arg=argsv[i];if(i===0){results.subcommand=allSubcommands.has(arg)?arg:undefined;const subcommandProps=GetSubcommandProps();if(subcommandProps?.allowPositional)results.positional=[];if(subcommandProps?.arguments?.length)results.arguments=[];if(results.subcommand)continue;}const argAndValue=arg.split("=").filter(Boolean);const argWithEquals=arg.includes("=");const argument=argAndValue[0];const argValue=argAndValue[1];if((0,_utils.isOptionArg)(argument)){if((0,_utils.isFlagArg)(argument)&&argWithEquals){throw new Error(`Flag arguments cannot be assigned using "=": "${arg}"`);}const subcommandProps=GetSubcommandProps();if(!subcommandProps)throw new Error(`Unknown subcommand: "${results.subcommand}"`);if(!subcommandProps.options){const msg=!results.subcommand?"options are not allowed here":`subcommand "${results.subcommand}" does not allow options`;throw new Error(`Error: ${msg}: "${argument}"`);}const optionName=(0,_utils.transformArg)(argument);const isNegative=argument.startsWith("--no-");const option=subcommandProps.options.find(o=>{if(o.name===optionName)return true;if(isNegative&&(0,_utils.noName)(o.name)===optionName)return true;if(!o.aliases)return false;if(o.aliases.includes(optionName))return true;if(isNegative&&o.aliases.map(_utils.noName).includes(optionName))return true;return false;});if(!option){throw new Error(`Unknown option: "${argument}"`);}if(option.name in results){throw new Error(`Duplicated option: "${argument}"`);}const isTypeBoolean=(0,_utils.isBooleanSchema)(option.type);const nextArg=argsv[i+1];let optionValue=argWithEquals?argValue:nextArg;if(isTypeBoolean){if(argWithEquals){const parsedBoolean=(0,_utils.stringToBoolean)(argValue);optionValue=isNegative?!parsedBoolean:parsedBoolean;}else{optionValue=!isNegative;}}if(typeof optionValue==="undefined"){throw new Error(`Expected a value for "${argument}" but got nothing`);}if(!argWithEquals&&(0,_utils.isOptionArg)(optionValue)){throw new Error(`Expected a value for "${argument}" but got an argument "${nextArg}"`);}const res=option.type.safeParse(optionValue);if(!res.success){throw new Error(`Invalid value "${optionValue}" for "${argument}": ${res.error.errors[0].message}`);}results[option.name]=res.data;addRawArg(option.name,argument);const rawVal=argWithEquals?argValue:isTypeBoolean?"":nextArg;addRawValue(option.name,rawVal);fillOption(option.name,option);if(!argWithEquals&&!isTypeBoolean)i++;continue;}const subcommandProps=GetSubcommandProps();if(subcommandProps?.arguments?.length){if(!results.arguments)results.arguments=[];const currentArgCount=results.arguments.length;if(currentArgCount<subcommandProps.arguments.length){const argType=subcommandProps.arguments[currentArgCount].type;let argValue=arg;const isTypeBoolean=(0,_utils.isBooleanSchema)(argType);if(isTypeBoolean)argValue=(0,_utils.stringToBoolean)(argValue);const res=argType.safeParse(argValue);if(!res.success){throw new Error(`The ${(0,_utils.getOrdinalPlacement)(currentArgCount)} argument "${arg}" is invalid: ${res.error.errors[0].message}`);}results.arguments.push(res.data);continue;}}if(subcommandProps?.allowPositional){if(!results.positional)results.positional=[];results.positional.push(arg);continue;}const msg=!results.subcommand?"here":`for subcommand "${results.subcommand}"`;throw new Error(`Unexpected argument "${arg}": positional arguments are not allowed ${msg}`);}const subcommandProps=GetSubcommandProps();if(subcommandProps?.options?.length){for(const option of subcommandProps.options){if(option.name in results){addSource(option.name,"cli");fillOption(option.name,option);continue;}if(option.type.isOptional()){const hasDefault=typeof option.type._def.defaultValue==="function";if(!hasDefault)continue;results[option.name]=option.type._def.defaultValue();addSource(option.name,"default");fillOption(option.name,option);continue;}throw new Error(`Missing required option: ${(0,_utils.transformOptionToArg)(option.name)}`);}}if(subcommandProps?.arguments?.length){const currentArgCount=results.arguments?.length??0;const subcommandArgCount=subcommandProps.arguments.length;if(currentArgCount<subcommandArgCount){for(let i=currentArgCount;i<subcommandArgCount;i++){const argumentType=subcommandProps.arguments[i].type;const hasDefault=typeof argumentType._def.defaultValue==="function";if(hasDefault&&results.arguments){results.arguments.push(argumentType._def.defaultValue());continue;}if(argumentType.isOptional())continue;throw new Error(`the ${(0,_utils.getOrdinalPlacement)(i)} argument is required: "${subcommandProps.arguments[i].name}"`);}}}if(subcommandProps?.action){subcommandProps.action(results);}return results;}function safeParse(argsv){for(var _len2=arguments.length,params=new Array(_len2>1?_len2-1:0),_key2=1;_key2<_len2;_key2++){params[_key2-1]=arguments[_key2];}const cliOptions="cliName"in params[0]?params[0]:{};const subcommandArr=params;const printCliHelp=opt=>_help.help.printCliHelp(params,opt);const printSubcommandHelp=(subcommandStr,opt)=>{const subcommand=subcommandArr.find(c=>c.name===subcommandStr);if(!subcommand)return console.error(`Cannot print help for subcommand "${subcommandStr}" as it does not exist`);_help.help.printSubcommandHelp(subcommand,opt,cliOptions.cliName);};try{const data=parse(argsv,...params);delete data.printCliHelp;delete data.printSubcommandHelp;return{success:true,data:data,printCliHelp,printSubcommandHelp};}catch(e){return{success:false,error:e,printCliHelp,printSubcommandHelp};}}
@@ -1 +1 @@
1
- {"version":3,"names":["_help","require","_utils","parse","argsv","_len","arguments","length","params","Array","_key","cliOptions","subcommandArr","allSubcommands","Set","flatMap","c","name","aliases","decoupleFlags","results","subcommand","undefined","printCliHelp","opt","help","printSubcommandHelp","subcommandStr","find","console","error","cliName","addRawArg","optionName","rawArg","_info","Object","create","addRawValue","rawValue","addSource","source","fillOption","value","assign","i","arg","has","subcommandProps","allowPositional","positional","argAndValue","split","filter","Boolean","argWithEquals","includes","argument","argValue","isOptionArg","isFlagArg","Error","options","msg","transformArg","isNegative","startsWith","option","o","noName","map","isTypeBoolean","isBooleanSchema","type","nextArg","optionValue","parsedBoolean","stringToBoolean","res","safeParse","success","errors","message","data","rawVal","currentArgCount","argType","getOrdinalPlacement","push","isOptional","hasDefault","_def","defaultValue","transformOptionToArg","subcommandArgCount","argumentType","action","_len2","_key2","e"],"sourceRoot":"../../src","sources":["parser.ts"],"sourcesContent":["import { help } from \"./help.js\";\nimport {\n decoupleFlags,\n getOrdinalPlacement,\n isBooleanSchema,\n isFlagArg,\n isOptionArg,\n noName,\n stringToBoolean,\n transformArg,\n transformOptionToArg,\n} from \"./utils.js\";\n\nimport type {\n Cli,\n NoSubcommand,\n Option,\n PrintHelpOpt,\n SafeParseResult,\n Subcommand,\n UnSafeParseResult,\n} from \"./types.js\";\n\nexport function parse<T extends Subcommand[]>(argsv: string[], ...params: T): UnSafeParseResult<T>;\nexport function parse<T extends Subcommand[], U extends Cli>(\n argsv: string[],\n ...params: [U, ...T]\n): UnSafeParseResult<[...T, NoSubcommand & U]>;\n\nexport function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ...params: [U, ...T]) {\n const cliOptions = (\"cliName\" in params[0] ? params[0] : {}) as U;\n const subcommandArr = params as unknown as T;\n const allSubcommands = new Set<string>(subcommandArr.flatMap(c => [c.name, ...(c.aliases || [])]));\n\n // decouple flags E.g. `-rf` -> `-r, -f`\n argsv = decoupleFlags(argsv);\n\n type ResultObj = Record<string, unknown> & {\n subcommand: string | undefined;\n positional?: string[];\n arguments?: unknown[];\n _info?: Record<string, { rawArg?: string; rawValue?: string; source: \"cli\" | \"default\" }>;\n printCliHelp: (options?: PrintHelpOpt) => void;\n printSubcommandHelp: (subcommand: any, options?: PrintHelpOpt) => void;\n };\n\n const results: ResultObj = {\n subcommand: undefined,\n printCliHelp(opt) {\n help.printCliHelp(params, opt);\n },\n printSubcommandHelp(subcommandStr, opt) {\n const subcommand = subcommandArr.find(c => c.name === subcommandStr);\n if (!subcommand) return console.error(`Cannot print help for subcommand \"${subcommandStr}\" as it does not exist`);\n help.printSubcommandHelp(subcommand, opt, cliOptions.cliName);\n },\n };\n\n const addRawArg = (optionName: string, rawArg: string) => {\n if (!results._info) results._info = {};\n if (!results._info[optionName]) results._info[optionName] = Object.create({});\n results._info[optionName].rawArg = rawArg;\n };\n\n const addRawValue = (optionName: string, rawValue: string) => {\n if (!results._info) results._info = {};\n if (!results._info[optionName]) results._info[optionName] = Object.create({});\n results._info[optionName].rawValue = rawValue;\n };\n\n const addSource = (optionName: string, source: \"cli\" | \"default\") => {\n if (!results._info) results._info = {};\n if (!results._info[optionName]) results._info[optionName] = Object.create({});\n results._info[optionName].source = source;\n };\n\n const fillOption = (optionName: string, value: Option) => {\n if (!results._info) results._info = {};\n if (!results._info[optionName]) results._info[optionName] = Object.create({});\n Object.assign(results._info[optionName], value);\n };\n\n for (let i = 0; i < argsv.length; i++) {\n const arg = argsv[i];\n\n // * subcommand\n if (i === 0) {\n results.subcommand = allSubcommands.has(arg) ? arg : undefined;\n\n // add positional and arguments array\n const subcommandProps = subcommandArr.find(c => c.name === results.subcommand);\n if (subcommandProps?.allowPositional) results.positional = [];\n if (subcommandProps?.arguments?.length) results.arguments = [];\n\n if (results.subcommand) continue;\n }\n\n // * option\n const argAndValue = arg.split(\"=\").filter(Boolean);\n const argWithEquals = arg.includes(\"=\");\n const argument = argAndValue[0];\n const argValue: string | undefined = argAndValue[1];\n\n if (isOptionArg(argument)) {\n if (isFlagArg(argument) && argWithEquals) {\n throw new Error(`Flag arguments cannot be assigned using \"=\": \"${arg}\"`);\n }\n\n const subcommandProps = subcommandArr.find(c => c.name === results.subcommand);\n if (!subcommandProps) throw new Error(`Unknown subcommand: \"${results.subcommand}\"`);\n\n if (!subcommandProps.options) {\n const msg = !results.subcommand\n ? \"options are not allowed here\"\n : `subcommand \"${results.subcommand}\" does not allow options`;\n throw new Error(`Error: ${msg}: \"${argument}\"`);\n }\n\n const optionName = transformArg(argument);\n if (optionName in results) throw new Error(`Duplicate option: \"${argument}\"`);\n\n const isNegative = argument.startsWith(\"--no-\");\n\n const option = subcommandProps.options.find(o => {\n if (o.name === optionName) return true;\n if (isNegative && noName(o.name) === optionName) return true;\n\n if (!o.aliases) return false;\n if (o.aliases.includes(optionName)) return true;\n if (isNegative && o.aliases.map(noName).includes(optionName)) return true;\n\n return false;\n });\n\n if (!option) {\n throw new Error(`Unknown option: \"${argument}\"`);\n }\n\n const isTypeBoolean = isBooleanSchema(option.type);\n const nextArg = argsv[i + 1];\n\n let optionValue: string | boolean = argWithEquals ? argValue : nextArg;\n\n if (isTypeBoolean) {\n if (argWithEquals) {\n const parsedBoolean = stringToBoolean(argValue);\n optionValue = isNegative ? !parsedBoolean : parsedBoolean;\n } else {\n optionValue = !isNegative;\n }\n }\n\n if (typeof optionValue === \"undefined\") {\n throw new Error(`Expected a value for \"${argument}\" but got nothing`);\n }\n\n if (!argWithEquals && isOptionArg(optionValue)) {\n throw new Error(`Expected a value for \"${argument}\" but got an argument \"${nextArg}\"`);\n }\n\n const res = option.type.safeParse(optionValue);\n if (!res.success) {\n throw new Error(`Invalid value \"${optionValue}\" for \"${argument}\": ${res.error.errors[0].message}`);\n }\n\n results[option.name] = res.data;\n addRawArg(option.name, argument);\n const rawVal = argWithEquals ? argValue : isTypeBoolean ? \"\" : nextArg;\n addRawValue(option.name, rawVal);\n fillOption(option.name, option);\n\n if (!argWithEquals && !isTypeBoolean) i++;\n continue;\n }\n\n const subcommandProps = subcommandArr.find(c => c.name === results.subcommand);\n\n // * arguments\n if (subcommandProps?.arguments?.length) {\n if (!results.arguments) results.arguments = [];\n\n const currentArgCount = results.arguments.length;\n\n if (currentArgCount < subcommandProps.arguments.length) {\n const argType = subcommandProps.arguments[currentArgCount].type;\n\n let argValue: string | boolean = arg;\n const isTypeBoolean = isBooleanSchema(argType);\n if (isTypeBoolean) argValue = stringToBoolean(argValue);\n\n const res = argType.safeParse(argValue);\n if (!res.success) {\n throw new Error(\n `The ${getOrdinalPlacement(currentArgCount)} argument \"${arg}\" is invalid: ${res.error.errors[0].message}`,\n );\n }\n\n results.arguments.push(res.data);\n continue;\n }\n }\n\n // * positional\n if (subcommandProps?.allowPositional) {\n if (!results.positional) results.positional = [];\n results.positional.push(arg);\n continue;\n }\n\n const msg = !results.subcommand ? \"here\" : `for subcommand \"${results.subcommand}\"`;\n throw new Error(`Unexpected argument \"${arg}\": positional arguments are not allowed ${msg}`);\n }\n\n // check for missing options - set defaults - add _source\n const subcommandProps = subcommandArr.find(c => c.name === results.subcommand);\n if (subcommandProps?.options?.length) {\n for (const option of subcommandProps.options) {\n if (option.name in results) {\n addSource(option.name, \"cli\");\n fillOption(option.name, option);\n continue;\n }\n\n if (option.type.isOptional()) {\n const hasDefault = typeof option.type._def.defaultValue === \"function\";\n if (!hasDefault) continue;\n results[option.name] = option.type._def.defaultValue();\n addSource(option.name, \"default\");\n fillOption(option.name, option);\n continue;\n }\n\n throw new Error(`Missing required option: ${transformOptionToArg(option.name)}`);\n }\n }\n\n // check for arguments - set defaults\n if (subcommandProps?.arguments?.length) {\n const currentArgCount = results.arguments?.length ?? 0;\n const subcommandArgCount = subcommandProps.arguments.length;\n\n // missing arguments\n if (currentArgCount < subcommandArgCount) {\n for (let i = currentArgCount; i < subcommandArgCount; i++) {\n const argumentType = subcommandProps.arguments[i].type;\n const hasDefault = typeof argumentType._def.defaultValue === \"function\";\n if (hasDefault && results.arguments) {\n results.arguments.push(argumentType._def.defaultValue());\n continue;\n }\n\n if (argumentType.isOptional()) continue;\n\n throw new Error(`the ${getOrdinalPlacement(i)} argument is required: \"${subcommandProps.arguments[i].name}\"`);\n }\n }\n }\n\n if (subcommandProps?.action) {\n subcommandProps.action(results);\n }\n\n return results as UnSafeParseResult<[...T, NoSubcommand & U]>;\n}\n\nexport function safeParse<T extends Subcommand[]>(argsv: string[], ...params: T): SafeParseResult<T>;\nexport function safeParse<T extends Subcommand[], U extends Cli>(\n argsv: string[],\n ...params: [U, ...T]\n): SafeParseResult<[...T, NoSubcommand & U]>;\n\nexport function safeParse<T extends Subcommand[], U extends Cli>(argsv: string[], ...params: [U, ...T]) {\n const cliOptions = (\"cliName\" in params[0] ? params[0] : {}) as U;\n const subcommandArr = params as Subcommand[];\n\n const printCliHelp = (opt?: PrintHelpOpt) => help.printCliHelp(params, opt);\n const printSubcommandHelp = (subcommandStr: NonNullable<T[number][\"name\"]>, opt?: PrintHelpOpt) => {\n const subcommand = subcommandArr.find(c => c.name === subcommandStr);\n if (!subcommand) return console.error(`Cannot print help for subcommand \"${subcommandStr}\" as it does not exist`);\n help.printSubcommandHelp(subcommand, opt, cliOptions.cliName);\n };\n\n try {\n const data = parse(argsv, ...params);\n // @ts-expect-error error\n delete data.printCliHelp;\n // @ts-expect-error error\n delete data.printSubcommandHelp;\n\n return {\n success: true,\n data: data as Omit<typeof data, \"printCliHelp\" | \"printSubcommandHelp\">,\n printCliHelp,\n printSubcommandHelp,\n };\n } catch (e) {\n return { success: false, error: e as Error, printCliHelp, printSubcommandHelp };\n }\n}\n"],"mappings":"sHAAA,IAAAA,KAAA,CAAAC,OAAA,cACA,IAAAC,MAAA,CAAAD,OAAA,e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GhF,KAAK,CAACC,KAAK,CAAE,GAAGI,MAAM,CAAC,CAEpC,MAAO,CAAA2E,IAAI,CAAC5D,YAAY,CAExB,MAAO,CAAA4D,IAAI,CAACzD,mBAAmB,CAE/B,MAAO,CACLsD,OAAO,CAAE,IAAI,CACbG,IAAI,CAAEA,IAAiE,CACvE5D,YAAY,CACZG,mBACF,CAAC,CACH,CAAE,MAAOyE,CAAC,CAAE,CACV,MAAO,CAAEnB,OAAO,CAAE,KAAK,CAAElD,KAAK,CAAEqE,CAAU,CAAE5E,YAAY,CAAEG,mBAAoB,CAAC,CACjF,CACF","ignoreList":[]}
1
+ {"version":3,"names":["_help","require","_utils","parse","argsv","_len","arguments","length","params","Array","_key","cliOptions","subcommandArr","allSubcommands","Set","flatMap","c","name","aliases","decoupleFlags","results","subcommand","undefined","printCliHelp","opt","help","printSubcommandHelp","subcommandStr","find","includes","console","error","cliName","GetSubcommandProps","cmd","addRawArg","optionName","rawArg","_info","Object","create","addRawValue","rawValue","addSource","source","fillOption","value","assign","i","arg","has","subcommandProps","allowPositional","positional","argAndValue","split","filter","Boolean","argWithEquals","argument","argValue","isOptionArg","isFlagArg","Error","options","msg","transformArg","isNegative","startsWith","option","o","noName","map","isTypeBoolean","isBooleanSchema","type","nextArg","optionValue","parsedBoolean","stringToBoolean","res","safeParse","success","errors","message","data","rawVal","currentArgCount","argType","getOrdinalPlacement","push","isOptional","hasDefault","_def","defaultValue","transformOptionToArg","subcommandArgCount","argumentType","action","_len2","_key2","e"],"sourceRoot":"../../src","sources":["parser.ts"],"sourcesContent":["import { help } from \"./help.js\";\nimport {\n decoupleFlags,\n getOrdinalPlacement,\n isBooleanSchema,\n isFlagArg,\n isOptionArg,\n noName,\n stringToBoolean,\n transformArg,\n transformOptionToArg,\n} from \"./utils.js\";\n\nimport type {\n Cli,\n NoSubcommand,\n Option,\n PrintHelpOpt,\n SafeParseResult,\n Subcommand,\n UnSafeParseResult,\n} from \"./types.js\";\n\nexport function parse<T extends Subcommand[]>(argsv: string[], ...params: T): UnSafeParseResult<T>;\nexport function parse<T extends Subcommand[], U extends Cli>(\n argsv: string[],\n ...params: [U, ...T]\n): UnSafeParseResult<[...T, NoSubcommand & U]>;\n\nexport function parse<T extends Subcommand[], U extends Cli>(argsv: string[], ...params: [U, ...T]) {\n const cliOptions = (\"cliName\" in params[0] ? params[0] : {}) as U;\n const subcommandArr = params as unknown as T;\n const allSubcommands = new Set<string>(subcommandArr.flatMap(c => [c.name, ...(c.aliases || [])]));\n\n // decouple flags E.g. `-rf` -> `-r, -f`\n argsv = decoupleFlags(argsv);\n\n type ResultObj = Record<string, unknown> & {\n subcommand: string | undefined;\n positional?: string[];\n arguments?: unknown[];\n _info?: Record<string, { rawArg?: string; rawValue?: string; source: \"cli\" | \"default\" }>;\n printCliHelp: (options?: PrintHelpOpt) => void;\n printSubcommandHelp: (subcommand: any, options?: PrintHelpOpt) => void;\n };\n\n const results: ResultObj = {\n subcommand: undefined,\n printCliHelp(opt) {\n help.printCliHelp(params, opt);\n },\n printSubcommandHelp(subcommandStr, opt) {\n const subcommand = subcommandArr.find(c => {\n if (c.name === subcommandStr) return true;\n if (!subcommandStr) return false;\n if (!c.aliases?.length) return false;\n return c.aliases.includes(subcommandStr);\n });\n if (!subcommand) return console.error(`Cannot print help for subcommand \"${subcommandStr}\" as it does not exist`);\n help.printSubcommandHelp(subcommand, opt, cliOptions.cliName);\n },\n };\n\n /** - Get current subcommand props */\n const GetSubcommandProps = (cmd = results.subcommand) => {\n return subcommandArr.find(c => {\n if (c.name === cmd) return true;\n if (!cmd) return false;\n if (!c.aliases?.length) return false;\n return c.aliases.includes(cmd);\n });\n };\n\n const addRawArg = (optionName: string, rawArg: string) => {\n if (!results._info) results._info = {};\n if (!results._info[optionName]) results._info[optionName] = Object.create({});\n results._info[optionName].rawArg = rawArg;\n };\n\n const addRawValue = (optionName: string, rawValue: string) => {\n if (!results._info) results._info = {};\n if (!results._info[optionName]) results._info[optionName] = Object.create({});\n results._info[optionName].rawValue = rawValue;\n };\n\n const addSource = (optionName: string, source: \"cli\" | \"default\") => {\n if (!results._info) results._info = {};\n if (!results._info[optionName]) results._info[optionName] = Object.create({});\n results._info[optionName].source = source;\n };\n\n const fillOption = (optionName: string, value: Option) => {\n if (!results._info) results._info = {};\n if (!results._info[optionName]) results._info[optionName] = Object.create({});\n Object.assign(results._info[optionName], value);\n };\n\n for (let i = 0; i < argsv.length; i++) {\n const arg = argsv[i];\n\n // * subcommand\n if (i === 0) {\n results.subcommand = allSubcommands.has(arg) ? arg : undefined;\n\n // add positional and arguments array\n const subcommandProps = GetSubcommandProps();\n if (subcommandProps?.allowPositional) results.positional = [];\n if (subcommandProps?.arguments?.length) results.arguments = [];\n\n if (results.subcommand) continue;\n }\n\n // * option\n const argAndValue = arg.split(\"=\").filter(Boolean);\n const argWithEquals = arg.includes(\"=\");\n const argument = argAndValue[0];\n const argValue: string | undefined = argAndValue[1];\n\n if (isOptionArg(argument)) {\n if (isFlagArg(argument) && argWithEquals) {\n throw new Error(`Flag arguments cannot be assigned using \"=\": \"${arg}\"`);\n }\n\n const subcommandProps = GetSubcommandProps();\n if (!subcommandProps) throw new Error(`Unknown subcommand: \"${results.subcommand}\"`);\n\n if (!subcommandProps.options) {\n const msg = !results.subcommand\n ? \"options are not allowed here\"\n : `subcommand \"${results.subcommand}\" does not allow options`;\n throw new Error(`Error: ${msg}: \"${argument}\"`);\n }\n\n const optionName = transformArg(argument);\n const isNegative = argument.startsWith(\"--no-\");\n\n const option = subcommandProps.options.find(o => {\n if (o.name === optionName) return true;\n if (isNegative && noName(o.name) === optionName) return true;\n\n if (!o.aliases) return false;\n if (o.aliases.includes(optionName)) return true;\n if (isNegative && o.aliases.map(noName).includes(optionName)) return true;\n\n return false;\n });\n\n if (!option) {\n throw new Error(`Unknown option: \"${argument}\"`);\n }\n\n if (option.name in results) {\n throw new Error(`Duplicated option: \"${argument}\"`);\n }\n\n const isTypeBoolean = isBooleanSchema(option.type);\n const nextArg = argsv[i + 1];\n\n let optionValue: string | boolean = argWithEquals ? argValue : nextArg;\n\n if (isTypeBoolean) {\n if (argWithEquals) {\n const parsedBoolean = stringToBoolean(argValue);\n optionValue = isNegative ? !parsedBoolean : parsedBoolean;\n } else {\n optionValue = !isNegative;\n }\n }\n\n if (typeof optionValue === \"undefined\") {\n throw new Error(`Expected a value for \"${argument}\" but got nothing`);\n }\n\n if (!argWithEquals && isOptionArg(optionValue)) {\n throw new Error(`Expected a value for \"${argument}\" but got an argument \"${nextArg}\"`);\n }\n\n const res = option.type.safeParse(optionValue);\n if (!res.success) {\n throw new Error(`Invalid value \"${optionValue}\" for \"${argument}\": ${res.error.errors[0].message}`);\n }\n\n results[option.name] = res.data;\n addRawArg(option.name, argument);\n const rawVal = argWithEquals ? argValue : isTypeBoolean ? \"\" : nextArg;\n addRawValue(option.name, rawVal);\n fillOption(option.name, option);\n\n if (!argWithEquals && !isTypeBoolean) i++;\n continue;\n }\n\n const subcommandProps = GetSubcommandProps();\n\n // * arguments\n if (subcommandProps?.arguments?.length) {\n if (!results.arguments) results.arguments = [];\n\n const currentArgCount = results.arguments.length;\n\n if (currentArgCount < subcommandProps.arguments.length) {\n const argType = subcommandProps.arguments[currentArgCount].type;\n\n let argValue: string | boolean = arg;\n const isTypeBoolean = isBooleanSchema(argType);\n if (isTypeBoolean) argValue = stringToBoolean(argValue);\n\n const res = argType.safeParse(argValue);\n if (!res.success) {\n throw new Error(\n `The ${getOrdinalPlacement(currentArgCount)} argument \"${arg}\" is invalid: ${res.error.errors[0].message}`,\n );\n }\n\n results.arguments.push(res.data);\n continue;\n }\n }\n\n // * positional\n if (subcommandProps?.allowPositional) {\n if (!results.positional) results.positional = [];\n results.positional.push(arg);\n continue;\n }\n\n const msg = !results.subcommand ? \"here\" : `for subcommand \"${results.subcommand}\"`;\n throw new Error(`Unexpected argument \"${arg}\": positional arguments are not allowed ${msg}`);\n }\n\n // check for missing options - set defaults - add _source\n const subcommandProps = GetSubcommandProps();\n if (subcommandProps?.options?.length) {\n for (const option of subcommandProps.options) {\n if (option.name in results) {\n addSource(option.name, \"cli\");\n fillOption(option.name, option);\n continue;\n }\n\n if (option.type.isOptional()) {\n const hasDefault = typeof option.type._def.defaultValue === \"function\";\n if (!hasDefault) continue;\n results[option.name] = option.type._def.defaultValue();\n addSource(option.name, \"default\");\n fillOption(option.name, option);\n continue;\n }\n\n throw new Error(`Missing required option: ${transformOptionToArg(option.name)}`);\n }\n }\n\n // check for arguments - set defaults\n if (subcommandProps?.arguments?.length) {\n const currentArgCount = results.arguments?.length ?? 0;\n const subcommandArgCount = subcommandProps.arguments.length;\n\n // missing arguments\n if (currentArgCount < subcommandArgCount) {\n for (let i = currentArgCount; i < subcommandArgCount; i++) {\n const argumentType = subcommandProps.arguments[i].type;\n const hasDefault = typeof argumentType._def.defaultValue === \"function\";\n if (hasDefault && results.arguments) {\n results.arguments.push(argumentType._def.defaultValue());\n continue;\n }\n\n if (argumentType.isOptional()) continue;\n\n throw new Error(`the ${getOrdinalPlacement(i)} argument is required: \"${subcommandProps.arguments[i].name}\"`);\n }\n }\n }\n\n if (subcommandProps?.action) {\n subcommandProps.action(results);\n }\n\n return results as UnSafeParseResult<[...T, NoSubcommand & U]>;\n}\n\nexport function safeParse<T extends Subcommand[]>(argsv: string[], ...params: T): SafeParseResult<T>;\nexport function safeParse<T extends Subcommand[], U extends Cli>(\n argsv: string[],\n ...params: [U, ...T]\n): SafeParseResult<[...T, NoSubcommand & U]>;\n\nexport function safeParse<T extends Subcommand[], U extends Cli>(argsv: string[], ...params: [U, ...T]) {\n const cliOptions = (\"cliName\" in params[0] ? params[0] : {}) as U;\n const subcommandArr = params as Subcommand[];\n\n const printCliHelp = (opt?: PrintHelpOpt) => help.printCliHelp(params, opt);\n const printSubcommandHelp = (subcommandStr: NonNullable<T[number][\"name\"]>, opt?: PrintHelpOpt) => {\n const subcommand = subcommandArr.find(c => c.name === subcommandStr);\n if (!subcommand) return console.error(`Cannot print help for subcommand \"${subcommandStr}\" as it does not exist`);\n help.printSubcommandHelp(subcommand, opt, cliOptions.cliName);\n };\n\n try {\n const data = parse(argsv, ...params);\n // @ts-expect-error error\n delete data.printCliHelp;\n // @ts-expect-error error\n delete data.printSubcommandHelp;\n\n return {\n success: true,\n data: data as Omit<typeof data, \"printCliHelp\" | \"printSubcommandHelp\">,\n printCliHelp,\n printSubcommandHelp,\n };\n } catch (e) {\n return { success: false, error: e as Error, printCliHelp, printSubcommandHelp };\n }\n}\n"],"mappings":"sHAAA,IAAAA,KAAA,CAAAC,OAAA,cACA,IAAAC,MAAA,CAAAD,OAAA,eA4BO,QAAS,CAAAE,KAAKA,CAAwCC,KAAe,CAAwB,SAAAC,IAAA,CAAAC,SAAA,CAAAC,MAAA,CAAnBC,MAAM,KAAAC,KAAA,CAAAJ,IAAA,GAAAA,IAAA,MAAAK,IAAA,GAAAA,IAAA,CAAAL,IAAA,CAAAK,IAAA,IAANF,MAAM,CAAAE,IAAA,IAAAJ,SAAA,CAAAI,IAAA,GACrF,KAAM,CAAAC,UAAU,CAAI,SAAS,EAAI,CAAAH,MAAM,CAAC,CAAC,CAAC,CAAGA,MAAM,CAAC,CAAC,CAAC,CAAG,CAAC,CAAO,CACjE,KAAM,CAAAI,aAAa,CAAGJ,MAAsB,CAC5C,KAAM,CAAAK,cAAc,CAAG,GAAI,CAAAC,GAAG,CAASF,aAAa,CAACG,OAAO,CAACC,CAAC,EAAI,CAACA,CAAC,CAACC,IAAI,CAAE,IAAID,CAAC,CAACE,OAAO,EAAI,EAAE,CAAC,CAAC,CAAC,CAAC,CAGlGd,KAAK,CAAG,GAAAe,oBAAa,EAACf,KAAK,CAAC,CAW5B,KAAM,CAAAgB,OAAkB,CAAG,CACzBC,UAAU,CAAEC,SAAS,CACrBC,YAAYA,CAACC,GAAG,CAAE,CAChBC,UAAI,CAACF,YAAY,CAACf,MAAM,CAAEgB,GAAG,CAAC,CAChC,CAAC,CACDE,mBAAmBA,CAACC,aAAa,CAAEH,GAAG,CAAE,CACtC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jF,CACF","ignoreList":[]}
@@ -1 +1 @@
1
- import chalk from"chalk";import{concat,getDefaultValueFromSchema,indent,ln,print,println,transformOptionToArg}from"./utils.js";const colors={title:chalk.bold.blue,description:chalk.white,default:chalk.dim.italic,optional:chalk.dim.italic,exampleTitle:chalk.yellow,example:chalk.dim.italic,command:chalk.yellow,option:chalk.cyan,argument:chalk.green,placeholder:chalk.hex("#FF9800"),punctuation:chalk.white.dim};function printCliHelp(params){let printOptions=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};printOptions.colors??=true;const noColors=new Proxy(colors,{get:()=>{return function(){for(var _len=arguments.length,str=new Array(_len),_key=0;_key<_len;_key++){str[_key]=arguments[_key];}return str.join(" ");};}});const c=printOptions.colors?colors:noColors;if(printOptions.customColors){Object.assign(c,printOptions.customColors);}const isFirstParamCli="cliName"in params[0];const cliOptions=isFirstParamCli?params.shift():{};const subcommands=params;const printTitle=title=>{print(c.title(` ${title.toUpperCase()} `));};const cliName=cliOptions.cliName??"";const usage=cliOptions.usage??concat(c.punctuation("$"),cliName,subcommands.length?c.command("[command]"):"",cliOptions.options?.length?c.option("[options]"):"",cliOptions.arguments?.length||cliOptions.allowPositional?c.argument("<arguments>"):"");printTitle("Usage");println();println(indent(2),usage,ln(1));if(cliOptions.description){printTitle("Description");println();println(indent(2),c.description(cliOptions.description),ln(1));}let longest=0;const[optionsToPrint,longestOptionIndent]=prepareOptionsToPrint(cliOptions.options);if(longestOptionIndent>longest)longest=longestOptionIndent;const[commandsToPrint,longestSubcommandIndent]=prepareCommandsToPrint(subcommands);if(longestSubcommandIndent>longest)longest=longestSubcommandIndent;const[argsToPrint,longestArgIndent]=prepareArgumentsToPrint(cliOptions.arguments);if(longestArgIndent>longest)longest=longestArgIndent;printPreparedOptions(optionsToPrint,c,longest);printPreparedCommands(commandsToPrint,c,longest);printPreparedArguments(argsToPrint,c,longest);if(cliOptions.example){printTitle("Example");println();const normalizeExample=cliOptions.example.replace(/\n/g,"\n"+indent(3));println(indent(2),c.example(normalizeExample),ln(1));}}function printSubcommandHelp(subcommand){let printOptions=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};let cliName=arguments.length>2&&arguments[2]!==undefined?arguments[2]:"";printOptions.colors??=true;const noColors=new Proxy(colors,{get:()=>{return function(){for(var _len2=arguments.length,str=new Array(_len2),_key2=0;_key2<_len2;_key2++){str[_key2]=arguments[_key2];}return str.join(" ");};}});const c=printOptions.colors?colors:noColors;if(printOptions.customColors){Object.assign(c,printOptions.customColors);}const printTitle=title=>{print(c.title(` ${title.toUpperCase()} `));};const usage=concat(c.punctuation("$"),cliName,c.command(subcommand.name),subcommand.options?.length?c.option("[options]"):"",subcommand.arguments?.length||subcommand.allowPositional?c.argument("<arguments>"):"");printTitle("Usage");println();println(indent(2),usage,ln(1));if(subcommand.description){printTitle("Description");println();const normalizeDesc=subcommand.description.replace(/\n/g,"\n"+indent(3));println(indent(2),c.description(normalizeDesc),ln(1));}let longest=0;const[optionsToPrint,longestOptionIndent]=prepareOptionsToPrint(subcommand.options);if(longestOptionIndent>longest)longest=longestOptionIndent;const[argsToPrint,longestArgIndent]=prepareArgumentsToPrint(subcommand.arguments);if(longestArgIndent>longest)longest=longestArgIndent;printPreparedOptions(optionsToPrint,c,longest);printPreparedArguments(argsToPrint,c,longest);if(subcommand.example){printTitle("Example");println();const normalizeExample=subcommand.example.replace(/\n/g,"\n"+indent(3));println(indent(2),c.example(normalizeExample),ln(1));}}function prepareOptionsToPrint(options){if(!options||!options.length)return[[],0];const optionsToPrint=[];let longest=0;for(const option of options){const nameWithAliases=option.aliases?[...option.aliases,option.name]:[option.name];const names=Array.from(new Set(nameWithAliases.map(name=>transformOptionToArg(name)))).join(", ");const defaultValue=getDefaultValueFromSchema(option.type);const placeholder=option.placeholder??" ";optionsToPrint.push({names,placeholder,description:option.description??option.type.description??"",default:typeof defaultValue!=="undefined"?`(default: ${JSON.stringify(defaultValue)})`:"",optional:option.type.isOptional()?"[optional]":"",example:option.example??""});const optLength=names.length+placeholder.length;if(optLength>longest)longest=optLength;}return[optionsToPrint,longest];}function prepareCommandsToPrint(subcommands){if(!subcommands||!subcommands.length)return[[],0];const commandsToPrint=[];let longest=0;for(const subcommand of subcommands){const{name,aliases,description}=subcommand;const names=Array.from(new Set([...(aliases??[]),name])).join(", ");const placeholder=subcommand.placeholder??(subcommand.options?"[options]":subcommand.allowPositional?"<args>":" ");commandsToPrint.push({names,placeholder,description:description??""});const cmdLength=names.length+placeholder.length;if(cmdLength>longest)longest=cmdLength;}return[commandsToPrint,longest];}function prepareArgumentsToPrint(args){if(!args||!args.length)return[[],0];const argsToPrint=[];let longest=0;for(const arg of args){const defaultValue=getDefaultValueFromSchema(arg.type);argsToPrint.push({names:arg.name,description:arg.description??"",default:typeof defaultValue!=="undefined"?`(default: ${JSON.stringify(defaultValue)})`:"",optional:arg.type.isOptional()?"[optional]":"",example:arg.example??""});const cmdLength=arg.name.length;if(cmdLength>longest)longest=cmdLength;}return[argsToPrint,longest];}function printPreparedOptions(optionsToPrint,c,longest){if(!optionsToPrint.length)return;print(c.title(" OPTIONS "));println();for(const{names,placeholder,description,example,optional,default:def}of optionsToPrint){const optLength=names.length+(placeholder?.length??0);const spacing=longest+1-optLength;const normalizeDesc=description.replace(/\n/g,"\n"+indent(longest+7)+c.punctuation("└"));const coloredNames=names.split(/(,)/).map(name=>name===","?c.punctuation(name):c.option(name)).join("");println(indent(2),coloredNames,c.placeholder(placeholder),indent(spacing),c.description(normalizeDesc),def?c.default(def):c.optional(optional));if(example){const normalizeExample=example.replace(/\n/g,"\n"+indent(longest+17));println(indent(longest+6),c.punctuation("└")+c.exampleTitle("Example:"),c.example(normalizeExample));}}println();}function printPreparedCommands(commandsToPrint,c,longest){if(!commandsToPrint.length)return;print(c.title(" COMMANDS "));println();for(const{names,placeholder,description}of commandsToPrint){const optLength=names.length+(placeholder?.length??0);const spacing=longest+1-optLength;const normalizeDesc=description.replace(/\n/g,"\n"+indent(longest+7));const coloredNames=names.split(/(,)/).map(name=>name===","?c.punctuation(name):c.command(name)).join("");println(indent(2),coloredNames,c.placeholder(placeholder),indent(spacing),c.description(normalizeDesc));}println();}function printPreparedArguments(argsToPrint,c,longest){if(!argsToPrint.length)return;print(c.title(" ARGUMENTS "));println();for(const{names,description,example,optional,default:def}of argsToPrint){const spacing=longest+2-names.length;const normalizeDesc=description.replace(/\n/g,"\n"+indent(longest+6)+c.punctuation("└"));println(indent(2),c.argument(names),indent(spacing),c.description(normalizeDesc),def?c.default(def):c.optional(optional));if(example){const normalizeExample=example.replace(/\n/g,"\n"+indent(longest+16));println(indent(longest+5),c.punctuation("└")+c.exampleTitle("Example:"),c.example(normalizeExample));}}println();}export const help={printCliHelp,printSubcommandHelp};
1
+ import chalk from"chalk";import{concat,getDefaultValueFromSchema,indent,ln,print,println,transformOptionToArg}from"./utils.js";const colors={title:chalk.bold.blue,description:chalk.white,default:chalk.dim.italic,optional:chalk.dim.italic,exampleTitle:chalk.yellow,example:chalk.dim,command:chalk.yellow,option:chalk.cyan,argument:chalk.green,placeholder:chalk.hex("#FF9800"),punctuation:chalk.white.dim};function printCliHelp(params){let printOptions=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};printOptions.colors??=true;const noColors=new Proxy(colors,{get:()=>{return function(){for(var _len=arguments.length,str=new Array(_len),_key=0;_key<_len;_key++){str[_key]=arguments[_key];}return str.join(" ");};}});const c=printOptions.colors?colors:noColors;if(printOptions.customColors){Object.assign(c,printOptions.customColors);}const isFirstParamCli="cliName"in params[0];const cliOptions=isFirstParamCli?params.shift():{};const subcommands=params;const printTitle=title=>{print(c.title(` ${title.toUpperCase()} `));};const cliName=cliOptions.cliName??"";const usage=cliOptions.usage??concat(c.punctuation("$"),cliName,subcommands.length?c.command("[command]"):"",cliOptions.options?.length?c.option("[options]"):"",cliOptions.arguments?.length||cliOptions.allowPositional?c.argument("<arguments>"):"");printTitle("Usage");println();println(indent(2),usage,ln(1));if(cliOptions.description){printTitle("Description");println();println(indent(2),c.description(cliOptions.description),ln(1));}let longest=0;const[optionsToPrint,longestOptionIndent]=prepareOptionsToPrint(cliOptions.options);if(longestOptionIndent>longest)longest=longestOptionIndent;const[commandsToPrint,longestSubcommandIndent]=prepareCommandsToPrint(subcommands);if(longestSubcommandIndent>longest)longest=longestSubcommandIndent;const[argsToPrint,longestArgIndent]=prepareArgumentsToPrint(cliOptions.arguments);if(longestArgIndent>longest)longest=longestArgIndent;printPreparedOptions(optionsToPrint,c,longest);printPreparedCommands(commandsToPrint,c,longest);printPreparedArguments(argsToPrint,c,longest);if(cliOptions.example){printTitle("Example");println();const normalizeExample=cliOptions.example.replace(/\n/g,"\n"+indent(3));println(indent(2),c.example(normalizeExample),ln(1));}}function printSubcommandHelp(subcommand){let printOptions=arguments.length>1&&arguments[1]!==undefined?arguments[1]:{};let cliName=arguments.length>2&&arguments[2]!==undefined?arguments[2]:"";printOptions.colors??=true;const noColors=new Proxy(colors,{get:()=>{return function(){for(var _len2=arguments.length,str=new Array(_len2),_key2=0;_key2<_len2;_key2++){str[_key2]=arguments[_key2];}return str.join(" ");};}});const c=printOptions.colors?colors:noColors;if(printOptions.customColors){Object.assign(c,printOptions.customColors);}const printTitle=title=>{print(c.title(` ${title.toUpperCase()} `));};const usage=subcommand.usage??concat(c.punctuation("$"),cliName,c.command(subcommand.name),subcommand.options?.length?c.option("[options]"):"",subcommand.arguments?.length||subcommand.allowPositional?c.argument("<arguments>"):"");printTitle("Usage");println();println(indent(2),usage,ln(1));if(subcommand.description){printTitle("Description");println();const normalizeDesc=subcommand.description.replace(/\n/g,"\n"+indent(3));println(indent(2),c.description(normalizeDesc),ln(1));}let longest=0;const[optionsToPrint,longestOptionIndent]=prepareOptionsToPrint(subcommand.options);if(longestOptionIndent>longest)longest=longestOptionIndent;const[argsToPrint,longestArgIndent]=prepareArgumentsToPrint(subcommand.arguments);if(longestArgIndent>longest)longest=longestArgIndent;printPreparedOptions(optionsToPrint,c,longest);printPreparedArguments(argsToPrint,c,longest);if(subcommand.example){printTitle("Example");println();const normalizeExample=subcommand.example.replace(/\n/g,"\n"+indent(3));println(indent(2),c.example(normalizeExample),ln(1));}}function prepareOptionsToPrint(options){if(!options||!options.length)return[[],0];const optionsToPrint=[];let longest=0;for(const option of options){const nameWithAliases=option.aliases?[...option.aliases,option.name]:[option.name];const names=Array.from(new Set(nameWithAliases.map(name=>transformOptionToArg(name)))).join(", ");const defaultValue=getDefaultValueFromSchema(option.type);const placeholder=option.placeholder??" ";optionsToPrint.push({names,placeholder,description:option.description??option.type.description??"",default:typeof defaultValue!=="undefined"?`(default: ${JSON.stringify(defaultValue)})`:"",optional:option.type.isOptional()?"[optional]":"",example:option.example??""});const optLength=names.length+placeholder.length;if(optLength>longest)longest=optLength;}return[optionsToPrint,longest];}function prepareCommandsToPrint(subcommands){if(!subcommands||!subcommands.length)return[[],0];const commandsToPrint=[];let longest=0;for(const subcommand of subcommands){const{name,aliases,description}=subcommand;const names=Array.from(new Set([...(aliases??[]),name])).join(", ");const placeholder=subcommand.placeholder??(subcommand.options?"[options]":subcommand.allowPositional?"<args>":" ");commandsToPrint.push({names,placeholder,description:description??""});const cmdLength=names.length+placeholder.length;if(cmdLength>longest)longest=cmdLength;}return[commandsToPrint,longest];}function prepareArgumentsToPrint(args){if(!args||!args.length)return[[],0];const argsToPrint=[];let longest=0;for(const arg of args){const defaultValue=getDefaultValueFromSchema(arg.type);argsToPrint.push({names:arg.name,description:arg.description??"",default:typeof defaultValue!=="undefined"?`(default: ${JSON.stringify(defaultValue)})`:"",optional:arg.type.isOptional()?"[optional]":"",example:arg.example??""});const cmdLength=arg.name.length;if(cmdLength>longest)longest=cmdLength;}return[argsToPrint,longest];}function printPreparedOptions(optionsToPrint,c,longest){if(!optionsToPrint.length)return;print(c.title(" OPTIONS "));println();for(const{names,placeholder,description,example,optional,default:def}of optionsToPrint){const optLength=names.length+(placeholder?.length??0);const spacing=longest+1-optLength;const normalizeDesc=description.replace(/\n/g,"\n"+indent(longest+7)+c.punctuation("└"));const coloredNames=names.split(/(,)/).map(name=>name===","?c.punctuation(name):c.option(name)).join("");println(indent(2),coloredNames,c.placeholder(placeholder),indent(spacing),c.description(normalizeDesc),def?c.default(def):c.optional(optional));if(example){const normalizeExample=example.replace(/\n/g,"\n"+indent(longest+17));println(indent(longest+6),c.punctuation("└")+c.exampleTitle("Example:"),c.example(normalizeExample));}}println();}function printPreparedCommands(commandsToPrint,c,longest){if(!commandsToPrint.length)return;print(c.title(" COMMANDS "));println();for(const{names,placeholder,description}of commandsToPrint){const optLength=names.length+(placeholder?.length??0);const spacing=longest+1-optLength;const normalizeDesc=description.replace(/\n/g,"\n"+indent(longest+7)+c.punctuation("└"));const coloredNames=names.split(/(,)/).map(name=>name===","?c.punctuation(name):c.command(name)).join("");println(indent(2),coloredNames,c.placeholder(placeholder),indent(spacing),c.description(normalizeDesc));}println();}function printPreparedArguments(argsToPrint,c,longest){if(!argsToPrint.length)return;print(c.title(" ARGUMENTS "));println();for(const{names,description,example,optional,default:def}of argsToPrint){const spacing=longest+2-names.length;const normalizeDesc=description.replace(/\n/g,"\n"+indent(longest+6)+c.punctuation("└"));println(indent(2),c.argument(names),indent(spacing),c.description(normalizeDesc),def?c.default(def):c.optional(optional));if(example){const normalizeExample=example.replace(/\n/g,"\n"+indent(longest+16));println(indent(longest+5),c.punctuation("└")+c.exampleTitle("Example:"),c.example(normalizeExample));}}println();}export const help={printCliHelp,printSubcommandHelp};