diff --git a/lib/commands/base_command.ts b/lib/commands/base_command.ts index 35c7b795..efa87e6a 100644 --- a/lib/commands/base_command.ts +++ b/lib/commands/base_command.ts @@ -10,16 +10,14 @@ import { readFileSync } from "fs"; */ export abstract class BaseCommand { public program: Command; - args: string[] | undefined; version: string; - constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean, args?: string[] }) { + constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean }) { this.version = JSON.parse(readFileSync(new URL("../../package.json", import.meta.url), "utf8")).version; this.program = this.create(options); - this.args = options?.args; } - abstract run(): void; + abstract run(args?: string[]): void; /** * Create sets up the command line program. Implementing classes can add additional options. @@ -47,10 +45,10 @@ export abstract class BaseCommand { /** * This allows us to pass a custom list of strings as arguments to the command during testing. */ - parseArguments() { - if (this.args) { + parseArguments(args?: string[]) { + if (args) { // custom arg parsing to be able to inject args for testing - this.program.parse(this.args, { from: "user" }); + this.program.parse(args, { from: "user" }); } else { this.program.parse(); } diff --git a/lib/commands/peptfilter.ts b/lib/commands/peptfilter.ts index f8435dc4..0bdb17ec 100644 --- a/lib/commands/peptfilter.ts +++ b/lib/commands/peptfilter.ts @@ -7,7 +7,7 @@ export class Peptfilter extends BaseCommand { The input should have one peptide per line. FASTA headers are preserved in the output, so that peptides remain bundled.`; - constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean, args?: string[] }) { + constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean }) { super(options); this.program @@ -24,8 +24,8 @@ The input should have one peptide per line. FASTA headers are preserved in the o * async iterators. This alternative implementation runs in 2.5 seconds. However, I decided that the async iterator implementation is * both more readable and more in line with the implementation of the other commands. */ - async run() { - this.parseArguments(); + async run(args?: string[]) { + this.parseArguments(args); const minLen = this.program.opts().minlen; const maxlen = this.program.opts().maxlen; const lacks = this.program.opts().lacks || []; diff --git a/lib/commands/prot2pept.ts b/lib/commands/prot2pept.ts index a64d862a..4c33ed7c 100644 --- a/lib/commands/prot2pept.ts +++ b/lib/commands/prot2pept.ts @@ -8,7 +8,7 @@ export class Prot2pept extends BaseCommand { The input should have either one protein sequence per line or contain a FASTA formatted list of protein sequences. FASTA headers are preserved in the output, so that peptides can be bundled per protein sequence. `; - constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean, args?: string[] }) { + constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean }) { super(options); this.program @@ -21,8 +21,8 @@ The input should have either one protein sequence per line or contain a FASTA fo * Performance note: Just as with peptfilter, this implementation can be made faster by using line events instead of * async iterators. */ - async run() { - this.parseArguments(); + async run(args?: string[]) { + this.parseArguments(args); let pattern; try { diff --git a/lib/commands/unipept.ts b/lib/commands/unipept.ts index 553a5fc2..24c1b29a 100644 --- a/lib/commands/unipept.ts +++ b/lib/commands/unipept.ts @@ -13,7 +13,7 @@ Subcommands that start with pept expect a list of tryptic peptides as input. Sub The command will give priority to the first way the input is passed, in the order as listed above. Text files and standard input should have one tryptic peptide or one NCBI Taxonomy Identifier per line.`; - constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean, args?: string[] }) { + constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean }) { super(options); this.program @@ -22,7 +22,7 @@ The command will give priority to the first way the input is passed, in the orde .addCommand(new Pept2lca().command); } - async run() { - this.parseArguments(); + async run(args?: string[]) { + this.parseArguments(args); } } diff --git a/lib/commands/uniprot.ts b/lib/commands/uniprot.ts index 11f5e57b..e0946251 100644 --- a/lib/commands/uniprot.ts +++ b/lib/commands/uniprot.ts @@ -16,7 +16,7 @@ The command will give priority to the first way UniProt Accession Numbers are pa The uniprot command yields just the protein sequences as a default, but can return several formats.`; - constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean, args?: string[] }) { + constructor(options?: { exitOverride?: boolean, suppressOutput?: boolean }) { super(options); this.program @@ -26,8 +26,8 @@ The uniprot command yields just the protein sequences as a default, but can retu .addOption(new Option("-f, --format ", `output format`).choices(Uniprot.VALID_FORMATS).default("sequence")); } - async run() { - this.parseArguments(); + async run(args?: string[]) { + this.parseArguments(args); const format = this.program.opts().format; const accessions = this.program.args; diff --git a/tests/commands/peptfilter.test.ts b/tests/commands/peptfilter.test.ts index 0318fde2..0f13096b 100644 --- a/tests/commands/peptfilter.test.ts +++ b/tests/commands/peptfilter.test.ts @@ -80,8 +80,8 @@ test('test if it passes fasta from stdin', async () => { test('test complex example from stdin', async () => { const stdin = mock.stdin(); - const command = new Peptfilter({ args: ["--minlen", "4", "--maxlen", "10", "--lacks", "B", "--contains", "A"] }); - const run = command.run(); + const command = new Peptfilter(); + const run = command.run(["--minlen", "4", "--maxlen", "10", "--lacks", "B", "--contains", "A"]); stdin.send("A\n"); stdin.send("AAAAAAAAAAA\n"); diff --git a/tests/commands/prot2pept.test.ts b/tests/commands/prot2pept.test.ts index ca01533a..80d7e504 100644 --- a/tests/commands/prot2pept.test.ts +++ b/tests/commands/prot2pept.test.ts @@ -111,8 +111,8 @@ test('test fasta input 3', async () => { test('test custom pattern', async () => { const stdin = mock.stdin(); - const command = new Prot2pept({ args: ["--pattern", "([KR])([^A])"] }); - const run = command.run(); + const command = new Prot2pept(); + const run = command.run(["--pattern", "([KR])([^A])"]); stdin.send("AALTERAALTERPAALTER\n"); stdin.end(); diff --git a/tests/commands/uniprot.test.ts b/tests/commands/uniprot.test.ts index 59ba4f7f..27b7f304 100644 --- a/tests/commands/uniprot.test.ts +++ b/tests/commands/uniprot.test.ts @@ -17,8 +17,8 @@ beforeEach(() => { }); test('test single argument', async () => { - const command = new Uniprot({ args: ["Q6GZX3"] }); - await command.run(); + const command = new Uniprot(); + await command.run(["Q6GZX3"]); expect(writeSpy).toHaveBeenCalledTimes(1); expect(errorSpy).toHaveBeenCalledTimes(0); @@ -26,8 +26,8 @@ test('test single argument', async () => { }); test('test two arguments', async () => { - const command = new Uniprot({ args: ["Q6GZX3", "Q6GZX4"] }); - await command.run(); + const command = new Uniprot(); + await command.run(["Q6GZX3", "Q6GZX4"]); expect(writeSpy).toHaveBeenCalledTimes(2); expect(errorSpy).toHaveBeenCalledTimes(0); @@ -35,8 +35,8 @@ test('test two arguments', async () => { }); test('test fasta output', async () => { - const command = new Uniprot({ args: ["--format", "fasta", "Q6GZX3", "Q6GZX4"] }); - await command.run(); + const command = new Uniprot(); + await command.run(["--format", "fasta", "Q6GZX3", "Q6GZX4"]); expect(writeSpy).toHaveBeenCalledTimes(2); expect(errorSpy).toHaveBeenCalledTimes(0); @@ -78,16 +78,16 @@ test('test double line stdin', async () => { }); test('test on invalid id', async () => { - const command = new Uniprot({ args: ["Bart"] }); - await command.run(); + const command = new Uniprot(); + await command.run(["Bart"]); expect(errorSpy).toHaveBeenCalledTimes(1); }); test('test all valid formats', async () => { for (const format of Uniprot.VALID_FORMATS) { - const command = new Uniprot({ args: ["--format", format, "Q6GZX3"] }); - await command.run(); + const command = new Uniprot(); + await command.run(["--format", format, "Q6GZX3"]); expect(errorSpy).toHaveBeenCalledTimes(0); }