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: move ctor for command help class to its own function #244

Merged
merged 3 commits into from
Sep 14, 2021
Merged
Show file tree
Hide file tree
Changes from 2 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
16 changes: 15 additions & 1 deletion src/help/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,21 @@ function getHelpSubject(args: string[], config: Interfaces.Config): string | und
}

export abstract class HelpBase extends HelpFormatter {
private argvCopy: string[] = []

constructor(config: Interfaces.Config, opts: Partial<Interfaces.HelpOptions> = {}) {
super(config, opts)
if (!config.topicSeparator) config.topicSeparator = ':' // back-support @oclif/config
}

public get originalArgv(): string[] {
RodEsp marked this conversation as resolved.
Show resolved Hide resolved
return this.argvCopy
}

public set originalArgv(argv: string[]) {
this.argvCopy = argv
}

/**
* Show help, used in multi-command CLIs
* @param args passed into your command, useful for determining which type of help to display
Expand Down Expand Up @@ -202,10 +212,14 @@ export class Help extends HelpBase {
command.id = command.id.replace(/:/g, this.config.topicSeparator)
command.aliases = command.aliases && command.aliases.map(a => a.replace(/:/g, this.config.topicSeparator))
}
const help = new this.CommandHelpClass(command, this.config, this.opts)
const help = this.getCommandHelpClass(command)
Comment on lines -205 to +207
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we really need a whole new method for this if this is the only place it's used?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@RodEsp This function will be overridden in the custom help class to allow the determination of short help.

return help.generate()
}

protected getCommandHelpClass(command: Interfaces.Command) {
return new this.CommandHelpClass(command, this.config, this.opts)
}

protected formatCommands(commands: Interfaces.Command[]): string {
if (commands.length === 0) return ''

Expand Down
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ export const versionAddition = (argv: string[], config?: Interfaces.Config): boo
}

export async function run(argv = process.argv.slice(2), options?: Interfaces.LoadOptions) {
const originalArgv = [...argv]
RodEsp marked this conversation as resolved.
Show resolved Hide resolved
// Handle the case when a file URL string or URL is passed in such as 'import.meta.url'; covert to file path.
if ((typeof options === 'string' && options.startsWith('file://')) || options instanceof URL) {
options = fileURLToPath(options)
Expand All @@ -54,6 +55,7 @@ export async function run(argv = process.argv.slice(2), options?: Interfaces.Loa
argv = argv.filter(arg => !getHelpFlagAdditions(config).includes(arg))
Copy link
Contributor Author

Choose a reason for hiding this comment

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

main.ts should not have any knowledge of what showHelp expects in argv, just pass the whole thing as is.

const Help = await loadHelpClass(config)
const help = new Help(config, config.pjson.helpOptions)
peternhale marked this conversation as resolved.
Show resolved Hide resolved
help.originalArgv = originalArgv
RodEsp marked this conversation as resolved.
Show resolved Hide resolved
await help.showHelp(argv)
return
}
Expand Down