Skip to content

Commit

Permalink
feat(command): add globalOption, globalType, globalComplete and globa…
Browse files Browse the repository at this point in the history
…lEnv alias method's (#152)
  • Loading branch information
c4spar committed Feb 23, 2021
1 parent 0972cc0 commit e1a6bb2
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 8 deletions.
35 changes: 35 additions & 0 deletions command/command.ts
Original file line number Diff line number Diff line change
Expand Up @@ -436,6 +436,14 @@ export class Command<O = any, A extends Array<any> = any> {
return this;
}

public globalType(
name: string,
type: Type<unknown> | ITypeHandler<unknown>,
options?: Omit<ITypeOptions, "global">,
): this {
return this.type(name, type, { ...options, global: true });
}

/**
* Register custom type.
* @param name The name of the type.
Expand Down Expand Up @@ -465,6 +473,14 @@ export class Command<O = any, A extends Array<any> = any> {
return this;
}

public globalComplete(
name: string,
complete: ICompleteHandler,
options?: Omit<ICompleteOptions, "global">,
): this {
return this.complete(name, complete, { ...options, global: true });
}

/**
* Register command specific custom type.
* @param name The name of the completion.
Expand Down Expand Up @@ -526,6 +542,17 @@ export class Command<O = any, A extends Array<any> = any> {
return this.cmd.throwOnError || !!this.cmd._parent?.shouldThrowErrors();
}

public globalOption(
flags: string,
desc: string,
opts?: Omit<ICommandOption, "global"> | IFlagValueHandler,
): this {
if (typeof opts === "function") {
return this.option(flags, desc, { value: opts, global: true });
}
return this.option(flags, desc, { ...opts, global: true });
}

/**
* Add a new option.
* @param flags Flags string like: -h, --help, --manual <requiredArg:string> [optionalArg: number] [...restArgs:string]
Expand Down Expand Up @@ -617,6 +644,14 @@ export class Command<O = any, A extends Array<any> = any> {
return this;
}

public globalEnv(
name: string,
description: string,
options?: Omit<IEnvVarOptions, "global">,
): this {
return this.env(name, description, { ...options, global: true });
}

/**
* Add new environment variable.
* @param name Name of the environment variable.
Expand Down
32 changes: 31 additions & 1 deletion command/test/command/environment_variable_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ function command(): Command {
.version("1.0.0")
.description("Test description ...")
.env("SOME_ENV_VAR=<val:number>", "number env var description ...")
.env("SOME_OTHER_ENV_VAR=<val:boolean>", "boolean env var description ...");
.globalEnv(
"SOME_OTHER_ENV_VAR=<val:boolean>",
"boolean env var description ...",
)
.command("foo");
}

Deno.test("command environment variable number", async () => {
Expand Down Expand Up @@ -61,3 +65,29 @@ Deno.test("command environment variable invalid boolean", async () => {

Deno.env.set("SOME_ENV_VAR", "");
});

Deno.test("command global environment variable boolean", async () => {
Deno.env.set("SOME_OTHER_ENV_VAR", "true");

const cmd: Command = command();
const { options, args } = await cmd.parse(["foo"]);

assertEquals(options, {});
assertEquals(args, []);

Deno.env.set("SOME_ENV_VAR", "");
});

Deno.test("command global environment variable invalid boolean", async () => {
Deno.env.set("SOME_OTHER_ENV_VAR", "2");

await assertThrowsAsync(
async () => {
await command().parse(["foo"]);
},
Error,
`Environment variable "SOME_OTHER_ENV_VAR" must be of type "boolean", but got "2".`,
);

Deno.env.set("SOME_ENV_VAR", "");
});
19 changes: 13 additions & 6 deletions command/test/option/global_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ const cmd = new Command()
"Available on all command's.",
{ global: true },
)
.globalOption("-G, --global2 [val:string]", "Available on all command's.")
.command(
"sub-command",
new Command()
Expand All @@ -32,25 +33,31 @@ const cmd = new Command()
);

Deno.test("command with global option", async () => {
const { options, args } = await cmd.parse(["-g", "halo"]);
const { options, args } = await cmd.parse(["-g", "halo", "-G", "halo"]);

assertEquals(options, { global: "HALO" });
assertEquals(options, { global: "HALO", global2: "halo" });
assertEquals(args, []);
});

Deno.test("sub command with global option", async () => {
const { options, args } = await cmd.parse(["sub-command", "-g", "halo"]);
const { options, args } = await cmd.parse([
"sub-command",
"-g",
"halo",
"-G",
"halo",
]);

assertEquals(options, { global: "HALO" });
assertEquals(options, { global: "HALO", global2: "halo" });
assertEquals(args, []);
});

Deno.test("nested sub command with global option", async () => {
const { options, args } = await cmd.parse(
["sub-command", "sub-command", "-g", "halo"],
["sub-command", "sub-command", "-g", "halo", "-G", "halo"],
);

assertEquals(options, { global: "HALO" });
assertEquals(options, { global: "HALO", global2: "halo" });
assertEquals(args, []);
});

Expand Down
2 changes: 1 addition & 1 deletion command/test/type/custom_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ const email = (): ITypeHandler<string> => {
const cmd = new Command()
.throwErrors()
.type("email", email())
.type("email2", email(), { global: true })
.globalType("email2", email())
.option("-e, --email [value:email]", "description ...")
.option("-E, --email2 [value:email2]", "description ...")
.command("init")
Expand Down

0 comments on commit e1a6bb2

Please sign in to comment.