Skip to content

Commit

Permalink
add a bit more comments
Browse files Browse the repository at this point in the history
  • Loading branch information
bmesuere committed Jun 18, 2024
1 parent e09cc6d commit e8b6773
Showing 1 changed file with 15 additions and 0 deletions.
15 changes: 15 additions & 0 deletions lib/commands/base_command.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
import { Command } from "commander";
import { version } from '../../package.json';

/**
* This is a base class which provides a common interface for all commands.
* This is mostly used for testing purposes.
*
* Commands implementing this class should override the run method and call parseArguments
* at the beginning of the run method.
*/
export abstract class BaseCommand {
public program: Command;
args: string[] | undefined;
Expand All @@ -12,6 +19,10 @@ export abstract class BaseCommand {

abstract run(): void;

/**
* Create sets up the command line program. Implementing classes can add additional options.
* to this.program.
*/
create(options?: { exitOverride?: boolean, suppressOutput?: boolean }): Command {
const program = new Command();

Expand All @@ -20,6 +31,7 @@ export abstract class BaseCommand {
program.exitOverride(); // don't exit on error
}
if (options?.suppressOutput) {
// don't write anything to the console
program.configureOutput({
writeOut: () => { },
writeErr: () => { }
Expand All @@ -31,6 +43,9 @@ export abstract class BaseCommand {
return program;
}

/**
* This allows us to pass a custom list of strings as arguments to the command during testing.
*/
parseArguments() {
if (this.args) {
// custom arg parsing to be able to inject args for testing
Expand Down

0 comments on commit e8b6773

Please sign in to comment.