Skip to content

Commit

Permalink
[feat] added function to flags to assert that required flags are prov…
Browse files Browse the repository at this point in the history
…ided
  • Loading branch information
aricart committed Jul 17, 2021
1 parent b9f7e98 commit 23077b3
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 6 deletions.
4 changes: 2 additions & 2 deletions main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ const hello = root.addCommand({
// this is the handler for the command, you get
// the command being executed, any args following a `--`
// and an object to let you access relevant flags.
run: (cmd, args, flags): Promise<number> => {
run: (cmd, _args, flags): Promise<number> => {
const strong = (flags.value<boolean>("strong") ?? false) ? "!!!" : "";
let n = flags.value<string>("name");
n = n === "" ? "mystery person" : n;
Expand All @@ -34,7 +34,7 @@ hello.addFlag({
const goodbye = root.addCommand({
use: "goodbye --name string [--strong]",
short: "says goodbye",
run: (cmd, args, flags): Promise<number> => {
run: (cmd, _args, flags): Promise<number> => {
const strong = (flags.value<boolean>("strong") ?? false) ? "!!!" : "";
let n = flags.value<string>("name");
n = n === "" ? "mystery person" : n;
Expand Down
28 changes: 25 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ export interface Flags {
value<T>(n: string): T;
values<T>(n: string): T[];
getFlag(n: string): Flag | null;
checkRequired(): void;
}

export class FlagsImpl implements Flags {
Expand Down Expand Up @@ -108,13 +109,22 @@ export class FlagsImpl implements Flags {
}
return v as T[];
}

checkRequired() {
this.m.forEach((f) => {
if (f.required && f.default === f.value) {
throw new Error(`--${f.name} is required`);
}
});
}
}

export class Command implements Cmd {
cmd: Cmd;
commands!: Command[];
parent!: Command;
flags!: Flag[];
showHelp: false;

constructor(cmd = {} as Cmd) {
if (!cmd.use) throw new Error("use is required");
Expand All @@ -124,6 +134,7 @@ export class Command implements Cmd {
return Promise.resolve(1);
};
}
this.showHelp = false;
this.cmd = cmd;
}

Expand Down Expand Up @@ -254,8 +265,20 @@ export class Command implements Cmd {
return this.parent.getFlag(name);
}

run(cmd: Command, args: string[], flags: Flags): Promise<number> {
return this.cmd.run(cmd, args, flags);
async run(cmd: Command, args: string[], flags: Flags): Promise<number> {
try {
const exit = await this.cmd.run(cmd, args, flags);
if (exit > 0 && this.showHelp) {
cmd.help();
}
return exit;
} catch (err) {
cmd.stderr(`${err.message}\n`);
if (cmd.showHelp) {
cmd.help();
}
return 1;
}
}

addCommand(cmd: Cmd | Command): Command {
Expand Down Expand Up @@ -396,7 +419,6 @@ export class RootCommand extends Command implements Execute {
});

const fm = new FlagsImpl(flags);

this.lastCmd = { cmd: cmd, args: a, flags: fm };

if (this._help.value) {
Expand Down
2 changes: 1 addition & 1 deletion test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import {
assertEquals,
assertThrows,
} from "https://deno.land/std/testing/asserts.ts";
import { cli, Cmd, Command, Flag, Flags } from "./mod.ts";
import { cli, Cmd, Command, Flags } from "./mod.ts";

export function buildCmd(v: Partial<Cmd>, debug = false): Cmd {
const d = {
Expand Down

0 comments on commit 23077b3

Please sign in to comment.