Skip to content

Commit

Permalink
removing options prefix
Browse files Browse the repository at this point in the history
  • Loading branch information
NAMEER242 committed Feb 17, 2024
1 parent 6c6d25e commit 2d0e20b
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion src/commander.ts → src/commanders/commander.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ export class Commander {
}

private extractCommand(args: string[], command: string): string | null {
if (args.length === 0) {
return null;
}

const commandExists = command == args[0];

if (commandExists) {
Expand All @@ -64,11 +68,19 @@ export class Commander {
}
}

private removeCommandOptionPrefix(args: string) {
return args.replace(/^-+/g, '');
}

private getCommandOptions(args: string[], options: CommandOptions[]) {
if (args.length === 0 || options.length === 0) {
return {};
}
const finalOptions: Record<string, any> = {};
for (const option of options) {
if (option.option === args[0] || option.shortOption === args[0]) {
finalOptions[args[0]] = args[1];
const key = this.removeCommandOptionPrefix(args[0]);
finalOptions[key] = args[1];
args.splice(0, 2);
}
}
Expand All @@ -91,6 +103,9 @@ export class Commander {
}

private checkSubCommands(subCommands: Commander[], args: string[]) {
if (args.length === 0 || subCommands.length === 0) {
return true;
}
const isCommandOption = args[0].startsWith('-');
const isSubcommand =
subCommands.find((subCommand) => subCommand.command === args[0]) !==
Expand Down

0 comments on commit 2d0e20b

Please sign in to comment.