Skip to content

Commit 234a363

Browse files
authored
feat: improve alias options (#1)
1 parent 027d59f commit 234a363

File tree

4 files changed

+19
-20
lines changed

4 files changed

+19
-20
lines changed

src/index.ts

+10-9
Original file line numberDiff line numberDiff line change
@@ -29,20 +29,21 @@ export function createArgs<T = Defaults>({
2929
_: [],
3030
}
3131

32-
const setArg = (arg: string, index: number) => {
32+
function _setArg(arg: string, index: number) {
3333
let value: boolean | string = true
3434
const argKey = isFlag(arg) ? arg.slice(2) : arg.slice(1)
3535
const argValue = argv[index + 1]
3636

3737
if (argValue && !argValue.startsWith('-')) value = argValue
3838

3939
if (argKey && !argKey.includes('=')) {
40-
if (alias?.[argKey]) {
41-
const _aliasKey = alias[argKey]
42-
43-
if (isString(_aliasKey)) args[_aliasKey] = value
44-
if (isArray(_aliasKey)) {
45-
for (const key of _aliasKey) args[key] = value
40+
if (alias) {
41+
for (const [aliasKey, aliasValue] of Object.entries(alias)) {
42+
if (aliasKey.includes(argKey) || aliasValue.includes(argKey)) {
43+
args[aliasKey] = value
44+
if (isString(aliasValue)) args[aliasValue] = value
45+
if (isArray(aliasValue)) for (const v of aliasValue) args[v] = value
46+
}
4647
}
4748
}
4849

@@ -52,9 +53,9 @@ export function createArgs<T = Defaults>({
5253

5354
for (const [index, arg] of argv.entries()) {
5455
// flags '--'
55-
if (isFlag(arg)) setArg(arg, index)
56+
if (isFlag(arg)) _setArg(arg, index)
5657
// aliases '-'
57-
else if (isAlias(arg)) setArg(arg, index)
58+
else if (isAlias(arg)) _setArg(arg, index)
5859
// unprefixed values
5960
else {
6061
const _arg = argv[index - 1]

src/types/index.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
// Auto-generated
2-
export * from '../index.js'
3-
41
export type Defaults = Record<string, unknown>
52

63
export type Args<T = Defaults> = T & {
@@ -11,3 +8,6 @@ export interface Options {
118
argv?: string[]
129
alias?: Record<string, string | string[]>
1310
}
11+
12+
// Auto-generated
13+
export * from '../index.js'

test/mix.test.ts

+2-4
Original file line numberDiff line numberDiff line change
@@ -10,13 +10,11 @@ test('args-mix', () => {
1010
}
1111

1212
// $ hello world --foo bar -baz -cli demo --fuz
13-
const args = createArgs<Args>({
13+
const argsMix = createArgs<Args>({
1414
argv: ['hello', 'world', '--foo', 'bar', '-baz', '-cli', 'demo', '--fuz'],
1515
})
1616

17-
console.log(args)
18-
19-
expect(args).toStrictEqual({
17+
expect(argsMix).toStrictEqual({
2018
_: ['hello', 'world'],
2119
foo: 'bar',
2220
baz: true,

test/options.test.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,16 +11,16 @@ test('args-options', () => {
1111
f?: boolean
1212
}
1313

14-
// $ --a value --d
15-
const argsFlags = createArgs<Args>({
16-
argv: ['--a', 'value', '--d'],
14+
// $ --c value --e
15+
const argsOptions = createArgs<Args>({
16+
argv: ['--c', 'value', '--e'],
1717
alias: {
1818
a: ['b', 'c'],
1919
d: ['e', 'f'],
2020
},
2121
})
2222

23-
expect(argsFlags).toStrictEqual({
23+
expect(argsOptions).toStrictEqual({
2424
_: [],
2525
a: 'value',
2626
b: 'value',

0 commit comments

Comments
 (0)