Skip to content

Commit

Permalink
fix: defaultHelp not called with flag value at runtime
Browse files Browse the repository at this point in the history
@W-12513575@
  • Loading branch information
peternhale committed Mar 29, 2023
1 parent 7896e2a commit 46e89c4
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 15 deletions.
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"name": "@oclif/core",
"description": "base library for oclif CLIs",
"version": "2.8.0",
"version": "2.8.1-beta.0",
"author": "Salesforce",
"bugs": "https://github.com/oclif/core/issues",
"dependencies": {
Expand Down Expand Up @@ -114,4 +114,4 @@
"pretest": "yarn build --noEmit && tsc -p test --noEmit --skipLibCheck"
},
"types": "lib/index.d.ts"
}
}
29 changes: 17 additions & 12 deletions src/parser/parse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -218,17 +218,6 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']

if (!flag) throw new CLIError(`Unexpected flag ${token.flag}`)

// if flag has defaultHelp, capture its value into metadata
if (Reflect.has(flag, 'defaultHelp')) {
try {
const defaultHelpProperty = Reflect.get(flag, 'defaultHelp')
const defaultHelp = (typeof defaultHelpProperty === 'function' ? await defaultHelpProperty({options: flag, flags, ...this.context}) : defaultHelpProperty)
this.metaData.flags[token.flag] = {...this.metaData.flags[token.flag], defaultHelp}
} catch {
// no-op
}
}

if (flag.type === 'boolean') {
if (token.input === `--no-${flag.name}`) {
flags[token.flag] = false
Expand Down Expand Up @@ -284,9 +273,25 @@ export class Parser<T extends ParserInput, TFlags extends OutputFlags<T['flags']

if (!(k in flags) && flag.default !== undefined) {
this.metaData.flags[k] = {...this.metaData.flags[k], setFromDefault: true}
const defaultValue = (typeof flag.default === 'function' ? await flag.default({options: flag, flags, ...this.context}) : flag.default)
const defaultValue = (typeof flag.default === 'function' ? await flag.default({
options: flag,
flags, ...this.context,
}) : flag.default)
flags[k] = defaultValue
}

if ((k in flags) && Reflect.has(flag, 'defaultHelp')) {
try {
const defaultHelpProperty = Reflect.get(flag, 'defaultHelp')
const defaultHelp = (typeof defaultHelpProperty === 'function' ? await defaultHelpProperty({
options: flag,
flags, ...this.context,
}) : defaultHelpProperty)
this.metaData.flags[k] = {...this.metaData.flags[k], defaultHelp}
} catch {
// no-op
}
}
}

return flags
Expand Down
2 changes: 1 addition & 1 deletion test/parser/parse.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -727,7 +727,7 @@ See more help with --help`)
expect.fail(`Should have thrown an error ${JSON.stringify(out)}`)
} catch (error) {
assert(error instanceof Error)
expect(error.message).to.include('--foo=bar cannot also be provided when using --bar')
expect(error.message).to.include('--foo=baz cannot also be provided when using --bar')
}
})
it('should error with exclusive flag violation and defaultHelp value', async () => {
Expand Down

0 comments on commit 46e89c4

Please sign in to comment.