Skip to content

Commit

Permalink
fix: ArgumentsReader should warn if missing a value
Browse files Browse the repository at this point in the history
Closes #1429.
  • Loading branch information
Gerrit0 committed Dec 29, 2020
1 parent 20febfd commit 02b915d
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 1 deletion.
10 changes: 10 additions & 0 deletions src/lib/utils/options/readers/arguments.ts
Expand Up @@ -16,6 +16,10 @@ export class ArgumentsReader implements OptionsReader {
}

read(container: Options, logger: Logger): void {
logger.verbose(
`Arguments reader reading with: ${JSON.stringify(this.args)}`
);

// Make container's type more lax, we do the appropriate checks manually.
const options = container as Options & {
setValue(name: string, value: unknown): void;
Expand Down Expand Up @@ -57,6 +61,12 @@ export class ArgumentsReader implements OptionsReader {
index--;
}
} else {
if (index === this.args.length) {
// Only boolean values have optional values.
logger.warn(
`--${decl.name} expected a value, but none was given as an argument`
);
}
trySet(decl.name, this.args[index]);
}
seen.add(decl.name);
Expand Down
19 changes: 18 additions & 1 deletion src/test/utils/options/readers/arguments.test.ts
@@ -1,4 +1,4 @@
import { deepStrictEqual as equal } from "assert";
import { deepStrictEqual as equal, ok } from "assert";

import { Options, Logger } from "../../../../lib/utils";
import { ArgumentsReader } from "../../../../lib/utils/options/readers";
Expand Down Expand Up @@ -137,4 +137,21 @@ describe("Options - ArgumentsReader", () => {
options.removeReaderByName(reader.name);
equal(check, true, "Reader did not report an error.");
});

it("Warns if option is expecting a value but no value is provided", () => {
let check = false;
class TestLogger extends Logger {
warn(msg: string) {
ok(msg.includes("--out"));
check = true;
}
}

const reader = new ArgumentsReader(1, ["--out"]);
options.reset();
options.addReader(reader);
options.read(new TestLogger());
options.removeReaderByName(reader.name);
equal(check, true, "Reader did not report an error.");
});
});

0 comments on commit 02b915d

Please sign in to comment.