mirror of
https://github.com/versia-pub/server.git
synced 2026-03-13 05:49:16 +01:00
Make CLI more robust to missing commands, add new help flags
This commit is contained in:
parent
28c73bc62a
commit
e4ed7ff2be
2 changed files with 16 additions and 8 deletions
|
|
@ -106,12 +106,28 @@ export class CliBuilder {
|
|||
*/
|
||||
async processArgs(args: string[]) {
|
||||
const revelantArgs = this.getRelevantArgs(args);
|
||||
|
||||
// Handle "-h", "--help" and "help" commands as special cases
|
||||
if (revelantArgs.length === 1) {
|
||||
if (["-h", "--help", "help"].includes(revelantArgs[0])) {
|
||||
this.displayHelp();
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Find revelant command
|
||||
// Search for a command with as many categories matching args as possible
|
||||
const matchingCommands = this.commands.filter(command =>
|
||||
startsWithArray(revelantArgs, command.categories)
|
||||
);
|
||||
|
||||
if (matchingCommands.length === 0) {
|
||||
console.log(
|
||||
`Invalid command "${revelantArgs.join(" ")}". Please use the ${chalk.bold("help")} command to see a list of commands`
|
||||
);
|
||||
return 0;
|
||||
}
|
||||
|
||||
// Get command with largest category size
|
||||
const command = matchingCommands.reduce((prev, current) =>
|
||||
prev.categories.length > current.categories.length ? prev : current
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue