Skip to content

Commit 64e22f1

Browse files
authored
fix: camelcase option name before setting default value (#62)
* fix: camelcase option name * add test * camelcase option alias names too
1 parent 607ce00 commit 64e22f1

File tree

4 files changed

+35
-14
lines changed

4 files changed

+35
-14
lines changed

examples/negated-option.js

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
require('ts-node/register')
2+
const cli = require('../src/index')()
3+
4+
cli.option('--no-clear-screen', 'Do not clear screen')
5+
6+
const parsed = cli.parse()
7+
8+
console.log(JSON.stringify(parsed, null, 2))

src/CAC.ts

+3-11
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,7 @@ import Command, {
77
CommandExample
88
} from './Command'
99
import { OptionConfig } from './Option'
10-
import {
11-
getMriOptions,
12-
camelcase,
13-
setDotProp,
14-
setByType,
15-
getFileName
16-
} from './utils'
10+
import { getMriOptions, setDotProp, setByType, getFileName } from './utils'
1711
import { processArgs } from './node'
1812

1913
interface ParsedArgv {
@@ -295,11 +289,9 @@ class CAC extends EventEmitter {
295289
}
296290
}
297291

298-
// Camelcase option names and set dot nested option values
292+
// Set dot nested option values
299293
for (const key of Object.keys(parsed)) {
300-
const keys = key.split('.').map((v, i) => {
301-
return i === 0 ? camelcase(v) : v
302-
})
294+
const keys = key.split('.')
303295
setDotProp(options, keys, parsed[key])
304296
setByType(options, transforms)
305297
}

src/Option.ts

+12-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { removeBrackets } from './utils'
1+
import { removeBrackets, camelcase } from './utils'
22

33
interface OptionConfig {
44
default?: any
@@ -35,11 +35,21 @@ export default class Option {
3535
this.negated = true
3636
name = name.replace(/^no-/, '')
3737
}
38+
39+
// Camelcase the option name
40+
// Don't camelcase anything after the dot `.`
41+
name = name
42+
.split('.')
43+
.map((v, i) => {
44+
return i === 0 ? camelcase(v) : v
45+
})
46+
.join('.')
47+
3848
return name
3949
})
4050
.sort((a, b) => (a.length > b.length ? 1 : -1)) // Sort names
4151

42-
// Use the longese name (last one) as actual option name
52+
// Use the longest name (last one) as actual option name
4353
this.name = this.names[this.names.length - 1]
4454

4555
if (this.negated) {

src/__test__/index.test.ts

+12-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,18 @@ test('double dashes', () => {
8080
expect(options['--']).toEqual(['npm', 'test'])
8181
})
8282

83-
test('negated optional validation', () => {
83+
test('default value for negated option', () => {
84+
const cli = cac()
85+
86+
cli.option('--no-clear-screen', 'no clear screen')
87+
cli.option('--no-a-b, --no-c-d', 'desc')
88+
89+
const { options } = cli.parse(`node bin`.split(' '))
90+
91+
expect(options).toEqual({ '--': [], clearScreen: true, aB: true, cD: true })
92+
})
93+
94+
test('negated option validation', () => {
8495
const cli = cac()
8596

8697
cli.option('--config <config>', 'config file')

0 commit comments

Comments
 (0)