8000 fix: camelcase option name before setting default value by egoist · Pull Request #62 · cacjs/cac · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

fix: camelcase option name before setting default value #62

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jan 13, 2020
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
8 changes: 8 additions & 0 deletions examples/negated-option.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require('ts-node/register')
const cli = require('../src/index')()

cli.option('--no-clear-screen', 'Do not clear screen')

const parsed = cli.parse()

console.log(JSON.stringify(parsed, null, 2))
14 changes: 3 additions & 11 deletions src/CAC.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,7 @@ import Command, {
CommandExample
} from './Command'
import { OptionConfig } from './Option'
import {
getMriOptions,
camelcase,
setDotProp,
setByType,
getFileName
} from './utils'
import { getMriOptions, setDotProp, setByType, getFileName } from './utils'
import { processArgs } from './node'

interface ParsedArgv {
Expand Down Expand Up @@ -295,11 +289,9 @@ class CAC extends EventEmitter {
}
}

// Camelcase option names and set dot nested option values
// Set dot nested option values
for (const key of Object.keys(parsed)) {
const keys = key.split('.').map((v, i) => {
return i === 0 ? camelcase(v) : v
})
const keys = key.split('.')
setDotProp(options, keys, parsed[key])
setByType(options, transforms)
}
Expand Down
14 changes: 12 additions & 2 deletions src/Option.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { removeBrackets } from './utils'
import { removeBrackets, camelcase } from './utils'

interface OptionConfig {
default?: any
Expand Down Expand Up @@ -35,11 +35,21 @@ export default class Option {
this.negated = true
name = name.replace(/^no-/, '')
}

// Camelcase the option name
// Don't camelcase anything after the dot `.`
name = name
.split('.')
.map((v, i) => {
return i === 0 ? camelcase(v) : v
})
.join('.')

return name
})
.sort((a, b) => (a.length > b.length ? 1 : -1)) // Sort names

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

if (this.negated) {
Expand Down
13 changes: 12 additions & 1 deletion src/__test__/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,18 @@ test('double dashes', () => {
expect(options['--']).toEqual(['npm', 'test'])
})

test('negated optional validation', () => {
test('default value for negated option', () => {
const cli = cac()

cli.option('--no-clear-screen', 'no clear screen')
cli.option('--no-a-b, --no-c-d', 'desc')

const { options } = cli.parse(`node bin`.split(' '))

expect(options).toEqual({ '--': [], clearScreen: true, aB: true, cD: true })
})

test('negated option validation', () => {
const cli = cac()

cli.option('--config <config>', 'config file')
Expand Down
0