Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions lib/yargs-parser-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,11 +186,16 @@ export type FlagsKey = KeyOf<Omit<Flags, 'keys'>>;

export type ArrayFlagsKey = Extract<FlagsKey, 'bools' | 'strings' | 'numbers'>;

export interface DefaultValuesForType {
boolean: boolean;
string: string;
number: undefined;
array: any[];
export enum DefaultValuesForTypeKey {
BOOLEAN = 'boolean',
STRING = 'string',
NUMBER = 'number',
ARRAY = 'array',
}

export type DefaultValuesForTypeKey = KeyOf<DefaultValuesForType>;
export interface DefaultValuesForType {
[DefaultValuesForTypeKey.BOOLEAN]: boolean;
[DefaultValuesForTypeKey.STRING]: string;
[DefaultValuesForTypeKey.NUMBER]: undefined;
[DefaultValuesForTypeKey.ARRAY]: any[];
}
20 changes: 10 additions & 10 deletions lib/yargs-parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import type {
CoerceCallback,
Configuration,
DefaultValuesForType,
DefaultValuesForTypeKey,
DetailedArguments,
Dictionary,
Flag,
Expand All @@ -29,6 +28,7 @@ import type {
ValueOf,
YargsParserMixin
} from './yargs-parser-types.js'
import { DefaultValuesForTypeKey } from './yargs-parser-types.js'
import { camelCase, decamelize, looksLikeNumber } from './string-utils.js'

let mixin: YargsParserMixin
Expand Down Expand Up @@ -1002,22 +1002,22 @@ export class YargsParser {
// return a default value, given the type of a flag.,
function defaultForType<K extends DefaultValuesForTypeKey> (type: K): DefaultValuesForType[K] {
const def: DefaultValuesForType = {
boolean: true,
string: '',
number: undefined,
array: []
[DefaultValuesForTypeKey.BOOLEAN]: true,
[DefaultValuesForTypeKey.STRING]: '',
[DefaultValuesForTypeKey.NUMBER]: undefined,
[DefaultValuesForTypeKey.ARRAY]: []
}

return def[type]
}

// given a flag, enforce a default type.
function guessType (key: string): DefaultValuesForTypeKey {
let type: DefaultValuesForTypeKey = 'boolean'
if (checkAllAliases(key, flags.strings)) type = 'string'
else if (checkAllAliases(key, flags.numbers)) type = 'number'
else if (checkAllAliases(key, flags.bools)) type = 'boolean'
else if (checkAllAliases(key, flags.arrays)) type = 'array'
let type: DefaultValuesForTypeKey = DefaultValuesForTypeKey.BOOLEAN
if (checkAllAliases(key, flags.strings)) type = DefaultValuesForTypeKey.STRING
else if (checkAllAliases(key, flags.numbers)) type = DefaultValuesForTypeKey.NUMBER
else if (checkAllAliases(key, flags.bools)) type = DefaultValuesForTypeKey.BOOLEAN
else if (checkAllAliases(key, flags.arrays)) type = DefaultValuesForTypeKey.ARRAY
return type
}

Expand Down