Skip to content
Open
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
15 changes: 14 additions & 1 deletion src/mri.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
export interface MriOptions {
alias: Record<string, string[]>
boolean: string[]
string: string[]
}

export interface MriResult {
Expand All @@ -21,10 +22,16 @@ function toVal(
key: string,
val: string | boolean,
booleans: string[],
strings: string[],
): void {
let next: string | number | boolean
if (typeof val === 'boolean') {
next = val
} else if (strings.includes(key)) {
// A string-typed option must keep its raw text: coercing "01234"
// to 1234 first would drop the leading zero before the caller's
// `type: [String]` transform ever runs.
next = val
} else if (booleans.includes(key)) {
if (val === 'false') {
next = false
Expand Down Expand Up @@ -68,6 +75,12 @@ export function mri(args: string[], opts: MriOptions): MriResult {
if (list) booleans.push(...list)
}

const strings = [...(opts.string || [])]
for (const name of opts.string || []) {
const list = aliases[name]
if (list) strings.push(...list)
}

for (let i = 0; i < args.length; i++) {
const arg = args[i]

Expand Down Expand Up @@ -103,7 +116,7 @@ export function mri(args: string[], opts: MriOptions): MriResult {
// where only the last one takes the value
const names = dashes === 2 ? [name] : name.split('')
for (const [idx, flag] of names.entries()) {
toVal(out, flag, idx + 1 < names.length || val, booleans)
toVal(out, flag, idx + 1 < names.length || val, booleans, strings)
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,11 @@ export function findAllBrackets(v: string): {
export function getMriOptions(options: Option[]): MriOptions {
// `alias` is looked up by user-provided option names inside mri,
// so it must not inherit from `Object.prototype`
const result: MriOptions = { alias: Object.create(null), boolean: [] }
const result: MriOptions = {
alias: Object.create(null),
boolean: [],
string: [],
}

for (const [index, option] of options.entries()) {
// We do not set default values in mri options
Expand All @@ -58,6 +62,12 @@ export function getMriOptions(options: Option[]): MriOptions {
if (option.names.length > 1) {
result.alias[option.names[0]] = option.names.slice(1)
}
// Set string: an option declared with `type: [String]` must keep
// its raw text — mri's numeric coercion would otherwise drop
// leading zeros (e.g. "01234" -> 1234) before the transform runs.
if (option.config.type?.[0] === String) {
result.string.push(...option.names)
}
// Set boolean
if (option.isBoolean) {
if (option.negated) {
Expand Down
15 changes: 15 additions & 0 deletions tests/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,21 @@ test('array types with transformFunction', () => {
expect(options.scale).toEqual(true)
})

test('String-typed option keeps leading zeros', () => {
const cli = cac()

cli
.command('', '')
.option('-c, --chars <chars>', 'Characters', { type: [String] })
.action(() => {})

const { options } = cli.parse(`node bin -c 01234`.split(' '))
expect(options.chars).toEqual(['01234'])
// The short alias keeps the raw value (setByType only transforms the
// canonical name); the leading zero must survive there too.
expect(options.c).toEqual('01234')
})

test('throw on unknown options', () => {
const cli = cac()

Expand Down