Skip to content
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

fix: default help behaves properly #678

Merged
merged 9 commits into from
Apr 10, 2023
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -114,4 +114,4 @@
"pretest": "yarn build --noEmit && tsc -p test --noEmit --skipLibCheck"
},
"types": "lib/index.d.ts"
}
}
31 changes: 19 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,11 +273,29 @@ 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
}
}

for (const k of Object.keys(this.input.flags)) {
if ((k in flags) && Reflect.has(this.input.flags[k], 'defaultHelp')) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could this be done in the previous for loop? both iterate over the keys of this.input.flags
https://github.com/oclif/core/blob/main/src/parser/parse.ts#L268-L290

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@cristiand391 When defaultHelp is called it is passed all resolved flag values (flags), which can only be done after all previous loops are finished.

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

return flags
}

Expand Down