From 74b81b94be68c27a6f2b5e57fc35e90f7131c9ac Mon Sep 17 00:00:00 2001 From: Alberto Ricart Date: Sun, 18 Jul 2021 16:42:52 -0500 Subject: [PATCH] [FEAT] if --help flag is specified on a cmd, the `long` help is printed, otherwise the `use` is printed. [FEAT] When a flag has a conflict on `name` or `short`, it now will print the first match of the conflict to help fix the conflict. --- mod.ts | 59 ++++++++++++++++++++++++++++++---------------------------- 1 file changed, 31 insertions(+), 28 deletions(-) diff --git a/mod.ts b/mod.ts index f0684a2..c8a48e3 100644 --- a/mod.ts +++ b/mod.ts @@ -1,6 +1,25 @@ import { parse } from "https://deno.land/std/flags/mod.ts"; import { sprintf } from "https://deno.land/std/fmt/printf.ts"; +export interface Flag { + type: "string" | "boolean" | "number"; + name: string; + usage: string; + short: string; + required: boolean; + persistent: boolean; + changed: boolean; + default: null | unknown | unknown[]; + value: null | unknown | unknown[]; +} + +export interface Flags { + value(n: string): T; + values(n: string): T[]; + getFlag(n: string): Flag | null; + checkRequired(): void; +} + export type Run = ( cmd: Command, args: string[], @@ -19,18 +38,6 @@ export function isCommand(t: Command | Cmd): t is Command { return v.cmd !== undefined; } -export interface Flag { - type: "string" | "boolean" | "number"; - name: string; - usage: string; - short: string; - required: boolean; - persistent: boolean; - changed: boolean; - default: null | unknown | unknown[]; - value: null | unknown | unknown[]; -} - function flag(f: Partial): Flag { const d = {} as Flag; d.usage = ""; @@ -44,13 +51,6 @@ function flag(f: Partial): Flag { return Object.assign(d, f); } -export interface Flags { - value(n: string): T; - values(n: string): T[]; - getFlag(n: string): Flag | null; - checkRequired(): void; -} - export class FlagsImpl implements Flags { m: Map; @@ -158,11 +158,12 @@ export class Command implements Cmd { Deno.stderr.writeSync(new TextEncoder().encode(s)); } - help(): void { + help(long = false): void { // usage for the parent - this.stderr(`${this.short ?? this.use}\n\n`); - if (this.long) { - this.stderr(`${this.long}`); + if (long) { + this.stderr(`${this.long ?? this.short ?? this.use}\n`); + } else { + this.stderr(`${this.short ?? this.use}\n`); } this.stderr("\nUsage:\n"); if (!this.commands) { @@ -210,7 +211,11 @@ export class Command implements Cmd { return sn || sh; }); if (f.length > 0) { - return true; + throw new Error( + `--${flag.name} has conflict with: --${f[0].name} ${ + f[0].short ? "-" + f[0].short : "" + }`, + ); } } if (this.parent) { @@ -224,9 +229,7 @@ export class Command implements Cmd { pf.name = pf.name ?? ""; pf.short = pf.short ?? ""; this.flags = this.flags ?? []; - if (this.checkFlags(pf)) { - throw new Error(`flag ${pf.name} already exists`); - } + this.checkFlags(pf); this.flags.push(pf); return pf; } @@ -422,7 +425,7 @@ export class RootCommand extends Command implements Execute { this.lastCmd = { cmd: cmd, args: a, flags: fm }; if (this._help.value) { - cmd.help(); + cmd.help(true); this.lastCmd.helped = true; return Promise.resolve(1); }