@@ -2,7 +2,6 @@ import parser from 'yargs-parser';
22import { z , ZodError } from 'zod/v4' ;
33import type { Options as YargsOptions } from 'yargs-parser' ;
44import {
5- type CliOptions ,
65 CliOptionsSchema ,
76 processPositionalCliOptions ,
87 validateCliOptions ,
@@ -36,19 +35,19 @@ export const defaultParserOptions: Partial<YargsOptions> = {
3635
3736export type ParserOptions = Partial < YargsOptions > ;
3837
39- export function parseArgs < T > ( {
38+ export function parseArgs < T extends z . ZodObject > ( {
4039 args,
4140 schema,
4241 parserOptions,
4342} : {
4443 args : string [ ] ;
45- schema : z . ZodObject ;
44+ schema : T ;
4645 parserOptions ?: YargsOptions ;
4746} ) : {
4847 /** Parsed options from the schema, including replaced deprecated arguments. */
49- parsed : z . infer < typeof schema > & Omit < parser . Arguments , '_' > ;
48+ parsed : z . infer < T > & Omit < parser . Arguments , '_' > ;
5049 /** Record of used deprecated arguments which have been replaced. */
51- deprecated : Record < keyof z . infer < typeof schema > , T > ;
50+ deprecated : Record < string , keyof z . infer < T > > ;
5251 /** Positional arguments which were not parsed as options. */
5352 positional : parser . Arguments [ '_' ] ;
5453} {
@@ -69,8 +68,8 @@ export function parseArgs<T>({
6968 throw error ;
7069 }
7170
72- const allDeprecatedArgs = getDeprecatedArgsWithReplacement < T > ( schema ) ;
73- const usedDeprecatedArgs = { } as Record < keyof z . infer < typeof schema > , T > ;
71+ const allDeprecatedArgs = getDeprecatedArgsWithReplacement ( schema ) ;
72+ const usedDeprecatedArgs = { } as Record < string , keyof z . infer < typeof schema > > ;
7473
7574 for ( const deprecated of Object . keys ( allDeprecatedArgs ) ) {
7675 if ( deprecated in parsedArgs ) {
@@ -100,30 +99,21 @@ export function parseArgs<T>({
10099 }
101100
102101 return {
103- parsed : parsedArgs as T & Omit < parser . Arguments , '_' > ,
102+ parsed : parsedArgs as z . infer < T > & Omit < parser . Arguments , '_' > ,
104103 deprecated : usedDeprecatedArgs ,
105104 positional,
106105 } ;
107106}
108107
109- type ParsedCliOptions = CliOptions & {
110- smokeTests : boolean ;
111- perfTests : boolean ;
112- buildInfo : boolean ;
113- file ?: string [ ] ;
114- } ;
115-
116108/** Parses the arguments with special handling of mongosh CLI options fields. */
117- export function parseArgsWithCliOptions <
118- T extends CliOptions = ParsedCliOptions
119- > ( {
109+ export function parseArgsWithCliOptions < T extends z . ZodObject > ( {
120110 args,
121111 schema : schemaToExtend ,
122112 parserOptions,
123113} : {
124114 args : string [ ] ;
125115 /** Schema to extend the CLI options schema with. */
126- schema ?: z . ZodObject ;
116+ schema ?: T ;
127117 parserOptions ?: Partial < YargsOptions > ;
128118} ) : ReturnType < typeof parseArgs < T > > {
129119 const schema =
@@ -133,7 +123,7 @@ export function parseArgsWithCliOptions<
133123 ...schemaToExtend . shape ,
134124 } )
135125 : CliOptionsSchema ;
136- const { parsed, positional, deprecated } = parseArgs < T > ( {
126+ const { parsed, positional, deprecated } = parseArgs ( {
137127 args,
138128 schema,
139129 parserOptions,
@@ -147,7 +137,7 @@ export function parseArgsWithCliOptions<
147137 validateCliOptions ( processed ) ;
148138
149139 return {
150- parsed : processed ,
140+ parsed : processed as z . infer < T > & Omit < parser . Arguments , '_' > ,
151141 positional,
152142 deprecated,
153143 } ;
0 commit comments