Skip to content

Commit ec908ac

Browse files
fix: add input validation for type
1 parent 036ae20 commit ec908ac

2 files changed

Lines changed: 20 additions & 4 deletions

File tree

src/commands/view/module.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,27 @@
44

55
import { Configurator, IModule } from "../../core/command/module-handler";
66
import { Context } from "../../core/command/cli-context";
7-
import { Command, OptionValues } from "commander";
7+
import { Command, InvalidArgumentError, OptionValues } from "commander";
88
import { ViewBookmarksCommandService } from "./view-bookmarks-command.service";
99

10+
const VIEW_BOOKMARK_TYPES = ["USER", "SHARED", "ALL"];
11+
12+
function parseViewBookmarkType(value: string): string {
13+
const normalized = value.toUpperCase();
14+
if (!VIEW_BOOKMARK_TYPES.includes(normalized)) {
15+
throw new InvalidArgumentError(`Allowed values are: ${VIEW_BOOKMARK_TYPES.join(", ")}.`);
16+
}
17+
return normalized;
18+
}
19+
1020
class Module extends IModule {
1121

1222
public register(context: Context, configurator: Configurator): void {
1323
const pullCommand = configurator.command("pull");
1424
pullCommand
1525
.command("view-bookmarks")
1626
.description("Command to pull view bookmarks")
17-
.option("--type <type>", "Type of view bookmarks to pull: USER (default), SHARED, or ALL")
27+
.option("--type <type>", "Type of view bookmarks to pull: USER (default), SHARED, or ALL", parseViewBookmarkType)
1828
.requiredOption("--id <id>", "ID of the view (board) to pull bookmarks from")
1929
.action(this.pullViewBookmarks);
2030

src/core/command/module-handler.ts

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,14 @@ export class CommandConfig {
181181
return this;
182182
}
183183

184-
public option(flags: string, description?: string, defaultValue?: string | boolean | string[]): CommandConfig {
185-
this.cmd.option(flags, description, defaultValue);
184+
public option(flags: string, description?: string, defaultValue?: string | boolean | string[]): CommandConfig;
185+
public option(flags: string, description: string, parseArg: (value: string, previous: unknown) => unknown, defaultValue?: string | boolean | string[]): CommandConfig;
186+
public option(flags: string, description?: string, parseArgOrDefault?: ((value: string, previous: unknown) => unknown) | string | boolean | string[], defaultValue?: string | boolean | string[]): CommandConfig {
187+
if (typeof parseArgOrDefault === "function") {
188+
this.cmd.option(flags, description, parseArgOrDefault, defaultValue);
189+
} else {
190+
this.cmd.option(flags, description, parseArgOrDefault);
191+
}
186192
return this;
187193
}
188194

0 commit comments

Comments
 (0)