Skip to content

Commit 94f13f8

Browse files
committed
Enhance parseArgs with coerce and configuration options
1 parent 18922c9 commit 94f13f8

1 file changed

Lines changed: 37 additions & 20 deletions

File tree

registry/src/lib/parse-args.ts

Lines changed: 37 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -8,21 +8,19 @@ import yargsParser from '../external/yargs-parser'
88
* Yargs parser options interface.
99
*/
1010
interface YargsOptions {
11-
boolean?: string[]
12-
string?: string[]
13-
array?: string[]
14-
alias?: Record<string, string | string[]>
15-
default?: Record<string, unknown>
16-
'unknown-options-as-args'?: boolean
17-
'parse-numbers'?: boolean
18-
'parse-positional-numbers'?: boolean
19-
'boolean-negation'?: boolean
20-
'halt-at-non-option'?: boolean
21-
configuration?: {
22-
'strip-aliased'?: boolean
23-
'strip-dashed'?: boolean
24-
}
25-
strict?: boolean
11+
boolean?: string[] | undefined
12+
string?: string[] | undefined
13+
array?: string[] | undefined
14+
alias?: Record<string, string | string[]> | undefined
15+
default?: Record<string, unknown> | undefined
16+
coerce?: Record<string, (value: any) => any> | undefined
17+
'unknown-options-as-args'?: boolean | undefined
18+
'parse-numbers'?: boolean | undefined
19+
'parse-positional-numbers'?: boolean | undefined
20+
'boolean-negation'?: boolean | undefined
21+
'halt-at-non-option'?: boolean | undefined
22+
configuration?: Record<string, boolean | string> | undefined
23+
strict?: boolean | undefined
2624
}
2725

2826
/**
@@ -38,13 +36,15 @@ interface YargsArguments extends Record<string, unknown> {
3836
*/
3937
export interface ParseArgsOptionsConfig {
4038
// Whether the option accepts multiple values (array).
41-
multiple?: boolean
39+
multiple?: boolean | undefined
4240
// Short alias for the option (single character).
43-
short?: string
41+
short?: string | undefined
4442
// Type of the option value.
45-
type?: 'boolean' | 'string'
43+
type?: 'boolean' | 'string' | undefined
4644
// Default value for the option.
47-
default?: unknown
45+
default?: unknown | undefined
46+
// Transform function to coerce parsed values.
47+
coerce?: (value: any) => any | undefined
4848
}
4949

5050
/**
@@ -63,6 +63,8 @@ export interface ParseArgsConfig {
6363
allowPositionals?: boolean | undefined
6464
// Whether to allow negative numbers as option values.
6565
allowNegative?: boolean | undefined
66+
// Advanced yargs-parser configuration passthrough.
67+
configuration?: Record<string, boolean | string> | undefined
6668
}
6769

6870
/**
@@ -88,6 +90,7 @@ export function parseArgs<T = Record<string, unknown>>(
8890
allowNegative = false,
8991
allowPositionals = true,
9092
args = process.argv.slice(2),
93+
configuration,
9194
options = {},
9295
strict = true,
9396
} = config
@@ -99,20 +102,29 @@ export function parseArgs<T = Record<string, unknown>>(
99102
array: [],
100103
alias: {},
101104
default: {},
105+
coerce: {},
102106
'unknown-options-as-args': !strict,
103107
'parse-numbers': false,
104108
'parse-positional-numbers': false,
105109
'boolean-negation': !allowNegative,
106110
'halt-at-non-option': !allowPositionals,
107111
configuration: {
112+
'camel-case-expansion': false,
108113
'strip-aliased': false,
109114
'strip-dashed': true,
115+
...configuration,
110116
},
111117
}
112118

113119
// Process each option configuration.
114120
for (const { 0: key, 1: optionConfig } of Object.entries(options)) {
115-
const { default: defaultValue, multiple, short, type } = optionConfig
121+
const {
122+
coerce,
123+
default: defaultValue,
124+
multiple,
125+
short,
126+
type,
127+
} = optionConfig
116128

117129
// Set the option type.
118130
if (type === 'boolean') {
@@ -135,6 +147,11 @@ export function parseArgs<T = Record<string, unknown>>(
135147
if (defaultValue !== undefined) {
136148
yargsOptions.default![key] = defaultValue
137149
}
150+
151+
// Set coerce function.
152+
if (coerce) {
153+
yargsOptions.coerce![key] = coerce
154+
}
138155
}
139156

140157
// Parse the arguments.

0 commit comments

Comments
 (0)