-
Notifications
You must be signed in to change notification settings - Fork 83
chore(arg-parser): include zod to yargs conversion output in snapshot #2619
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -33,113 +33,122 @@ export const defaultParserOptions: Partial<YargsOptions> = { | |
| }, | ||
| }; | ||
|
|
||
| export type ParserOptions = Partial<YargsOptions>; | ||
|
|
||
| export function parseArgs<T extends z.ZodObject>({ | ||
| args, | ||
| schema, | ||
| parserOptions, | ||
| }: { | ||
| args: string[]; | ||
| schema: T; | ||
| parserOptions?: YargsOptions; | ||
| }): { | ||
| export type ParsedArgs<T extends z.ZodObject> = { | ||
| /** Parsed options from the schema, including replaced deprecated arguments. */ | ||
| parsed: z.infer<T> & Omit<parser.Arguments, '_'>; | ||
| /** Record of used deprecated arguments which have been replaced. */ | ||
| deprecated: Record<string, keyof z.infer<T>>; | ||
| /** Positional arguments which were not parsed as options. */ | ||
| positional: parser.Arguments['_']; | ||
| } { | ||
| }; | ||
|
|
||
| export type ArgsListOptions = { | ||
| /** List of arguments to parse. */ | ||
| args: string[]; | ||
| }; | ||
|
|
||
| export type ParserCreationOptions<T extends z.ZodObject> = { | ||
| schema: T; | ||
| parserOptions?: YargsOptions; | ||
| }; | ||
|
|
||
| export function createParseArgs<T extends z.ZodObject>({ | ||
| schema, | ||
| parserOptions, | ||
| }: ParserCreationOptions<T>): ({ args }: ArgsListOptions) => ParsedArgs<T> { | ||
| const options = generateYargsOptionsFromSchema({ | ||
| schema, | ||
| parserOptions, | ||
| }); | ||
|
|
||
| const { argv, error } = parser.detailed(args, { | ||
| ...options, | ||
| }); | ||
| const { _: positional, ...parsedArgs } = argv; | ||
| return ({ args }: { args: string[] }) => { | ||
|
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Recommend reviewing this with whitespace diffs turned off (https://github.com/mongodb-js/mongosh/pull/2619/changes?w=1) |
||
| const { argv, error } = parser.detailed(args, { | ||
| ...options, | ||
| }); | ||
| const { _: positional, ...parsedArgs } = argv; | ||
|
|
||
| if (error) { | ||
| if (error instanceof ZodError) { | ||
| throw new InvalidArgumentError(error.message); | ||
| if (error) { | ||
| if (error instanceof ZodError) { | ||
| throw new InvalidArgumentError(error.message); | ||
| } | ||
| throw error; | ||
| } | ||
| throw error; | ||
| } | ||
|
|
||
| const allDeprecatedArgs = getDeprecatedArgsWithReplacement(schema); | ||
| const usedDeprecatedArgs = {} as Record<string, keyof z.infer<typeof schema>>; | ||
| const allDeprecatedArgs = getDeprecatedArgsWithReplacement(schema); | ||
| const usedDeprecatedArgs = {} as Record< | ||
| string, | ||
| keyof z.infer<typeof schema> | ||
| >; | ||
|
|
||
| for (const deprecated of Object.keys(allDeprecatedArgs)) { | ||
| if (deprecated in parsedArgs) { | ||
| const replacement = allDeprecatedArgs[deprecated]; | ||
| for (const deprecated of Object.keys(allDeprecatedArgs)) { | ||
| if (deprecated in parsedArgs) { | ||
| const replacement = allDeprecatedArgs[deprecated]; | ||
|
|
||
| // This is a complicated type scenario. | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| (parsedArgs as any)[replacement] = | ||
| parsedArgs[deprecated as keyof typeof parsedArgs]; | ||
| usedDeprecatedArgs[deprecated] = replacement; | ||
| // This is a complicated type scenario. | ||
| // eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
| (parsedArgs as any)[replacement] = | ||
| parsedArgs[deprecated as keyof typeof parsedArgs]; | ||
| usedDeprecatedArgs[deprecated] = replacement; | ||
|
|
||
| delete parsedArgs[deprecated as keyof typeof parsedArgs]; | ||
| delete parsedArgs[deprecated as keyof typeof parsedArgs]; | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for (const arg of positional) { | ||
| if (typeof arg === 'string' && arg.startsWith('-')) { | ||
| throw new UnknownArgumentError(arg); | ||
| for (const arg of positional) { | ||
| if (typeof arg === 'string' && arg.startsWith('-')) { | ||
| throw new UnknownArgumentError(arg); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| const unsupportedArgs = getUnsupportedArgs(schema); | ||
| for (const unsupported of unsupportedArgs) { | ||
| if (unsupported in parsedArgs) { | ||
| throw new UnsupportedArgumentError(unsupported); | ||
| const unsupportedArgs = getUnsupportedArgs(schema); | ||
| for (const unsupported of unsupportedArgs) { | ||
| if (unsupported in parsedArgs) { | ||
| throw new UnsupportedArgumentError(unsupported); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| return { | ||
| parsed: parsedArgs as z.infer<T> & Omit<parser.Arguments, '_'>, | ||
| deprecated: usedDeprecatedArgs, | ||
| positional, | ||
| return { | ||
| parsed: parsedArgs as z.infer<T> & Omit<parser.Arguments, '_'>, | ||
| deprecated: usedDeprecatedArgs, | ||
| positional, | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
| /** Parses the arguments with special handling of mongosh CLI options fields. */ | ||
| export function parseArgsWithCliOptions<T extends z.ZodObject>({ | ||
| args, | ||
| export function createParseArgsWithCliOptions<T extends z.ZodObject>({ | ||
| schema: schemaToExtend, | ||
| parserOptions, | ||
| }: { | ||
| args: string[]; | ||
| /** Schema to extend the CLI options schema with. */ | ||
| schema?: T; | ||
| parserOptions?: Partial<YargsOptions>; | ||
| }): ReturnType<typeof parseArgs<T>> { | ||
| }: Partial<ParserCreationOptions<T>> = {}): ({ | ||
| args, | ||
| }: ArgsListOptions) => ParsedArgs<T> { | ||
| const schema = | ||
| schemaToExtend !== undefined | ||
| ? z.object({ | ||
| ...CliOptionsSchema.shape, | ||
| ...schemaToExtend.shape, | ||
| }) | ||
| : CliOptionsSchema; | ||
| const { parsed, positional, deprecated } = parseArgs({ | ||
| args, | ||
| const parser = createParseArgs({ | ||
| schema, | ||
| parserOptions, | ||
| }); | ||
|
|
||
| const processed = processPositionalCliOptions({ | ||
| parsed, | ||
| positional, | ||
| }); | ||
| return ({ args }: { args: string[] }) => { | ||
| const { parsed, positional, deprecated } = parser({ args }); | ||
|
|
||
| const processed = processPositionalCliOptions({ | ||
| parsed, | ||
| positional, | ||
| }); | ||
|
|
||
| validateCliOptions(processed); | ||
| validateCliOptions(processed); | ||
|
|
||
| return { | ||
| parsed: processed as z.infer<T> & Omit<parser.Arguments, '_'>, | ||
| positional, | ||
| deprecated, | ||
| return { | ||
| parsed: processed as z.infer<T> & Omit<parser.Arguments, '_'>, | ||
| positional, | ||
| deprecated, | ||
| }; | ||
| }; | ||
| } | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test helper functions call
createParseArgsandcreateParseArgsWithCliOptionson every test invocation, recreating the parser each time. Since these tests don't benefit from snapshot optimization and the approach differs from production usage (where the parser is created once), consider adding a comment explaining this test-only pattern to clarify why it differs from the production implementation in cli-repl/src/arg-parser.ts.