@@ -36,6 +36,123 @@ export const defaultParserOptions: Partial<YargsOptions> = {
3636
3737export type ParserOptions = Partial < YargsOptions > ;
3838
39+ export function parseArgs < T > ( {
40+ args,
41+ schema,
42+ parserOptions,
43+ } : {
44+ args : string [ ] ;
45+ schema : z . ZodObject ;
46+ parserOptions ?: YargsOptions ;
47+ } ) : {
48+ /** Parsed options from the schema, including replaced deprecated arguments. */
49+ parsed : z . infer < typeof schema > & Omit < parser . Arguments , '_' > ;
50+ /** Record of used deprecated arguments which have been replaced. */
51+ deprecated : Record < keyof z . infer < typeof schema > , T > ;
52+ /** Positional arguments which were not parsed as options. */
53+ positional : parser . Arguments [ '_' ] ;
54+ } {
55+ const options = generateYargsOptionsFromSchema ( {
56+ schema,
57+ parserOptions,
58+ } ) ;
59+
60+ const { argv, error } = parser . detailed ( args , {
61+ ...options ,
62+ } ) ;
63+ const { _ : positional , ...parsedArgs } = argv ;
64+
65+ if ( error ) {
66+ if ( error instanceof ZodError ) {
67+ throw new InvalidArgumentError ( error . message ) ;
68+ }
69+ throw error ;
70+ }
71+
72+ const allDeprecatedArgs = getDeprecatedArgsWithReplacement < T > ( schema ) ;
73+ const usedDeprecatedArgs = { } as Record < keyof z . infer < typeof schema > , T > ;
74+
75+ for ( const deprecated of Object . keys ( allDeprecatedArgs ) ) {
76+ if ( deprecated in parsedArgs ) {
77+ const replacement = allDeprecatedArgs [ deprecated ] ;
78+
79+ // This is a complicated type scenario.
80+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
81+ ( parsedArgs as any ) [ replacement ] =
82+ parsedArgs [ deprecated as keyof typeof parsedArgs ] ;
83+ usedDeprecatedArgs [ deprecated ] = replacement ;
84+
85+ delete parsedArgs [ deprecated as keyof typeof parsedArgs ] ;
86+ }
87+ }
88+
89+ for ( const arg of positional ) {
90+ if ( typeof arg === 'string' && arg . startsWith ( '-' ) ) {
91+ throw new UnknownArgumentError ( arg ) ;
92+ }
93+ }
94+
95+ const unsupportedArgs = getUnsupportedArgs ( schema ) ;
96+ for ( const unsupported of unsupportedArgs ) {
97+ if ( unsupported in parsedArgs ) {
98+ throw new UnsupportedArgumentError ( unsupported ) ;
99+ }
100+ }
101+
102+ return {
103+ parsed : parsedArgs as T & Omit < parser . Arguments , '_' > ,
104+ deprecated : usedDeprecatedArgs ,
105+ positional,
106+ } ;
107+ }
108+
109+ type ParsedCliOptions = CliOptions & {
110+ smokeTests : boolean ;
111+ perfTests : boolean ;
112+ buildInfo : boolean ;
113+ file ?: string [ ] ;
114+ } ;
115+
116+ /** Parses the arguments with special handling of mongosh CLI options fields. */
117+ export function parseArgsWithCliOptions <
118+ T extends CliOptions = ParsedCliOptions
119+ > ( {
120+ args,
121+ schema : schemaToExtend ,
122+ parserOptions,
123+ } : {
124+ args : string [ ] ;
125+ /** Schema to extend the CLI options schema with. */
126+ schema ?: z . ZodObject ;
127+ parserOptions ?: Partial < YargsOptions > ;
128+ } ) : ReturnType < typeof parseArgs < T > > {
129+ const schema =
130+ schemaToExtend !== undefined
131+ ? z . object ( {
132+ ...CliOptionsSchema . shape ,
133+ ...schemaToExtend . shape ,
134+ } )
135+ : CliOptionsSchema ;
136+ const { parsed, positional, deprecated } = parseArgs < T > ( {
137+ args,
138+ schema,
139+ parserOptions,
140+ } ) ;
141+
142+ const processed = processPositionalCliOptions ( {
143+ parsed,
144+ positional,
145+ } ) ;
146+
147+ validateCliOptions ( processed ) ;
148+
149+ return {
150+ parsed : processed ,
151+ positional,
152+ deprecated,
153+ } ;
154+ }
155+
39156/**
40157 * Generate yargs-parser configuration from schema
41158 */
@@ -173,123 +290,6 @@ export function getLocale(args: string[], env: any): string {
173290 return lang ? lang . split ( '.' ) [ 0 ] : lang ;
174291}
175292
176- export function parseArgs < T > ( {
177- args,
178- schema,
179- parserOptions,
180- } : {
181- args : string [ ] ;
182- schema : z . ZodObject ;
183- parserOptions ?: YargsOptions ;
184- } ) : {
185- /** Parsed options from the schema, including replaced deprecated arguments. */
186- parsed : z . infer < typeof schema > & Omit < parser . Arguments , '_' > ;
187- /** Record of used deprecated arguments which have been replaced. */
188- deprecated : Record < keyof z . infer < typeof schema > , T > ;
189- /** Positional arguments which were not parsed as options. */
190- positional : parser . Arguments [ '_' ] ;
191- } {
192- const options = generateYargsOptionsFromSchema ( {
193- schema,
194- parserOptions,
195- } ) ;
196-
197- const { argv, error } = parser . detailed ( args , {
198- ...options ,
199- } ) ;
200- const { _ : positional , ...parsedArgs } = argv ;
201-
202- if ( error ) {
203- if ( error instanceof ZodError ) {
204- throw new InvalidArgumentError ( error . message ) ;
205- }
206- throw error ;
207- }
208-
209- const allDeprecatedArgs = getDeprecatedArgsWithReplacement < T > ( schema ) ;
210- const usedDeprecatedArgs = { } as Record < keyof z . infer < typeof schema > , T > ;
211-
212- for ( const deprecated of Object . keys ( allDeprecatedArgs ) ) {
213- if ( deprecated in parsedArgs ) {
214- const replacement = allDeprecatedArgs [ deprecated ] ;
215-
216- // This is a complicated type scenario.
217- // eslint-disable-next-line @typescript-eslint/no-explicit-any
218- ( parsedArgs as any ) [ replacement ] =
219- parsedArgs [ deprecated as keyof typeof parsedArgs ] ;
220- usedDeprecatedArgs [ deprecated ] = replacement ;
221-
222- delete parsedArgs [ deprecated as keyof typeof parsedArgs ] ;
223- }
224- }
225-
226- for ( const arg of positional ) {
227- if ( typeof arg === 'string' && arg . startsWith ( '-' ) ) {
228- throw new UnknownArgumentError ( arg ) ;
229- }
230- }
231-
232- const unsupportedArgs = getUnsupportedArgs ( schema ) ;
233- for ( const unsupported of unsupportedArgs ) {
234- if ( unsupported in parsedArgs ) {
235- throw new UnsupportedArgumentError ( unsupported ) ;
236- }
237- }
238-
239- return {
240- parsed : parsedArgs as T & Omit < parser . Arguments , '_' > ,
241- deprecated : usedDeprecatedArgs ,
242- positional,
243- } ;
244- }
245-
246- type ParsedCliOptions = CliOptions & {
247- smokeTests : boolean ;
248- perfTests : boolean ;
249- buildInfo : boolean ;
250- file ?: string [ ] ;
251- } ;
252-
253- /** Parses the arguments with special handling of mongosh CLI options fields. */
254- export function parseArgsWithCliOptions <
255- T extends CliOptions = ParsedCliOptions
256- > ( {
257- args,
258- schema : schemaToExtend ,
259- parserOptions,
260- } : {
261- args : string [ ] ;
262- /** Schema to extend the CLI options schema with. */
263- schema ?: z . ZodObject ;
264- parserOptions ?: Partial < YargsOptions > ;
265- } ) : ReturnType < typeof parseArgs < T > > {
266- const schema =
267- schemaToExtend !== undefined
268- ? z . object ( {
269- ...CliOptionsSchema . shape ,
270- ...schemaToExtend . shape ,
271- } )
272- : CliOptionsSchema ;
273- const { parsed, positional, deprecated } = parseArgs < T > ( {
274- args,
275- schema,
276- parserOptions,
277- } ) ;
278-
279- const processed = processPositionalCliOptions ( {
280- parsed,
281- positional,
282- } ) ;
283-
284- validateCliOptions ( processed ) ;
285-
286- return {
287- parsed : processed ,
288- positional,
289- deprecated,
290- } ;
291- }
292-
293293export { argMetadata , UnknownArgumentError , UnsupportedArgumentError } ;
294294export { type ArgumentMetadata } from './arg-metadata' ;
295295export { type CliOptions , CliOptionsSchema } from './cli-options' ;
0 commit comments