Skip to content

Commit 46ec49f

Browse files
committed
chore: exit with an error on unknown cli arguments
1 parent d46b44b commit 46ec49f

File tree

2 files changed

+38
-6
lines changed

2 files changed

+38
-6
lines changed

src/common/config.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,13 @@ function parseCliConfig(args: string[]): CliOptions {
306306
// so we don't have a logger. For stdio, the warning will be received as a string in
307307
// the client and IDEs like VSCode do show the message in the log window. For HTTP,
308308
// it will be in the stdout of the process.
309-
warnAboutDeprecatedOrUnknownCliArgs({ ...parsed, _: positionalArguments }, console.warn);
309+
warnAboutDeprecatedOrUnknownCliArgs(
310+
{ ...parsed, _: positionalArguments },
311+
{
312+
warn: console.warn,
313+
exit: process.exit,
314+
}
315+
);
310316

311317
// if we have a positional argument that matches a connection string
312318
// store it as the connection specifier and remove it from the argument
@@ -319,7 +325,10 @@ function parseCliConfig(args: string[]): CliOptions {
319325
return parsed;
320326
}
321327

322-
export function warnAboutDeprecatedOrUnknownCliArgs(args: Record<string, unknown>, warn: (msg: string) => void): void {
328+
export function warnAboutDeprecatedOrUnknownCliArgs(
329+
args: Record<string, unknown>,
330+
{ warn, exit }: { warn: (msg: string) => void; exit: (status: number) => void | never }
331+
): void {
323332
let usedDeprecatedArgument = false;
324333
let usedInvalidArgument = false;
325334

@@ -353,6 +362,10 @@ export function warnAboutDeprecatedOrUnknownCliArgs(args: Record<string, unknown
353362
if (usedInvalidArgument || usedDeprecatedArgument) {
354363
warn("Refer to https://www.mongodb.com/docs/mcp-server/get-started/ for setting up the MCP Server.");
355364
}
365+
366+
if (usedInvalidArgument) {
367+
exit(1);
368+
}
356369
}
357370

358371
function commaSeparatedToArray<T extends string[]>(str: string | string[] | undefined): T {

tests/unit/common/config.test.ts

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -655,12 +655,14 @@ describe("CLI arguments", () => {
655655
describe(`deprecation behaviour of ${cliArg}`, () => {
656656
let cliArgs: CliOptions & UserConfig & { _?: string[] };
657657
let warn: (msg: string) => void;
658+
let exit: (status: number) => void | never;
658659

659660
beforeEach(() => {
660661
cliArgs = { [cliArg]: "RandomString" } as unknown as CliOptions & UserConfig & { _?: string[] };
661662
warn = vi.fn();
663+
exit = vi.fn();
662664

663-
warnAboutDeprecatedOrUnknownCliArgs(cliArgs as unknown as Record<string, unknown>, warn);
665+
warnAboutDeprecatedOrUnknownCliArgs(cliArgs as unknown as Record<string, unknown>, { warn, exit });
664666
});
665667

666668
it(`warns the usage of ${cliArg} as it is deprecated`, () => {
@@ -670,22 +672,28 @@ describe("CLI arguments", () => {
670672
it(`shows the reference message when ${cliArg} was passed`, () => {
671673
expect(warn).toHaveBeenCalledWith(referDocMessage);
672674
});
675+
676+
it(`should not exit the process`, () => {
677+
expect(exit).not.toHaveBeenCalled();
678+
});
673679
});
674680
}
675681

676682
describe("invalid arguments", () => {
677683
let warn: (msg: string) => void;
684+
let exit: (status: number) => void | never;
678685

679686
beforeEach(() => {
680687
warn = vi.fn();
688+
exit = vi.fn();
681689
});
682690

683691
it("should show a warning when an argument is not known", () => {
684692
warnAboutDeprecatedOrUnknownCliArgs(
685693
{
686694
wakanda: "",
687695
},
688-
warn
696+
{ warn, exit }
689697
);
690698

691699
expect(warn).toHaveBeenCalledWith("Invalid command line argument 'wakanda'.");
@@ -694,12 +702,23 @@ describe("CLI arguments", () => {
694702
);
695703
});
696704

705+
it("should exit the process on unknown cli args", () => {
706+
warnAboutDeprecatedOrUnknownCliArgs(
707+
{
708+
wakanda: "",
709+
},
710+
{ warn, exit }
711+
);
712+
713+
expect(exit).toHaveBeenCalledWith(1);
714+
});
715+
697716
it("should show a suggestion when is a simple typo", () => {
698717
warnAboutDeprecatedOrUnknownCliArgs(
699718
{
700719
readonli: "",
701720
},
702-
warn
721+
{ warn, exit }
703722
);
704723

705724
expect(warn).toHaveBeenCalledWith("Invalid command line argument 'readonli'. Did you mean 'readOnly'?");
@@ -713,7 +732,7 @@ describe("CLI arguments", () => {
713732
{
714733
readonly: "",
715734
},
716-
warn
735+
{ warn, exit }
717736
);
718737

719738
expect(warn).toHaveBeenCalledWith("Invalid command line argument 'readonly'. Did you mean 'readOnly'?");

0 commit comments

Comments
 (0)