-
Notifications
You must be signed in to change notification settings - Fork 83
chore: use zod for arg-parser MCP-298 #2589
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 7 commits
Commits
Show all changes
21 commits
Select commit
Hold shift + click to select a range
09c5f4c
chore: move parseCliArgs to args-parser
gagik 674866f
Apply suggestions from code review
gagik 132a48d
chore: try nodenext
gagik fafa0ed
chore: run reformat, fix build
gagik 0d5aeb5
chore: fix check
gagik dac9f72
chore: use zod schema for arg parser
gagik 5cf92e8
chore: separate mongosh logic, process and validate args separately
gagik 981a602
Merge branch 'main' of github.com:mongodb-js/mongosh into gagik/options
gagik 708932c
chore: fixups
gagik 42d6f1a
chore: make diffs better by keeping the rename
gagik 387925a
chore: cleanup browser behavior
gagik f7ff517
chore: let one pass a z.object
gagik 7455b83
chore: make types cleaner and more flexible
gagik d8732a2
chore: stronger type assertion, fix erasable syntax
gagik 64b3027
chore: use a while loop
gagik 8cec797
chore: reorganize
gagik 88c960f
Merge branch 'main' of github.com:mongodb-js/mongosh into gagik/options
gagik 3143a60
chore: add support for objects and nested fields
gagik fab8eac
chore: reorganize
gagik b59f469
chore: reorganize, fix check
gagik 2057c77
chore: improve and cleanup types
gagik 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import z from 'zod/v4'; | ||
|
|
||
| /** | ||
| * Registry for argument options metadata | ||
| */ | ||
| export const argMetadata = z.registry<ArgumentMetadata>(); | ||
|
|
||
| /** | ||
| * Metadata that can be used to define field's parsing behavior | ||
| */ | ||
| export type ArgumentMetadata = { | ||
| /** If set, sets this field as deprecated and replaces this field with the set field. */ | ||
| deprecationReplacement?: string; | ||
| /** If set, gets replaced with a differet field name (without deprecation) */ | ||
gagik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
gagik marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| replacement?: string; | ||
| /** Whether this argument is unsupported. Always throws an error if set to true. */ | ||
| unsupported?: boolean; | ||
| /** Aliases for this argument. */ | ||
| alias?: string[]; | ||
| }; | ||
|
|
||
| /** | ||
| * Extract metadata for a field using the custom registry | ||
| */ | ||
| export function getArgumentMetadata( | ||
| schema: z.ZodObject, | ||
| fieldName: string | ||
| ): ArgumentMetadata | undefined { | ||
| const fieldSchema = schema.shape[fieldName as keyof typeof schema.shape]; | ||
| if (!fieldSchema) { | ||
| return undefined; | ||
| } | ||
| return argMetadata.get(fieldSchema); | ||
| } | ||
|
|
||
| /** | ||
| * Maps deprecated arguments to their new counterparts, derived from schema metadata. | ||
| */ | ||
| export function getDeprecatedArgsWithReplacement<T>( | ||
| schema: z.ZodObject | ||
| ): Record<keyof z.infer<typeof schema>, T> { | ||
| const deprecated: Record<string, T> = {}; | ||
| for (const fieldName of Object.keys(schema.shape)) { | ||
| const meta = getArgumentMetadata(schema, fieldName); | ||
| if (meta?.deprecationReplacement) { | ||
| deprecated[fieldName] = meta.deprecationReplacement as T; | ||
| } | ||
| } | ||
| return deprecated; | ||
| } | ||
|
|
||
| /** | ||
| * Get list of unsupported arguments, derived from schema metadata. | ||
| */ | ||
| export function getUnsupportedArgs(schema: z.ZodObject): string[] { | ||
| const unsupported: string[] = []; | ||
| for (const fieldName of Object.keys(schema.shape)) { | ||
| const meta = getArgumentMetadata(schema, fieldName); | ||
| if (meta?.unsupported) { | ||
| unsupported.push(fieldName); | ||
| } | ||
| } | ||
| return unsupported; | ||
| } | ||
|
|
||
| export class UnknownCliArgumentError extends Error { | ||
| /** The argument that was not parsed. */ | ||
| readonly argument: string; | ||
| constructor(argument: string) { | ||
| super(`Unknown argument: ${argument}`); | ||
| this.name = 'UnknownCliArgumentError'; | ||
| this.argument = argument; | ||
| } | ||
| } | ||
|
|
||
| export class UnsupportedCliArgumentError extends Error { | ||
| /** The argument that was not supported. */ | ||
| readonly argument: string; | ||
| constructor(argument: string) { | ||
| super(`Unsupported argument: ${argument}`); | ||
| this.name = 'UnsupportedCliArgumentError'; | ||
| this.argument = argument; | ||
| } | ||
| } | ||
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.
Uh oh!
There was an error while loading. Please reload this page.