Skip to content

Commit 7455b83

Browse files
committed
chore: make types cleaner and more flexible
1 parent f7ff517 commit 7455b83

File tree

3 files changed

+33
-22
lines changed

3 files changed

+33
-22
lines changed

packages/arg-parser/src/arg-parser.spec.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1247,8 +1247,10 @@ describe('arg-parser', function () {
12471247
it('generates from arbitrary schema', function () {
12481248
const options = generateYargsOptionsFromSchema({
12491249
schema: testSchema,
1250-
configuration: {
1251-
'combine-arrays': true,
1250+
parserOptions: {
1251+
configuration: {
1252+
'combine-arrays': true,
1253+
},
12521254
},
12531255
});
12541256

packages/arg-parser/src/arg-parser.ts

Lines changed: 26 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -26,24 +26,31 @@ function unwrapType(type: unknown): unknown {
2626
return type;
2727
}
2828

29-
/**
30-
* Generate yargs-parser configuration from schema
31-
*/
32-
export function generateYargsOptionsFromSchema({
33-
schema,
34-
configuration = {
29+
export const defaultParserOptions: Partial<YargsOptions> = {
30+
configuration: {
3531
'camel-case-expansion': false,
3632
'unknown-options-as-args': true,
3733
'parse-positional-numbers': false,
3834
'parse-numbers': false,
3935
'greedy-arrays': false,
4036
'short-option-groups': false,
4137
},
38+
};
39+
40+
export type ParserOptions = Partial<YargsOptions>;
41+
42+
/**
43+
* Generate yargs-parser configuration from schema
44+
*/
45+
export function generateYargsOptionsFromSchema({
46+
schema,
47+
parserOptions = defaultParserOptions,
4248
}: {
4349
schema: z.ZodObject;
44-
configuration?: YargsOptions['configuration'];
50+
parserOptions?: Partial<YargsOptions>;
4551
}): YargsOptions {
4652
const options = {
53+
...parserOptions,
4754
string: <string[]>[],
4855
boolean: <string[]>[],
4956
array: <string[]>[],
@@ -132,10 +139,7 @@ export function generateYargsOptionsFromSchema({
132139
}
133140
}
134141

135-
return {
136-
...options,
137-
configuration,
138-
};
142+
return options;
139143
}
140144

141145
/**
@@ -157,11 +161,11 @@ export function getLocale(args: string[], env: any): string {
157161
export function parseArgs<T>({
158162
args,
159163
schema,
160-
parserConfiguration,
164+
parserOptions,
161165
}: {
162166
args: string[];
163167
schema: z.ZodObject;
164-
parserConfiguration?: YargsOptions['configuration'];
168+
parserOptions?: YargsOptions;
165169
}): {
166170
/** Parsed options from the schema, including replaced deprecated arguments. */
167171
parsed: T & Omit<parser.Arguments, '_'>;
@@ -172,7 +176,7 @@ export function parseArgs<T>({
172176
} {
173177
const options = generateYargsOptionsFromSchema({
174178
schema,
175-
configuration: parserConfiguration,
179+
parserOptions,
176180
});
177181

178182
const { _: positional, ...parsedArgs } = parser(args, options);
@@ -222,24 +226,29 @@ type ParsedCliOptions = CliOptions & {
222226
};
223227

224228
/** Parses the arguments with special handling of mongosh CLI options fields. */
225-
export function parseArgsWithCliOptions({
229+
export function parseArgsWithCliOptions<
230+
T extends CliOptions = ParsedCliOptions
231+
>({
226232
args,
227233
schema: schemaToExtend,
234+
parserOptions,
228235
}: {
229236
args: string[];
230237
/** Schema to extend the CLI options schema with. */
231238
schema?: z.ZodObject;
232-
}): ReturnType<typeof parseArgs<CliOptions>> {
239+
parserOptions?: Partial<YargsOptions>;
240+
}): ReturnType<typeof parseArgs<T>> {
233241
const schema =
234242
schemaToExtend !== undefined
235243
? z.object({
236244
...CliOptionsSchema.shape,
237245
...schemaToExtend.shape,
238246
})
239247
: CliOptionsSchema;
240-
const { parsed, positional, deprecated } = parseArgs<ParsedCliOptions>({
248+
const { parsed, positional, deprecated } = parseArgs<T>({
241249
args,
242250
schema,
251+
parserOptions,
243252
});
244253

245254
const processed = processPositionalCliOptions({

packages/arg-parser/src/cli-options.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -167,13 +167,13 @@ export type CliOptions = Omit<
167167
fileNames?: string[];
168168
};
169169

170-
export function processPositionalCliOptions({
170+
export function processPositionalCliOptions<T extends CliOptions>({
171171
parsed,
172172
positional,
173173
}: {
174-
parsed: CliOptions;
174+
parsed: T;
175175
positional: parser.Arguments['_'];
176-
}): CliOptions {
176+
}): T {
177177
const processed = { ...parsed };
178178
if (typeof positional[0] === 'string') {
179179
if (!processed.nodb && isConnectionSpecifier(positional[0])) {

0 commit comments

Comments
 (0)