Skip to content

Commit

Permalink
feat: improve alias options (#1)
Browse files Browse the repository at this point in the history
  • Loading branch information
ivodolenc authored Oct 4, 2023
1 parent 027d59f commit 234a363
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 20 deletions.
19 changes: 10 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,20 +29,21 @@ export function createArgs<T = Defaults>({
_: [],
}

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

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

if (argKey && !argKey.includes('=')) {
if (alias?.[argKey]) {
const _aliasKey = alias[argKey]

if (isString(_aliasKey)) args[_aliasKey] = value
if (isArray(_aliasKey)) {
for (const key of _aliasKey) args[key] = value
if (alias) {
for (const [aliasKey, aliasValue] of Object.entries(alias)) {
if (aliasKey.includes(argKey) || aliasValue.includes(argKey)) {
args[aliasKey] = value
if (isString(aliasValue)) args[aliasValue] = value
if (isArray(aliasValue)) for (const v of aliasValue) args[v] = value
}
}
}

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

for (const [index, arg] of argv.entries()) {
// flags '--'
if (isFlag(arg)) setArg(arg, index)
if (isFlag(arg)) _setArg(arg, index)
// aliases '-'
else if (isAlias(arg)) setArg(arg, index)
else if (isAlias(arg)) _setArg(arg, index)
// unprefixed values
else {
const _arg = argv[index - 1]
Expand Down
6 changes: 3 additions & 3 deletions src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
// Auto-generated
export * from '../index.js'

export type Defaults = Record<string, unknown>

export type Args<T = Defaults> = T & {
Expand All @@ -11,3 +8,6 @@ export interface Options {
argv?: string[]
alias?: Record<string, string | string[]>
}

// Auto-generated
export * from '../index.js'
6 changes: 2 additions & 4 deletions test/mix.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,11 @@ test('args-mix', () => {
}

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

console.log(args)

expect(args).toStrictEqual({
expect(argsMix).toStrictEqual({
_: ['hello', 'world'],
foo: 'bar',
baz: true,
Expand Down
8 changes: 4 additions & 4 deletions test/options.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,16 @@ test('args-options', () => {
f?: boolean
}

// $ --a value --d
const argsFlags = createArgs<Args>({
argv: ['--a', 'value', '--d'],
// $ --c value --e
const argsOptions = createArgs<Args>({
argv: ['--c', 'value', '--e'],
alias: {
a: ['b', 'c'],
d: ['e', 'f'],
},
})

expect(argsFlags).toStrictEqual({
expect(argsOptions).toStrictEqual({
_: [],
a: 'value',
b: 'value',
Expand Down

0 comments on commit 234a363

Please sign in to comment.