Skip to content

Commit

Permalink
fix(help): fix generated help when using default()
Browse files Browse the repository at this point in the history
When using .default(), generated help using `--help` would only show the default command. WIth this
fix, all commands are shown.

fixes #196
  • Loading branch information
mattallty committed Aug 28, 2023
1 parent dd82761 commit 3eb6ca2
Showing 1 changed file with 27 additions and 1 deletion.
28 changes: 27 additions & 1 deletion src/program/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,8 @@ export class Program extends EventEmitter {
private _discoveryPath?: string
private _discoveredCommands?: Command[]

private argv: string[] = []

/**
* Number validator. Check that the value looks like a numeric one
* and cast the provided value to a javascript `Number`.
Expand Down Expand Up @@ -529,6 +531,13 @@ export class Program extends EventEmitter {
return this
}

/**
* Returns the raw argv array
*/
public getRawArgv() {
return this.argv
}

/**
* Run the program by parsing command line arguments.
* Caporal will automatically detect command line arguments from `process.argv` values,
Expand Down Expand Up @@ -561,6 +570,8 @@ export class Program extends EventEmitter {
}
}

this.argv = argv

/*
Search for the command from args, then, if a default command exists,
take it, otherwise take the command attached to the program,
Expand Down Expand Up @@ -591,7 +602,22 @@ export class Program extends EventEmitter {
* @param argv
*/
private async findCommand(argv: string[]): ReturnType<typeof findCommand> {
return (await findCommand(this, argv)) || this.defaultCommand || this._progCommand
const cmd = await findCommand(this, argv)
if (cmd) {
return cmd
}
/**
* If we've been asked for help (and only help) and there is no progCommand,
* so we should return undefined in order for the help command to display
* the global help.
* Fixes this particular use case: Fixes this particular use case:
* https://github.com/mattallty/Caporal.js/issues/196
*/
if (argv.every((arg) => arg === "--help") && !this._progCommand) {
return
}

return this.defaultCommand || this._progCommand
}

/**
Expand Down

0 comments on commit 3eb6ca2

Please sign in to comment.