Skip to content

Commit b1fbe18

Browse files
committed
fix: option with brackets should be set to string type
fix #34
1 parent 1ec6d5e commit b1fbe18

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

src/Option.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ interface OptionConfig {
77

88
export default class Option {
99
names: string[]
10-
isBoolean: boolean
11-
required: boolean
10+
isBoolean?: boolean
11+
// `required` will be a boolean for options with brackets
12+
required?: boolean
1213
config: OptionConfig
1314

1415
constructor(

src/__test__/index.test.ts

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import path from 'path'
22
import execa from 'execa'
3+
import cac from '..'
34

45
function fixture(file: string) {
56
return path.relative(
@@ -46,3 +47,18 @@ snapshotOutput({
4647
file: 'ignore-default-value.js',
4748
args: ['build']
4849
})
50+
51+
test('negated option', () => {
52+
const cli = cac()
53+
54+
cli.option('--foo [foo]', 'Set foo').option('--no-foo', 'Disable foo')
55+
56+
cli.option('--no-bar', 'Disable bar')
57+
58+
const { options } = cli.parse(['node', 'bin', '--foo', 'foo'])
59+
expect(options).toEqual({
60+
'--': [],
61+
foo: 'foo',
62+
bar: true
63+
})
64+
})

src/utils.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,12 @@ export const getMriOptions = (globalCommand: Command, subCommand?: Command) => {
6565
res[option.names[0]] = option.names.slice(1)
6666
}
6767
return res
68-
}, {})
68+
}, {}),
69+
string: options
70+
.filter(option => typeof option.required === 'boolean')
71+
.reduce((res: string[], option) => {
72+
return res.concat(option.names)
73+
}, [])
6974
}
7075
}
7176

0 commit comments

Comments
 (0)